Compare commits

...

3 Commits

Author SHA1 Message Date
Nils O. Selåsdal 85713d25c1 Add pool allocator 2026-04-06 22:20:49 +02:00
Nils O. Selåsdal 7cf73fe561 Fix sarena comments 2026-04-06 22:20:32 +02:00
Nils O. Selåsdal 3e4826cf36 Add simple static arena allocator 2026-04-06 15:02:34 +02:00
4 changed files with 326 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
#ifndef UC_SARENA_H_
#define UC_SARENA_H_
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @file
* Static arena allocator.
*
* An UCSArena allocates memory from a provided buffer.
*
* The backing buffer should be suitable aligned to the types
* that will be allocated from it. Use .e.g
* @code
* _Alignas(16) char mem[1024];
* @endcode
* as the backing buffer.
*
* Once allocated, a piece of memory obtained from the
* allocator cannot be free'd individually, only the entire SArena can
* be free'd/reset
*/
typedef struct UCSArena UCSArena;
struct UCSArena {
char *start; // start of user buffer
char *curr; // next alloc point
char *end; // one past end of user buffer
};
/** Initialize a UCSArena with predefined backing array.
*
* @data must be an array type
* Use as
* @code
* _Alignas(16) char v[128];
* UCSArena a = UC_BV_STATIC_INITIALIZER(v);
* @endcode
*/
#define UC_SAR_STATIC_INITIALIZER(data)\
UC_SAR_INITIALIZER((data), sizeof(data))
/** Initializer for UCSArena with predefined memory
* Use as
* @code
* char v[128];
* UCSArena a = UC_BV_STATIC_INITIALIZER(v, sizeof v);
* @endcode
* or
* @code
* char *m = malloc(128);
* UCSArena a = UC_BV_INIT(m, 128);
* @endcode
*/
#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,
* or at global scope where the backing array will have static storage duration
*
* Use as:
* @code
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
* struct foo *foo = uc_sar_alloc(&my_allocator);
* if (foo == NULL) {
* // out of memory
* }
* @endcode
*
* @note, the backing buffer has alignment of 16. Use manual init if a different alignment
* is needed.
* @param var_name Variable name for the allocator
* @param size_ byte size of the backing array
*/
#define UC_SAR_ALLOC_DEF(var_name, size_) \
struct {\
_Alignas(16) char memory[size_]; \
} var_name ## _memory;\
UCSArena var_name = UC_SAR_STATIC_INITIALIZER(var_name ## _memory.memory)
/** Allocate space for @type_ , returned memory is aligned to requirements
* of @type.
*
* Use as:
* @code
* UC_SAR_ALLOC_DEF(my_allocator, 1024)
* struct foo *foo = UC_SAR_ALLOC_ALIGNED(&my_allocator, struct foo);
* // Array types can be used too:
* 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_))
//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
void *uc_sar_alloc(UCSArena *a, 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 *a, size_t sz, unsigned int align) [[gnu::alloc_align(2)]];
static inline void uc_sar_reset(UCSArena *a)
{
a->curr = a->start;
}
#ifdef __cplusplus
}
#endif
#endif
+60
View File
@@ -0,0 +1,60 @@
#ifndef UC_SPOOL_H_
#define UC_SPOOL_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @file
* Static pool allocator.
*
* An UCPool allocates fixed size memory chunks from a provided buffer,
* and memory allocations can be free'd in any order.
*
* The backing array should be suitable aligned to the types
* that will be allocated from it. Use .e.g
* @code
* _Alignas(16) char mem[1024];
* @endcode
* as the backing buffer.
*
*/
typedef struct UCPool UCPool;
struct UCPool {
unsigned char *free_list; // intrusive free list
size_t block_size; // aligned size of each block
size_t capacity; // number of blocks
size_t align; // alignment
unsigned char *start; // start of user buffer
unsigned char *end; // one past end of user buffer
};
/** Initialize the pool.
*
* Block size will be rounded up by alignment, or to size of a pointer
*
* @param buffer backing buffer for the pool
* @param buffer_size size of @buffer
* @param block_size size of each allocation.
* @param alignment alignment of each block (must be power of 2)
*/
void uc_pool_init(UCPool *pool,
void *buffer,
size_t buffer_size,
size_t block_size,
size_t alignment);
// Allocate a block from the pool. Returns NULL if no more space
void *uc_pool_alloc(UCPool *pool);
// Free @p . Returns memory to the pool
void uc_pool_free(UCPool *pool, void *p);
void uc_pool_reset(UCPool *pool);
#ifdef __cplusplus
}
#endif
#endif
+30
View File
@@ -0,0 +1,30 @@
#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;
}
+111
View File
@@ -0,0 +1,111 @@
#include <assert.h>
#include "ucore/spool.h"
#include "ucore/utils.h"
#include <string.h>
void uc_pool_init(UCPool *pool, void *buffer, size_t buffer_size, size_t block_size, size_t align)
{
assert(align > 0);
assert((align & (align - 1)) == 0); // power of 2
// enforce minimum for storing free list pointers
if (align < _Alignof(unsigned char *)) {
align = _Alignof(unsigned char *);
}
if (block_size < sizeof(unsigned char *)) {
block_size = sizeof(unsigned char *);
}
// ensure initial alignment
size_t stride = UC_ALIGN(block_size, align);
// align first usable block
uintptr_t start_addr = (uintptr_t)buffer;
uintptr_t first_addr = UC_ALIGN(start_addr, align);
size_t padding = first_addr - start_addr;
if (padding + stride > buffer_size) {
pool->capacity = 0;
pool->free_list = NULL;
} else {
size_t usable = buffer_size - padding;
pool->capacity = usable / stride;
// build intrusive free list
unsigned char *p = (unsigned char *)first_addr;
for (size_t i = 0; i < pool->capacity - 1; ++i) {
*(unsigned char **)p = p + stride;
p += stride;
}
*(unsigned char **)p = NULL; // last element
pool->free_list = (unsigned char *)first_addr;
}
pool->block_size = stride;
pool->align = align;
pool->start = (unsigned char *)buffer;
pool->end = (unsigned char *)buffer + buffer_size;
}
void *uc_pool_alloc(UCPool *pool)
{
if (!pool->free_list) {
return NULL;
}
unsigned char *block = pool->free_list;
pool->free_list = *(unsigned char **)block;
#ifdef DEBUG
memset(block, 0xCB, pool->block_size);
#endif
return block;
}
#include <stdio.h>
void uc_pool_free(UCPool *pool, void *p)
{
if (p == NULL) {
return;
}
#ifdef DEBUG
assert((unsigned char *)p >= pool->start && (unsigned char*)p < pool->end);
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
// double-free detection
for (unsigned char *f = pool->free_list; f; f = *(unsigned char**)f) {
if (f == p) {
assert(0 && "Double free detected in pool!");
return;
}
}
memset(p, 0xDD, pool->block_size);
#endif
// When not in use, store pointer to next free element inline in the block
*(unsigned char **)p = pool->free_list;
pool->free_list = p;
}
void uc_pool_reset(UCPool *pool)
{
if (pool->capacity == 0)
return;
uintptr_t start_addr = (uintptr_t)pool->start;
uintptr_t first_addr = UC_ALIGN(start_addr, pool->align);
unsigned char *p = (unsigned char *)first_addr;
// build intrusive free list
for (size_t i = 0; i < pool->capacity - 1; ++i) {
*(unsigned char **)p = p + pool->block_size;
p += pool->block_size;
}
*(unsigned char **)p = NULL; // last element
pool->free_list = (unsigned char *)first_addr;
}