diff --git a/kernel/src/memory/KernelVM.cpp b/kernel/src/memory/KernelVM.cpp index 0a4a445b..dbb4eb85 100644 --- a/kernel/src/memory/KernelVM.cpp +++ b/kernel/src/memory/KernelVM.cpp @@ -21,6 +21,7 @@ namespace KernelVM void init() { g_kernelvm_bitmap.initialize(bitmap_memory, sizeof(bitmap_memory)); + g_kernelvm_bitmap.clear(false); } Result alloc_one_page() @@ -36,7 +37,7 @@ namespace KernelVM return err(ENOMEM); } - Result alloc_several_pages(usize count) + bool find_several_pages_impl(usize count, u64& start_index) { u64 first_free_index = 0; u64 free_contiguous_pages = 0; @@ -56,12 +57,24 @@ namespace KernelVM // Found enough contiguous free pages!! if (free_contiguous_pages == count) { - g_used_vm += ARCH_PAGE_SIZE * count; - g_kernelvm_bitmap.clear_region(first_free_index, count, true); - return KERNEL_VM_RANGE_START + (first_free_index * ARCH_PAGE_SIZE); + start_index = first_free_index; + return true; } } + return false; + } + + Result alloc_several_pages(const usize count) + { + u64 start_index; + if (find_several_pages_impl(count, start_index)) + { + g_kernelvm_bitmap.clear_region(start_index, count, true); + g_used_vm += ARCH_PAGE_SIZE * count; + return KERNEL_VM_RANGE_START + (start_index * ARCH_PAGE_SIZE); + } + return err(ENOMEM); }