sarena, add temp allocations
This commit is contained in:
+15
-11
@@ -28,8 +28,14 @@ struct UCSArena {
|
||||
unsigned char *start; // start of user buffer
|
||||
unsigned char *curr; // next alloc point
|
||||
unsigned char *end; // one past end of user buffer
|
||||
int tmp_allocs; // no of uc_sar_tmp_begin calls
|
||||
};
|
||||
|
||||
typedef struct UCSTmpAlloc UCSTmpAlloc;
|
||||
struct UCSTmpAlloc {
|
||||
unsigned char *alloc_point;
|
||||
UCSArena *arena;
|
||||
};
|
||||
/** Initialize a UCSArena with predefined backing array.
|
||||
*
|
||||
* @data must be an array type
|
||||
@@ -39,8 +45,7 @@ struct UCSArena {
|
||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_STATIC_INITIALIZER(data)\
|
||||
UC_SAR_INITIALIZER((data), sizeof(data))
|
||||
#define UC_SAR_STATIC_INITIALIZER(data) UC_SAR_INITIALIZER((data), sizeof(data))
|
||||
|
||||
/** Initializer for UCSArena with predefined memory
|
||||
* Use as
|
||||
@@ -54,12 +59,7 @@ struct UCSArena {
|
||||
* UCSArena a = UC_BV_INIT(m, 128);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_INITIALIZER(data, sz)\
|
||||
{\
|
||||
(data),\
|
||||
(data),\
|
||||
(data) + sz\
|
||||
}
|
||||
#define UC_SAR_INITIALIZER(data, sz) {(data), (data), (data) + sz}
|
||||
|
||||
/** 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,
|
||||
@@ -96,8 +96,7 @@ struct UCSArena {
|
||||
* struct Foo *str = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct Foo[32]);
|
||||
* @endcode
|
||||
*/
|
||||
#define UC_SAR_ALLOC_ALIGNED(allocator, type_) \
|
||||
uc_sar_alloc_aligned((allocator), sizeof (type_), _Alignof(type_))
|
||||
#define UC_SAR_ALLOC_ALIGNED(allocator, type_) uc_sar_alloc_aligned((allocator), sizeof(type_), _Alignof(type_))
|
||||
|
||||
// Initialize an UCSArena to allocate from @mem which is @sz bytes
|
||||
static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
||||
@@ -106,6 +105,12 @@ static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
||||
a->end = a->start + sz;
|
||||
}
|
||||
|
||||
// begin temporary allocation. All allocations afterwards will be released in uc_sar_tmp_end
|
||||
UCSTmpAlloc uc_sar_tmp_begin(UCSArena *a);
|
||||
|
||||
// end temporary allocations, releaseing all allocations since the most recent uc_sar_tmp_begin
|
||||
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 *a, size_t sz);
|
||||
|
||||
@@ -113,7 +118,6 @@ void *uc_sar_alloc(UCSArena *a, size_t sz);
|
||||
// Returns NULL if not enough space
|
||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align) [[gnu::alloc_align(2)]];
|
||||
|
||||
|
||||
static inline void uc_sar_reset(UCSArena *a)
|
||||
{
|
||||
a->curr = a->start;
|
||||
|
||||
@@ -28,3 +28,18 @@ void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
UCSTmpAlloc uc_sar_tmp_begin(UCSArena *a)
|
||||
{
|
||||
UCSTmpAlloc state = {.alloc_point = a->curr, .arena = a};
|
||||
a->tmp_allocs++;
|
||||
return state;
|
||||
}
|
||||
|
||||
void uc_sar_tmp_end(const UCSTmpAlloc *state)
|
||||
{
|
||||
UCSArena *a = state->arena;
|
||||
assert(a->tmp_allocs > 0);
|
||||
a->curr = state->alloc_point;
|
||||
a->tmp_allocs--;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user