- // Next fit approach. Maximize the throughput!
- uintptr_t good_page_found = (uintptr_t)NULL;
- size_t old_pg_ptr = pg_lookup_ptr;
- size_t upper_lim = max_pg;
- struct pp_struct* pm;
- while (!good_page_found && pg_lookup_ptr < upper_lim) {
- pm = &pm_table[pg_lookup_ptr];
-
- // skip the fully occupied chunk, reduce # of iterations
- if (!pm->ref_counts) {
- *pm = (struct pp_struct) {
- .attr = attr,
- .owner = owner,
- .ref_counts = 1
- };
- good_page_found = pg_lookup_ptr << 12;
- } else {
- pg_lookup_ptr++;
-
- // We've searched the interval [old_pg_ptr, max_pg) but failed
- // may be chances in [1, old_pg_ptr) ?
- // Let's find out!
- if (pg_lookup_ptr >= upper_lim && old_pg_ptr != LOOKUP_START) {
- upper_lim = old_pg_ptr;
- pg_lookup_ptr = LOOKUP_START;
- old_pg_ptr = LOOKUP_START;
- }
- }
- }
- if (!good_page_found) {
- __current->k_status = LXOUTOFMEM;
- }
- return (void*)good_page_found;