sarena, add temp allocations
This commit is contained in:
+21
-17
@@ -22,14 +22,20 @@ extern "C" {
|
|||||||
* Once allocated, a piece of memory obtained from the
|
* Once allocated, a piece of memory obtained from the
|
||||||
* allocator cannot be free'd individually, only the entire SArena can
|
* allocator cannot be free'd individually, only the entire SArena can
|
||||||
* be free'd/reset
|
* be free'd/reset
|
||||||
*/
|
*/
|
||||||
typedef struct UCSArena UCSArena;
|
typedef struct UCSArena UCSArena;
|
||||||
struct UCSArena {
|
struct UCSArena {
|
||||||
unsigned char *start; // start of user buffer
|
unsigned char *start; // start of user buffer
|
||||||
unsigned char *curr; // next alloc point
|
unsigned char *curr; // next alloc point
|
||||||
unsigned char *end; // one past end of user buffer
|
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.
|
/** Initialize a UCSArena with predefined backing array.
|
||||||
*
|
*
|
||||||
* @data must be an array type
|
* @data must be an array type
|
||||||
@@ -39,8 +45,7 @@ struct UCSArena {
|
|||||||
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_STATIC_INITIALIZER(data)\
|
#define UC_SAR_STATIC_INITIALIZER(data) UC_SAR_INITIALIZER((data), sizeof(data))
|
||||||
UC_SAR_INITIALIZER((data), sizeof(data))
|
|
||||||
|
|
||||||
/** Initializer for UCSArena with predefined memory
|
/** Initializer for UCSArena with predefined memory
|
||||||
* Use as
|
* Use as
|
||||||
@@ -54,12 +59,7 @@ struct UCSArena {
|
|||||||
* UCSArena a = UC_BV_INIT(m, 128);
|
* UCSArena a = UC_BV_INIT(m, 128);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_INITIALIZER(data, sz)\
|
#define UC_SAR_INITIALIZER(data, sz) {(data), (data), (data) + sz}
|
||||||
{\
|
|
||||||
(data),\
|
|
||||||
(data),\
|
|
||||||
(data) + sz\
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Initialize a UCSArena with an array as the backing memory to allocate from.
|
/** 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,
|
* The macro can be used at local scope to allocate memory automatic on the stack,
|
||||||
@@ -78,12 +78,12 @@ struct UCSArena {
|
|||||||
* is needed.
|
* is needed.
|
||||||
* @param var_name Variable name for the allocator
|
* @param var_name Variable name for the allocator
|
||||||
* @param size_ byte size of the backing array
|
* @param size_ byte size of the backing array
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_ALLOC_DEF(var_name, size_) \
|
#define UC_SAR_ALLOC_DEF(var_name, size_) \
|
||||||
struct {\
|
struct { \
|
||||||
_Alignas(16) char memory[size_]; \
|
_Alignas(16) char memory[size_]; \
|
||||||
} var_name ## _memory;\
|
} var_name##_memory; \
|
||||||
UCSArena var_name = UC_SAR_STATIC_INITIALIZER(var_name ## _memory.memory)
|
UCSArena var_name = UC_SAR_STATIC_INITIALIZER(var_name##_memory.memory)
|
||||||
|
|
||||||
/** Allocate space for @type_ , returned memory is aligned to requirements
|
/** Allocate space for @type_ , returned memory is aligned to requirements
|
||||||
* of @type.
|
* of @type.
|
||||||
@@ -96,16 +96,21 @@ struct UCSArena {
|
|||||||
* struct Foo *str = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct Foo[32]);
|
* struct Foo *str = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct Foo[32]);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_ALLOC_ALIGNED(allocator, type_) \
|
#define UC_SAR_ALLOC_ALIGNED(allocator, type_) uc_sar_alloc_aligned((allocator), sizeof(type_), _Alignof(type_))
|
||||||
uc_sar_alloc_aligned((allocator), sizeof (type_), _Alignof(type_))
|
|
||||||
|
|
||||||
//Initialize an UCSArena to allocate from @mem which is @sz bytes
|
// Initialize an UCSArena to allocate from @mem which is @sz bytes
|
||||||
static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
||||||
{
|
{
|
||||||
a->start = a->curr = mem;
|
a->start = a->curr = mem;
|
||||||
a->end = a->start + 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
|
// Allocate @sz bytes from the UCSArena. Returns NULL if not enough space
|
||||||
void *uc_sar_alloc(UCSArena *a, size_t sz);
|
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
|
// Returns NULL if not enough space
|
||||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align) [[gnu::alloc_align(2)]];
|
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)
|
static inline void uc_sar_reset(UCSArena *a)
|
||||||
{
|
{
|
||||||
a->curr = a->start;
|
a->curr = a->start;
|
||||||
|
|||||||
+16
-1
@@ -14,7 +14,7 @@ void *uc_sar_alloc(UCSArena *a, size_t sz)
|
|||||||
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
||||||
{
|
{
|
||||||
assert(align != 0);
|
assert(align != 0);
|
||||||
assert((align & (align - 1)) == 0); //power of 2
|
assert((align & (align - 1)) == 0); // power of 2
|
||||||
|
|
||||||
uintptr_t curr = (uintptr_t)a->curr;
|
uintptr_t curr = (uintptr_t)a->curr;
|
||||||
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
||||||
@@ -28,3 +28,18 @@ void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
|||||||
|
|
||||||
return start;
|
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