apool: track total&free elements

This commit is contained in:
2026-09-14 19:55:23 +02:00
parent 9ed137abcb
commit 076b152555
3 changed files with 83 additions and 51 deletions
+19 -9
View File
@@ -1,6 +1,10 @@
#pragma once
#include "core.h"
enum {
APoolFlag_AutoGrow = (1 << 0)
};
typedef U32 APoolFlags;
typedef union APoolElement APoolElement;
union APoolElement {
APoolElement *next; // offset from start to next free block (when not allocated)
@@ -11,16 +15,22 @@ struct APool {
APoolElement *free_list; // free list
U8 *start; // start of user buffer
Allocator *allocator;
U32 element_size; // aligned size of each block
U32 n_elements; // total number of blocks
U32 align; // alignment
};
enum {
APOOL_AUTOGROW = 0
APoolFlags flags;
U32 element_size; // aligned size of each element
U32 element_align; // alignment
U32 n_elements; // total elements in the pool
U32 n_free; // number of free elements
};
typedef struct APoolCreateOptions APoolCreateOptions;
struct APoolCreateOptions {
U32 element_size; // aligned size of each element
U32 element_align; // alignment of elements
U32 n_reserved; // number of elements reserved
APoolFlags flags;
#define apool_create(arena, n_elements, type) \
apool_create_ex(arena, n_elements, sizeof(type), alignof(type));
APool *apool_create_ex(Allocator *allocator, U32 n_elements, U32 element_size, uint32_t element_align);
};
#define apool_create(arena, n_reserved, type) \
apool_create_ex(arena, &(APoolCreateOptions){sizeof(type), alignof(type), n_reserved, 0})
APool *apool_create_ex(Allocator *allocator, const APoolCreateOptions *options);
void *apool_alloc(APool *pool);
void apool_free(APool *pool, void *element);