31 lines
658 B
C
31 lines
658 B
C
#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);
|
|
unsigned char *start = (unsigned char *)aligned;
|
|
|
|
if (start + sz > a->end) {
|
|
return NULL;
|
|
}
|
|
|
|
a->curr = start + sz;
|
|
|
|
return start;
|
|
}
|