Files
balatro-gba-chinese-jocker-…/include/pool.h
T
2025-10-17 16:55:07 -07:00

58 lines
2.9 KiB
C

#ifndef POOL_H
#define POOL_H
#include <tonc.h>
#define POOL_BITS_PER_WORD 32
#define POOL_MAX_CAPACITY 128
typedef struct PoolBitmap {
u32 *w;
u32 nbits;
u32 nwords;
} PoolBitmap;
void pool_bm_clear_idx(PoolBitmap *bm, int idx);
int pool_bm_get_free_idx(PoolBitmap *bm);
#define POOL_DECLARE_TYPE(type) \
typedef struct \
{ \
PoolBitmap bm; \
type * objects; \
u32 max_entries; \
} type##Pool; \
type *pool_get_##type(type##Pool* pool); \
void pool_free_##type(type##Pool *pool, type *obj); \
void pool_init_##type(type##Pool *pool);
#define POOL_DEFINE_TYPE(type, capacity) \
static type type##_storage[capacity]; \
static u32 type##_bitmap_w[sizeof(u32)] = {0}; \
static type##Pool type##_pool = \
{ \
.bm = { \
.w = type##_bitmap_w, \
.nbits = POOL_BITS_PER_WORD, \
.nwords = sizeof(u32)}, \
.objects = type##_storage, \
.max_entries = capacity, \
}; \
type * pool_get_##type(type##Pool *pool) \
{ \
int free_offset = pool_bm_get_free_idx(&pool->bm); \
if(free_offset == -1) return NULL; \
return &pool->objects[free_offset]; \
} \
void pool_free_##type(type##Pool *pool, type *entry) \
{ \
if(entry == NULL) return; \
int offset = entry - &pool->objects[0]; \
pool_bm_clear_idx(&pool->bm, offset); \
}
#define POOL_GET(type) pool_get_##type(&type##_pool)
#define POOL_FREE(type, obj) pool_free_##type(&type##_pool, obj)
#endif // POOL_H