Fix sarena comments
This commit is contained in:
+25
-16
@@ -10,14 +10,14 @@ extern "C" {
|
|||||||
/** @file
|
/** @file
|
||||||
* Static arena allocator.
|
* Static arena allocator.
|
||||||
*
|
*
|
||||||
* An UCSArena allocates memory from pre-existing array
|
* An UCSArena allocates memory from a provided buffer.
|
||||||
*
|
*
|
||||||
* The backing array should be suitable aligned to the types
|
* The backing buffer should be suitable aligned to the types
|
||||||
* that will be allocated from it. Use .e.g
|
* that will be allocated from it. Use .e.g
|
||||||
* @code
|
* @code
|
||||||
* _Alignas(16) char mem[1024];
|
* _Alignas(16) char mem[1024];
|
||||||
* @endcode
|
* @endcode
|
||||||
* as the backing array.
|
* as the backing buffer.
|
||||||
*
|
*
|
||||||
* 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
|
||||||
@@ -31,29 +31,30 @@ struct UCSArena {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** 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
|
||||||
* Use as
|
* Use as
|
||||||
* @code
|
* @code
|
||||||
* _Alignas(16) char v[128];
|
* _Alignas(16) char v[128];
|
||||||
* SArana a = UC_BV_STATIC_INIT(v);
|
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_STATIC_INIT(data)\
|
#define UC_SAR_STATIC_INITIALIZER(data)\
|
||||||
UC_SAR_INIT((data), sizeof(data))
|
UC_SAR_INITIALIZER((data), sizeof(data))
|
||||||
|
|
||||||
/** Initialize a UCSArena with predefined memory
|
/** Initializer for UCSArena with predefined memory
|
||||||
* Use as
|
* Use as
|
||||||
* @code
|
* @code
|
||||||
* char v[128];
|
* char v[128];
|
||||||
* SArana a = UC_BV_STATIC_INIT(v, sizeof v);
|
* UCSArena a = UC_BV_STATIC_INITIALIZER(v, sizeof v);
|
||||||
* @endcode
|
* @endcode
|
||||||
* or
|
* or
|
||||||
* @code
|
* @code
|
||||||
* char *m = malloc(128);
|
* char *m = malloc(128);
|
||||||
* SArana a = UC_BV_INIT(m, 128);
|
* UCSArena a = UC_BV_INIT(m, 128);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define UC_SAR_INIT(data, sz)\
|
#define UC_SAR_INITIALIZER(data, sz)\
|
||||||
{\
|
{\
|
||||||
(data),\
|
(data),\
|
||||||
(data),\
|
(data),\
|
||||||
@@ -61,7 +62,7 @@ struct UCSArena {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 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 (stack) storage,
|
* The macro can be used at local scope to allocate memory automatic on the stack,
|
||||||
* or at global scope where the backing array will have static storage duration
|
* or at global scope where the backing array will have static storage duration
|
||||||
*
|
*
|
||||||
* Use as:
|
* Use as:
|
||||||
@@ -73,7 +74,7 @@ struct UCSArena {
|
|||||||
* }
|
* }
|
||||||
* @endcode
|
* @endcode
|
||||||
*
|
*
|
||||||
* @note, the backing store has alignment of 16. Use manual init if a different alignment
|
* @note, the backing buffer has alignment of 16. Use manual init if a different alignment
|
||||||
* 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
|
||||||
@@ -82,7 +83,7 @@ struct UCSArena {
|
|||||||
struct {\
|
struct {\
|
||||||
_Alignas(16) char memory[size_]; \
|
_Alignas(16) char memory[size_]; \
|
||||||
} var_name ## _memory;\
|
} var_name ## _memory;\
|
||||||
UCSArena var_name = UC_SAR_STATIC_INIT(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.
|
||||||
@@ -91,19 +92,27 @@ struct UCSArena {
|
|||||||
* @code
|
* @code
|
||||||
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
|
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
|
||||||
* struct foo *foo = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct foo);
|
* struct foo *foo = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct foo);
|
||||||
* // Array types can be used too, room for null terminated string of lengh 10:
|
* // Array types can be used too:
|
||||||
* char *str = UC_SAR_ALLOC_ALIGNED(&my_allocator, char[11]);
|
* 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
|
||||||
|
static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
|
||||||
|
{
|
||||||
|
a->start = a->curr = mem;
|
||||||
|
a->end = a->start + sz;
|
||||||
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
|
|
||||||
// Allocate @sz bytes from the UCSArena, returned pointer is aligned to @align
|
// Allocate @sz bytes from the UCSArena, returned pointer is aligned to @align
|
||||||
// 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);
|
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)
|
||||||
{
|
{
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ void *uc_sar_alloc_aligned(UCSArena *a, size_t sz, unsigned int align)
|
|||||||
|
|
||||||
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);
|
||||||
char *start = (char *)aligned;
|
unsigned char *start = (unsigned char *)aligned;
|
||||||
|
|
||||||
if (start + sz > a->end) {
|
if (start + sz > a->end) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user