UserHeap: some nice improvements
This commit is contained in:
parent
64f5078494
commit
92634048fc
@ -23,4 +23,5 @@ struct UserHeap
|
||||
void bitmap_set(uint64_t index, bool value);
|
||||
|
||||
bool try_expand();
|
||||
bool try_expand_size(uint64_t size);
|
||||
};
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user