UserHeap: some nice improvements

This commit is contained in:
apio 2022-10-17 18:49:19 +02:00
parent 64f5078494
commit 92634048fc
2 changed files with 22 additions and 1 deletions

View File

@ -23,4 +23,5 @@ struct UserHeap
void bitmap_set(uint64_t index, bool value); void bitmap_set(uint64_t index, bool value);
bool try_expand(); bool try_expand();
bool try_expand_size(uint64_t size);
}; };

View File

@ -5,6 +5,11 @@
#include "std/stdlib.h" #include "std/stdlib.h"
#include "std/string.h" #include "std/string.h"
#ifndef USER_HEAP_DEBUG
#undef kdbgln
#define kdbgln(...)
#endif
#ifndef PAGE_SIZE #ifndef PAGE_SIZE
#define PAGE_SIZE 4096 #define PAGE_SIZE 4096
#endif #endif
@ -56,6 +61,21 @@ bool UserHeap::try_expand()
return true; 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) bool UserHeap::bitmap_read(uint64_t index)
{ {
return (bitmap[index / 8] & (0b10000000 >> (index % 8))) > 0; return (bitmap[index / 8] & (0b10000000 >> (index % 8))) > 0;
@ -81,7 +101,7 @@ allocate:
return ALLOC_BASE + (index * PAGE_SIZE); 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++; attempts++;
goto allocate; goto allocate;