diff --git a/kernel/include/memory/UserHeap.h b/kernel/include/memory/UserHeap.h index d1c85814..166c6a5c 100644 --- a/kernel/include/memory/UserHeap.h +++ b/kernel/include/memory/UserHeap.h @@ -23,4 +23,5 @@ struct UserHeap void bitmap_set(uint64_t index, bool value); bool try_expand(); + bool try_expand_size(uint64_t size); }; \ No newline at end of file diff --git a/kernel/src/memory/UserHeap.cpp b/kernel/src/memory/UserHeap.cpp index bdcdad14..f25f09ed 100644 --- a/kernel/src/memory/UserHeap.cpp +++ b/kernel/src/memory/UserHeap.cpp @@ -5,6 +5,11 @@ #include "std/stdlib.h" #include "std/string.h" +#ifndef USER_HEAP_DEBUG +#undef kdbgln +#define kdbgln(...) +#endif + #ifndef PAGE_SIZE #define PAGE_SIZE 4096 #endif @@ -56,6 +61,21 @@ bool UserHeap::try_expand() return true; } +bool UserHeap::try_expand_size(uint64_t size) +{ + void* new_bitmap = krealloc(bitmap, bitmap_size + size); + if (!new_bitmap) + { + kdbgln("expansion failed"); + return false; + } + bitmap = (uint8_t*)new_bitmap; + memset(bitmap + bitmap_size, 0, size); + bitmap_size += size; + kdbgln("expanded user heap, bitmap at %p, size %ld", (void*)bitmap, bitmap_size); + return true; +} + bool UserHeap::bitmap_read(uint64_t index) { return (bitmap[index / 8] & (0b10000000 >> (index % 8))) > 0; @@ -81,7 +101,7 @@ allocate: return ALLOC_BASE + (index * PAGE_SIZE); } - if (attempts < 5 && try_expand()) + if (attempts == 0 && try_expand()) // We are allocating ONE PAGE, only one attempt should be necessary. { attempts++; goto allocate;