Add simple static arena allocator
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#include <assert.h>
|
||||
#include "ucore/sarena.h"
|
||||
|
||||
void *uc_sar_alloc(UCSArena *a, size_t sz)
|
||||
{
|
||||
if (a->curr + sz > a->end) {
|
||||
return NULL;
|
||||
}
|
||||
void *start = a->curr;
|
||||
a->curr += sz;
|
||||
return start;
|
||||
}
|
||||
|
||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
||||
{
|
||||
assert(align != 0);
|
||||
assert((align & (align - 1)) == 0); //power of 2
|
||||
|
||||
uintptr_t curr = (uintptr_t)a->curr;
|
||||
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
||||
char *start = (char *)aligned;
|
||||
|
||||
if (start + sz > a->end) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
a->curr = start + sz;
|
||||
|
||||
return start;
|
||||
}
|
||||
Reference in New Issue
Block a user