Join page_block insert methods into one

This commit is contained in:
Justin C. Miller
2018-04-22 23:27:15 -07:00
parent 1de73de2e3
commit 1113164505
3 changed files with 26 additions and 31 deletions

View File

@@ -256,12 +256,12 @@ gather_block_lists(
} }
if (block->has_flag(page_block_flags::used)) { 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; block->flags |= page_block_flags::mapped;
used = page_block::insert_virtual(used, block); used = page_block::insert(used, block);
} else { } else {
free = page_block::insert_physical(free, block); free = page_block::insert(free, block);
} }
desc = desc_incr(desc, desc_length); 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 // Add it to the used list
removed->virtual_address = free_region_start_virt; removed->virtual_address = free_region_start_virt;
removed->flags = page_block_flags::used; 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 // Pull out the block that represents the rest
uint64_t free_next_phys = free_region_start_phys + used; 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 // Record that we're about to remap it into the page table address space
removed->virtual_address = pt_start_virt; removed->virtual_address = pt_start_virt;
removed->flags = page_block_flags::used; 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 // Actually remap them into page table space
page_out(&tables[0], free_next, remaining_pages); page_out(&tables[0], free_next, remaining_pages);

View File

@@ -33,14 +33,14 @@ page_block::append(page_block *list, page_block *extra)
} }
page_block * 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; if (list == nullptr) return block;
else if (block == nullptr) return list; else if (block == nullptr) return list;
page_block *cur = list; page_block *cur = list;
page_block *prev = nullptr; page_block *prev = nullptr;
while (cur && cur->physical_address < block->physical_address) { while (cur && page_block::compare(block, cur) > 0) {
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
} }
@@ -53,25 +53,20 @@ page_block::insert_physical(page_block *list, page_block *block)
return block; return block;
} }
page_block * int
page_block::insert_virtual(page_block *list, page_block *block) page_block::compare(const page_block *lhs, const page_block *rhs)
{ {
if (list == nullptr) return block; if (lhs->virtual_address < rhs->virtual_address)
else if (block == nullptr) return list; return -1;
else if (lhs->virtual_address > rhs->virtual_address)
return 1;
page_block *cur = list; if (lhs->physical_address < rhs->physical_address)
page_block *prev = nullptr; return -1;
while (cur && cur->virtual_address < block->virtual_address) { else if (lhs->physical_address > rhs->physical_address)
prev = cur; return 1;
cur = cur->next;
}
block->next = cur; return 0;
if (prev) {
prev->next = block;
return list;
}
return block;
} }
page_block * page_block *
@@ -300,7 +295,7 @@ page_manager::unmap_pages(uint64_t address, unsigned count)
cur->next = nullptr; cur->next = nullptr;
cur->virtual_address = 0; cur->virtual_address = 0;
cur->flags = cur->flags & ~(page_block_flags::used | page_block_flags::mapped); 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; cur = next;
} }

View File

@@ -154,17 +154,17 @@ struct page_block
/// \returns The new list head /// \returns The new list head
static page_block * append(page_block *list, page_block *extra); 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 list The list to insert into
/// \arg block The single block to insert /// \arg block The single block to insert
/// \returns The new list head /// \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. /// Compare two blocks by address.
/// \arg list The list to insert into /// \arg lhs The left-hand comparator
/// \arg block The single block to insert /// \arg rhs The right-hand comparator
/// \returns The new list head /// \returns <0 if lhs is sorts earlier, >0 if lhs sorts later, 0 for equal
static page_block * insert_virtual(page_block *list, page_block *block); static int compare(const page_block *lhs, const page_block *rhs);
/// Traverse the list, joining adjacent blocks where possible. /// Traverse the list, joining adjacent blocks where possible.
/// \arg list The list to consolidate /// \arg list The list to consolidate