Files
nob/src/base/pool.h
T

37 lines
1.2 KiB
C

#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)
};
typedef struct APool APool;
struct APool {
APoolElement *free_list; // free list
U8 *start; // start of user buffer
Allocator *allocator;
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_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);