|
|
@ -0,0 +1,44 @@ |
|
|
|
/* |
|
|
|
* Allocates space within a tree allocator for performing |
|
|
|
* watermark allocations. |
|
|
|
*/ |
|
|
|
WatermarkAlloc *new_watermark_allocator(TreeAlloc *ta, int size) { |
|
|
|
// TODO: Allocate space correctly when that is ironed out. |
|
|
|
// This will just SIGSEGV 100% of the time for now. |
|
|
|
WatermarkAlloc *wma = NULL; |
|
|
|
wma->type = RT_WATERMARK; |
|
|
|
wma->parent = ta; |
|
|
|
wma->left = wma->right = NULL; // TODO: How should these be set? |
|
|
|
wma->size = size; |
|
|
|
wma->num_allocs = 0; |
|
|
|
wma->next_alloc = wma + sizeof(wma); |
|
|
|
return wma; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Attempt to allocate space within a watermark region. |
|
|
|
* Returns the null pointer if the allocation cannot fit. |
|
|
|
*/ |
|
|
|
void *watermark_allocate(WatermarkAlloc *wma, int size) { |
|
|
|
int space_left = wma + wma->size - wma->next_alloc; |
|
|
|
if (space_left < size) |
|
|
|
return NULL; |
|
|
|
|
|
|
|
void *allocation = wma->next_alloc; |
|
|
|
wma->next_alloc += size; |
|
|
|
wma->num_allocs += 1; |
|
|
|
|
|
|
|
return allocation; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* "Frees" space inside the watermark region. Since this |
|
|
|
* is watermark, however, this doesn't actually do anything |
|
|
|
* besides decrement the ref-count. It returns the new |
|
|
|
* ref-count so that the parent allocator can clean up the |
|
|
|
* space properly. |
|
|
|
*/ |
|
|
|
bool watermark_free(WatermarkAlloc *wma) { |
|
|
|
wma->num_allocs -= 1; |
|
|
|
return wma->num_allocs; |
|
|
|
} |