You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

46 lines
1.3 KiB

#include <stddef.h>
#include "allocator_internal.h"
/*
* Attempt to allocate space within a watermark region. It will try to
* allocate more space if
*/
void *alloc_fixed(struct Arena *arena, uintptr_t size, uintptr_t align)
{
// Large allocations will never fit inside this allocator.
if (size > MIN_NEW_MEM_SIZE) return NULL;
WatermarkAlloc *wma = arena->head_watermark;
int space_left;
if (!wma) {
// No watermark allocators are currently active, so a new one
// needs to be created to act as the head.
wma = alloc(arena, MIN_NEW_MEM_SIZE + sizeof(WatermarkAlloc), align);
arena->head_watermark = wma;
wma->before = wma->after = NULL;
} else {
// Scan through the current allocators until one with enough space is
// found or the end of the list is reached. In the latter case, alloc
// a new allocator.
while(wma) {
if(wma->size <= size) {
// There is enough room for the allocation in this allocator,
// so use it.
break;
} else if (!wma->after) {
wma->after = alloc(arena, MIN_NEW_MEM_SIZE + sizeof(WatermarkAlloc), align);
wma->after->before = (TreeAlloc *) wma;
wma = (WatermarkAlloc *) wma->after;
break;
}
wma = (WatermarkAlloc *) wma->after;
}
}
void *allocation = wma->next_alloc;
wma->next_alloc += size;
wma->num_allocs += 1;
return allocation;
}