SImplify init_physical_frame_allocator() even more

This commit is contained in:
apio 2022-12-04 15:50:21 +01:00
parent 5f6c48bd12
commit 0d10c98477
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -54,29 +54,26 @@ namespace MemoryManager
void init_physical_frame_allocator()
{
u64 total_mem = 0;
MemoryMapIterator iter;
MemoryMapEntry entry;
while (iter.next().try_set_value(entry)) { total_mem += entry.size; }
auto largest_free = iter.largest_free();
void* biggest_usable_memory_block = (void*)largest_free.ptr;
u64 biggest_usable_memory_block_size = largest_free.size;
expect(largest_free.free, "We were given a largest free block that isn't even free!");
// The entire physical address space. May contain inexistent memory holes, thus differs from total_mem which
// only counts existent memory. Our bitmap needs to have space for all of the physical address space, since
// usable addresses will be scattered across it.
usize physical_address_space_size = get_physical_address_space_size();
char* frame_bitmap_addr = (char*)biggest_usable_memory_block;
// We store our frame bitmap at the beginning of the largest free memory block.
char* frame_bitmap_addr = (char*)largest_free.ptr;
usize frame_bitmap_size = physical_address_space_size / ARCH_PAGE_SIZE / 8 + 1;
// This should never happen, unless memory is very fragmented. Usually there is always a very big block of
// usable memory and then some tiny blocks around it.
if (frame_bitmap_size >= biggest_usable_memory_block_size) [[unlikely]]
if (frame_bitmap_size >= largest_free.size) [[unlikely]]
{
kerrorln("ERROR: No single memory block is enough to hold the frame bitmap");
CPU::efficient_halt();