Compare commits

...

3 Commits

Author SHA1 Message Date
Nils O. Selåsdal fc418f6163 sarena, add temp allocations 2026-04-19 04:23:52 +02:00
Nils O. Selåsdal aba9cba602 Reformat & cleanup 2026-04-19 04:23:12 +02:00
Nils O. Selåsdal 64a5b20941 Comment update 2026-04-15 19:40:11 +02:00
4 changed files with 108 additions and 100 deletions
+15 -11
View File
@@ -28,8 +28,14 @@ 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,
@@ -96,8 +96,7 @@ 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)
@@ -106,6 +105,12 @@ static inline void uc_sar_init(UCSArena *a, void *mem, size_t sz)
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;
+2 -6
View File
@@ -10,7 +10,7 @@ extern "C" {
typedef union UCPoolBlock UCPoolBlock; typedef union UCPoolBlock UCPoolBlock;
union UCPoolBlock { union UCPoolBlock {
UCPoolBlock *next; // next free block (when not allocated) UCPoolBlock *next; // next free block (when not allocated)
unsigned char data[]; // data when allocated unsigned char data[0]; // data when allocated
}; };
/** @file /** @file
* Static pool allocator. * Static pool allocator.
@@ -45,11 +45,7 @@ struct UCPool {
* @param block_size size of each allocation. * @param block_size size of each allocation.
* @param alignment alignment of each block (must be power of 2) * @param alignment alignment of each block (must be power of 2)
*/ */
void uc_pool_init(UCPool *pool, void uc_pool_init(UCPool *pool, void *buffer, size_t buffer_size, size_t block_size, size_t alignment);
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 // Allocate a block from the pool. Returns NULL if no more space
[[gnu::assume_aligned(_Alignof(UCPoolBlock))]] [[gnu::assume_aligned(_Alignof(UCPoolBlock))]]
+15
View File
@@ -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--;
}
+35 -42
View File
@@ -8,37 +8,28 @@
* - Allocated pointers are aligned to 16 bytes. * - Allocated pointers are aligned to 16 bytes.
* - Backing buffer must be aligned to 16 bytes start * - Backing buffer must be aligned to 16 bytes start
* - 4Gb - 16 is max for the allocator backing buffer. * - 4Gb - 16 is max for the allocator backing buffer.
* - An allocation "wastes" 16 bytes for the block header
* - Allocation of > 0 bytes returns a size aligned up to 16 bytes * - Allocation of > 0 bytes returns a size aligned up to 16 bytes
* - Allocation of 0 bytes returns a valid pointer with 0 usable space * - Allocation of 0 bytes returns a valid pointer with 0 usable space
*
* The allocator maintains a doubly linked list of blocks, * The allocator maintains a doubly linked list of blocks,
* when blocks are free'd, adjacent blocks are merged to keep * when blocks are free'd, adjacent blocks are merged to keep
* fragmentation to a minimum * fragmentation to a minimum
*/ */
#define ALIGNMENT 16
#define ALIGN_UP(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define ALIGN_DOWN(n) ((n) & ~(ALIGNMENT - 1))
#define NULL_BLOCK (0xffffffff)
#define BLOCK_FREE ((uint32_t)1)
typedef struct BlockHeader { typedef struct BlockHeader {
uint32_t next; // offset from base uint32_t next; // offset from base
uint32_t prev; // offset from base uint32_t prev; // offset from base
uint32_t size_and_flags; // size | free_bit uint32_t size_and_flags; // size | free_bit
// Padding to ensure we always return 16 byte aligned pointers. // Padding to ensure we always return 16 byte aligned pointers.
// Remove padding & set ALIGNMENT to 8 to get 8 byte alignmet
// (for 64 bit systems, 32 bit systems might need other adjustments)
uint8_t pad[4]; uint8_t pad[4];
} BlockHeader; } BlockHeader;
#define ALIGNMENT sizeof(BlockHeader)
#define ALIGN_UP(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define ALIGN_DOWN(n) ((n) & ~(ALIGNMENT - 1))
_Static_assert(sizeof(BlockHeader) % ALIGNMENT == 0, #define NULL_BLOCK (0xffffffff)
"BlockHeader size must be a multiple of ALIGNMENT"); #define BLOCK_FREE ((uint32_t)1)
_Static_assert(ALIGNMENT == 16, "sizeof(BlockHeader) is not 16");
typedef struct { typedef struct {
uint8_t* base; // backing store uint8_t* base; // backing store
@@ -49,12 +40,12 @@ typedef struct {
/* --- Header helpers --- */ /* --- Header helpers --- */
static inline uint32_t block_size(BlockHeader* b) static inline uint32_t block_size(const BlockHeader* b)
{ {
return b->size_and_flags & ~BLOCK_FREE; return b->size_and_flags & ~BLOCK_FREE;
} }
static inline int block_is_free(BlockHeader* b) static inline int block_is_free(const BlockHeader* b)
{ {
return (b->size_and_flags & BLOCK_FREE) != 0; return (b->size_and_flags & BLOCK_FREE) != 0;
} }
@@ -69,14 +60,13 @@ static inline void block_set_used(BlockHeader* b)
b->size_and_flags &= ~BLOCK_FREE; b->size_and_flags &= ~BLOCK_FREE;
} }
static inline void block_set_size(BlockHeader* b, uint32_t size) static inline void block_set_size(BlockHeader* b, uint32_t size)
{ {
uint32_t flags = b->size_and_flags & BLOCK_FREE; uint32_t flags = b->size_and_flags & BLOCK_FREE;
b->size_and_flags = size | flags; b->size_and_flags = size | flags;
} }
static inline BlockHeader * bh_from_offset(const SmallAllocator *a, uint32_t off) static inline BlockHeader* block_from_offset(const SmallAllocator* a, uint32_t off)
{ {
if (off == NULL_BLOCK) { if (off == NULL_BLOCK) {
return NULL; return NULL;
@@ -115,17 +105,15 @@ void small_allocator_init(SmallAllocator* a, void* backing_store, uint32_t size)
a->last_alloc = first; a->last_alloc = first;
} }
static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size) static void maybe_split_block(SmallAllocator* a, BlockHeader* block, uint32_t size)
{ {
size = ALIGN_UP(size); // assumes size is already aligned
uint32_t bsize = block_size(block); uint32_t bsize = block_size(block);
if (bsize < size + sizeof(BlockHeader) + ALIGNMENT) if (bsize < size + sizeof(BlockHeader) + ALIGNMENT) return;
return;
uint8_t* base = (uint8_t*)block; uint8_t* base = (uint8_t*)block;
BlockHeader* new_block = BlockHeader* new_block = (BlockHeader*)(base + sizeof(BlockHeader) + size);
(BlockHeader*)(base + sizeof(BlockHeader) + size);
uint32_t new_size = ALIGN_UP(bsize - size - sizeof(BlockHeader)); uint32_t new_size = ALIGN_UP(bsize - size - sizeof(BlockHeader));
@@ -136,10 +124,9 @@ static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size)
new_block->next = block->next; new_block->next = block->next;
new_block->prev = bh_offset(a, block); new_block->prev = bh_offset(a, block);
BlockHeader *next = bh_from_offset(a, new_block->next); BlockHeader* next = block_from_offset(a, new_block->next);
if (next) { if (next) {
next->prev = bh_offset(a, new_block); next->prev = bh_offset(a, new_block);
//block->next->prev = new_block;
} }
block->next = bh_offset(a, new_block); block->next = bh_offset(a, new_block);
@@ -147,19 +134,15 @@ static void split_block(SmallAllocator *a, BlockHeader* block, uint32_t size)
static void merge_with_next(SmallAllocator* a, BlockHeader* block) static void merge_with_next(SmallAllocator* a, BlockHeader* block)
{ {
BlockHeader* next = bh_from_offset(a, block->next); BlockHeader* next = block_from_offset(a, block->next);
if (!next || !block_is_free(next)) if (next == NULL || !block_is_free(next)) return;
return;
uint32_t new_size = ALIGN_UP(block_size(block) + uint32_t new_size = ALIGN_UP(block_size(block) + sizeof(BlockHeader) + block_size(next));
sizeof(BlockHeader) +
block_size(next));
block_set_size(block, new_size); block_set_size(block, new_size);
block->next = next->next; block->next = next->next;
BlockHeader *nextnext = bh_from_offset(a, next->next); BlockHeader* nextnext = block_from_offset(a, next->next);
if (nextnext) if (nextnext) nextnext->prev = bh_offset(a, block);
nextnext->prev = bh_offset(a, block);
} }
void* small_alloc(SmallAllocator* a, uint32_t size) void* small_alloc(SmallAllocator* a, uint32_t size)
@@ -171,12 +154,12 @@ void* small_alloc(SmallAllocator* a, uint32_t size)
do { do {
if (block_is_free(curr) && block_size(curr) >= size) { if (block_is_free(curr) && block_size(curr) >= size) {
split_block(a, curr, size); maybe_split_block(a, curr, size);
block_set_used(curr); block_set_used(curr);
a->last_alloc = curr; a->last_alloc = curr;
return (uint8_t*)curr + sizeof(BlockHeader); return (uint8_t*)curr + sizeof(BlockHeader);
} }
curr = bh_from_offset(a, curr->next) ? bh_from_offset(a, curr->next) : a->heap_start; curr = block_from_offset(a, curr->next) ? block_from_offset(a, curr->next) : a->heap_start;
} while (curr != start); } while (curr != start);
return NULL; return NULL;
@@ -199,7 +182,7 @@ void small_free(SmallAllocator* a, void* ptr)
block_set_free(block); block_set_free(block);
merge_with_next(a, block); merge_with_next(a, block);
BlockHeader* prev = bh_from_offset(a, block->prev); BlockHeader* prev = block_from_offset(a, block->prev);
if (prev != NULL && block_is_free(prev)) { if (prev != NULL && block_is_free(prev)) {
merge_with_next(a, prev); merge_with_next(a, prev);
@@ -217,13 +200,13 @@ void small_free(SmallAllocator* a, void* ptr)
void debug_alloc(const SmallAllocator* a) void debug_alloc(const SmallAllocator* a)
{ {
puts("--debug start--"); puts("--debug start--");
for (BlockHeader *b = a->heap_start ; b ; b = bh_from_offset(a, b->next)) { for (BlockHeader* b = a->heap_start; b; b = block_from_offset(a, b->next)) {
printf("Block size %u free %d prev %u next %u \n", block_size(b), block_is_free(b), b->prev, b->next); printf("Block size %u free %d prev %u next %u \n", block_size(b), block_is_free(b), b->prev, b->next);
} }
puts("--debug end--"); puts("--debug end--");
} }
char buff[1023*1023]; [[gnu::aligned(16)]] char buff[1023 * 1023];
int main(void) int main(void)
{ {
printf("bh size %zu\n", sizeof(BlockHeader)); printf("bh size %zu\n", sizeof(BlockHeader));
@@ -232,9 +215,19 @@ int main(void)
char* ptrs[SZ]; char* ptrs[SZ];
small_allocator_init(&a, buff, sizeof buff); small_allocator_init(&a, buff, sizeof buff);
ptrs[0] = small_alloc(&a, 16);
debug_alloc(&a); debug_alloc(&a);
ptrs[0] = small_alloc(&a, 32);
ptrs[1] = small_alloc(&a, 80);
ptrs[2] = small_alloc(&a, 32);
small_free(&a, ptrs[1]);
ptrs[3] = small_alloc(&a, 12);
ptrs[4] = small_alloc(&a, 12);
debug_alloc(&a);
small_free(&a, ptrs[0]); small_free(&a, ptrs[0]);
small_free(&a, ptrs[2]);
small_free(&a, ptrs[3]);
small_free(&a, ptrs[4]);
for (int i = 0; i < SZ; i++) { for (int i = 0; i < SZ; i++) {
ptrs[i] = small_alloc(&a, rand() % 256 + 1); ptrs[i] = small_alloc(&a, rand() % 256 + 1);