diff --git a/src/kernel/memory_bootstrap.cpp b/src/kernel/memory_bootstrap.cpp index cee6837..d0b4fca 100644 --- a/src/kernel/memory_bootstrap.cpp +++ b/src/kernel/memory_bootstrap.cpp @@ -256,12 +256,12 @@ gather_block_lists( } if (block->has_flag(page_block_flags::used)) { - if (block->virtual_address == block->physical_address) + if (block->virtual_address || !block->physical_address) block->flags |= page_block_flags::mapped; - used = page_block::insert_virtual(used, block); + used = page_block::insert(used, block); } else { - free = page_block::insert_physical(free, block); + free = page_block::insert(free, block); } desc = desc_incr(desc, desc_length); @@ -415,7 +415,7 @@ memory_initialize_managers(const void *memory_map, size_t map_length, size_t des // Add it to the used list removed->virtual_address = free_region_start_virt; removed->flags = page_block_flags::used; - used_head = page_block::insert_virtual(used_head, removed); + used_head = page_block::insert(used_head, removed); // Pull out the block that represents the rest uint64_t free_next_phys = free_region_start_phys + used; @@ -436,7 +436,7 @@ memory_initialize_managers(const void *memory_map, size_t map_length, size_t des // Record that we're about to remap it into the page table address space removed->virtual_address = pt_start_virt; removed->flags = page_block_flags::used; - used_head = page_block::insert_virtual(used_head, removed); + used_head = page_block::insert(used_head, removed); // Actually remap them into page table space page_out(&tables[0], free_next, remaining_pages); diff --git a/src/kernel/memory_pages.cpp b/src/kernel/memory_pages.cpp index 0c12a16..d0d5d58 100644 --- a/src/kernel/memory_pages.cpp +++ b/src/kernel/memory_pages.cpp @@ -33,14 +33,14 @@ page_block::append(page_block *list, page_block *extra) } page_block * -page_block::insert_physical(page_block *list, page_block *block) +page_block::insert(page_block *list, page_block *block) { if (list == nullptr) return block; else if (block == nullptr) return list; page_block *cur = list; page_block *prev = nullptr; - while (cur && cur->physical_address < block->physical_address) { + while (cur && page_block::compare(block, cur) > 0) { prev = cur; cur = cur->next; } @@ -53,25 +53,20 @@ page_block::insert_physical(page_block *list, page_block *block) return block; } -page_block * -page_block::insert_virtual(page_block *list, page_block *block) +int +page_block::compare(const page_block *lhs, const page_block *rhs) { - if (list == nullptr) return block; - else if (block == nullptr) return list; + if (lhs->virtual_address < rhs->virtual_address) + return -1; + else if (lhs->virtual_address > rhs->virtual_address) + return 1; - page_block *cur = list; - page_block *prev = nullptr; - while (cur && cur->virtual_address < block->virtual_address) { - prev = cur; - cur = cur->next; - } + if (lhs->physical_address < rhs->physical_address) + return -1; + else if (lhs->physical_address > rhs->physical_address) + return 1; - block->next = cur; - if (prev) { - prev->next = block; - return list; - } - return block; + return 0; } page_block * @@ -300,7 +295,7 @@ page_manager::unmap_pages(uint64_t address, unsigned count) cur->next = nullptr; cur->virtual_address = 0; cur->flags = cur->flags & ~(page_block_flags::used | page_block_flags::mapped); - m_free = page_block::insert_physical(m_free, cur); + m_free = page_block::insert(m_free, cur); cur = next; } diff --git a/src/kernel/memory_pages.h b/src/kernel/memory_pages.h index 69c3d9a..da37f95 100644 --- a/src/kernel/memory_pages.h +++ b/src/kernel/memory_pages.h @@ -154,17 +154,17 @@ struct page_block /// \returns The new list head static page_block * append(page_block *list, page_block *extra); - /// Sorted-insert of a block into the list by physical address. + /// Sorted-insert of a block into the list by address. /// \arg list The list to insert into /// \arg block The single block to insert /// \returns The new list head - static page_block * insert_physical(page_block *list, page_block *block); + static page_block * insert(page_block *list, page_block *block); - /// Sorted-insert of a block into the list by virtual address. - /// \arg list The list to insert into - /// \arg block The single block to insert - /// \returns The new list head - static page_block * insert_virtual(page_block *list, page_block *block); + /// Compare two blocks by address. + /// \arg lhs The left-hand comparator + /// \arg rhs The right-hand comparator + /// \returns <0 if lhs is sorts earlier, >0 if lhs sorts later, 0 for equal + static int compare(const page_block *lhs, const page_block *rhs); /// Traverse the list, joining adjacent blocks where possible. /// \arg list The list to consolidate