Improve sarena.h
This commit is contained in:
+17
-9
@@ -40,7 +40,7 @@ struct UCSTmpAlloc {
|
||||
* Use as
|
||||
* @code
|
||||
* _Alignas(16) char v[128];
|
||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
||||
* UCSArena a = UC_SAR_STATIC_INITIALIZER(v);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_STATIC_INITIALIZER(data) UC_SAR_INITIALIZER((data), sizeof(data))
|
||||
@@ -49,15 +49,20 @@ struct UCSTmpAlloc {
|
||||
* Use as
|
||||
* @code
|
||||
* char v[128];
|
||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v, sizeof v);
|
||||
* UCSArena a = UC_SAR_INITIALIZER(v, sizeof v);
|
||||
* @endcode
|
||||
* or
|
||||
* @code
|
||||
* char *m = malloc(128);
|
||||
* UCSArena a = UC_BV_INIT(m, 128);
|
||||
* UCSArena a = UC_SAR_INITIALIZER(m, 128);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_INITIALIZER(data, sz) {(data), (data), (data) + sz}
|
||||
#define UC_SAR_INITIALIZER(data, sz) {\
|
||||
.start = (data),\
|
||||
.curr = (data),\
|
||||
.end=(data) + sz,\
|
||||
.tmp_allocs = 0\
|
||||
}
|
||||
|
||||
/** Initialize a UCSArena with an array as the backing memory to allocate from.
|
||||
* The macro can be used at local scope to allocate memory automatic on the stack,
|
||||
@@ -111,7 +116,7 @@ static inline UCSTmpAlloc uc_sar_tmp_begin(UCSArena *arena)
|
||||
return state;
|
||||
}
|
||||
|
||||
// end temporary allocations, releaseing all allocations since the most recent uc_sar_tmp_begin
|
||||
// end temporary allocations, releasing all allocations since the most recent uc_sar_tmp_begin
|
||||
static inline void uc_sar_tmp_end(const UCSTmpAlloc *state)
|
||||
{
|
||||
UCSArena *arena = state->arena;
|
||||
@@ -121,9 +126,9 @@ static inline void uc_sar_tmp_end(const UCSTmpAlloc *state)
|
||||
}
|
||||
|
||||
// Allocate @sz bytes from the UCSArena. Returns NULL if not enough space
|
||||
void *uc_sar_alloc(UCSArena *arena, size_t sz)
|
||||
static inline void *uc_sar_alloc(UCSArena *arena, size_t sz)
|
||||
{
|
||||
if (arena->curr + sz > arena->end) {
|
||||
if (sz > (arena->curr - arena->end)) {
|
||||
return NULL;
|
||||
}
|
||||
void *start = arena->curr;
|
||||
@@ -133,7 +138,7 @@ void *uc_sar_alloc(UCSArena *arena, size_t sz)
|
||||
|
||||
// Allocate @sz bytes from the UCSArena, returned pointer is aligned to @align
|
||||
// Returns NULL if not enough space
|
||||
void *uc_sar_alloc_aligned(UCSArena *arena, size_t sz, unsigned int align) [[gnu::alloc_align(3)]]
|
||||
static inline void *uc_sar_alloc_aligned(UCSArena *arena, size_t sz, unsigned int align) [[gnu::alloc_align(3)]]
|
||||
{
|
||||
assert(align != 0);
|
||||
assert((align & (align - 1)) == 0); // power of 2
|
||||
@@ -142,7 +147,8 @@ void *uc_sar_alloc_aligned(UCSArena *arena, size_t sz, unsigned int align) [[gnu
|
||||
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
||||
unsigned char *start = (unsigned char *)aligned;
|
||||
|
||||
if (start + sz > arena->end) {
|
||||
if (sz > (arena->curr - arena->end)) {
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -151,6 +157,8 @@ void *uc_sar_alloc_aligned(UCSArena *arena, size_t sz, unsigned int align) [[gnu
|
||||
return start;
|
||||
}
|
||||
|
||||
// reset arena, making all memory available for allocation
|
||||
// also invalidates any UCSTmpAlloc instances.
|
||||
static inline void uc_sar_reset(UCSArena *arena)
|
||||
{
|
||||
arena->curr = arena->start;
|
||||
|
||||
Reference in New Issue
Block a user