Implemented Scratchpad_ReserveAligned and refactored internally
This commit is contained in:
parent
7d8ba6fc28
commit
c2c3c2d96e
2 changed files with 52 additions and 18 deletions
|
@ -30,18 +30,8 @@ static void* _Scratchpad_ReserveHere(Scratchpad* pad, size_t size)
|
|||
return scratch_buffer;
|
||||
}
|
||||
|
||||
void* Scratchpad_Reserve(Scratchpad* pad, size_t size)
|
||||
static int _Scratchpad_CreateEmbedded(Scratchpad* pad, size_t size)
|
||||
{
|
||||
if (pad == NULL) return NULL;
|
||||
|
||||
Scratchpad* current = pad;
|
||||
void* reserve = NULL;
|
||||
while (reserve == NULL && current != NULL) {
|
||||
reserve = _Scratchpad_ReserveHere(current, size);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if (reserve == NULL) {
|
||||
Scratchpad* new = Allocator_Allocate(pad->allocator, sizeof(*pad));
|
||||
if (new != NULL) {
|
||||
// Copy current pad in the *next pointer
|
||||
|
@ -55,16 +45,59 @@ void* Scratchpad_Reserve(Scratchpad* pad, size_t size)
|
|||
// Initialize new pad
|
||||
if (Scratchpad_Create(pad, new_capacity, pad->allocator)) {
|
||||
Allocator_Free(pad->allocator, new, sizeof(*new));
|
||||
return EXIT_FAILURE;
|
||||
} else {
|
||||
reserve = Scratchpad_Reserve(pad, size);
|
||||
pad->next = new;
|
||||
}
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
void* Scratchpad_Reserve(Scratchpad* pad, size_t size)
|
||||
{
|
||||
if (pad == NULL) return NULL;
|
||||
|
||||
Scratchpad* current = pad;
|
||||
void* reserve = NULL;
|
||||
while (reserve == NULL && current != NULL) {
|
||||
reserve = _Scratchpad_ReserveHere(current, size);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if (reserve == NULL) {
|
||||
if (_Scratchpad_CreateEmbedded(pad, size)) return NULL;
|
||||
reserve = _Scratchpad_ReserveHere(pad, size);
|
||||
}
|
||||
|
||||
return reserve;
|
||||
}
|
||||
|
||||
void* Scratchpad_ReserveAligned(Scratchpad* pad, size_t amount, size_t alignment)
|
||||
{
|
||||
if (pad == NULL) return NULL;
|
||||
|
||||
if (alignment == 1) return Scratchpad_Reserve(pad, amount);
|
||||
|
||||
Scratchpad* root = pad;
|
||||
do {
|
||||
uintptr_t pointer_value = (uintptr_t) pad->memory;
|
||||
uintptr_t alignment_padding = alignment - (pointer_value % alignment);
|
||||
void* memory = _Scratchpad_ReserveHere(pad, alignment_padding + amount);
|
||||
if (memory != NULL) return memory;
|
||||
|
||||
pad = pad->next;
|
||||
|
||||
} while (pad != NULL);
|
||||
|
||||
|
||||
if (_Scratchpad_CreateEmbedded(root, amount)) return NULL;
|
||||
|
||||
return _Scratchpad_ReserveHere(root, amount);
|
||||
}
|
||||
|
||||
int Scratchpad_Reclaim(Scratchpad* pad, void* pointer, size_t length)
|
||||
{
|
||||
if (pad == NULL) return EDESTADDRREQ;
|
||||
|
|
|
@ -21,6 +21,7 @@ typedef struct Scratchpad_s {
|
|||
int Scratchpad_Create(Scratchpad* target, size_t capacity, allocator_t* allocator);
|
||||
|
||||
void* Scratchpad_Reserve(Scratchpad* pad, size_t size);
|
||||
void* Scratchpad_ReserveAligned(Scratchpad* pad, size_t amount, size_t alignment);
|
||||
int Scratchpad_Reclaim(Scratchpad* pad, void* pointer, size_t length);
|
||||
void Scratchpad_Reset(Scratchpad* pad);
|
||||
|
||||
|
|
Loading…
Reference in a new issue