Update list implementation to work with memory pools (#168)

* Introduce indexed list implementation

* Fix CI tests for pool

* Take bitset out of pool

* Replace joker bitset interactions with wrappers

* Add bitset tests

* Add test to gitignore

---------

Co-authored-by: rfehr-idexx <ric-fehr@idexx.com>
This commit is contained in:
Rickey
2025-11-11 17:56:21 +02:00
committed by GitHub
co-authored by rfehr-idexx
parent d5f59e904b
commit e0cffab768
22 changed files with 1448 additions and 338 deletions
+21 -23
View File
@@ -2,6 +2,9 @@
#define POOL_H
#include <stdint.h>
#include <stdbool.h>
#include "bitset.h"
#ifdef POOLS_TEST_ENV
#define POOLS_DEF_FILE "def_test_mempool.h"
@@ -9,44 +12,28 @@
#define POOLS_DEF_FILE "def_balatro_mempool.h"
#endif
#define POOL_BITS_PER_WORD 32
#define POOL_BITMAP_BYTES 8
typedef struct PoolBitmap {
uint32_t *w;
uint32_t nbits;
uint32_t nwords;
uint32_t cap;
} 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; \
Bitset* bitset; \
type * objects; \
} type##Pool; \
type *pool_get_##type(); \
void pool_free_##type(type *obj); \
int pool_idx_##type(type *obj); \
type *pool_at_##type(int idx);
#define POOL_DEFINE_TYPE(type, capacity) \
BITSET_DEFINE(type##_bitset, capacity) \
static type type##_storage[capacity]; \
static uint32_t type##_bitmap_w[POOL_BITMAP_BYTES] = {0}; \
static type##Pool type##_pool = \
{ \
.bm = { \
.w = type##_bitmap_w, \
.nbits = POOL_BITS_PER_WORD, \
.nwords = POOL_BITMAP_BYTES, \
.cap = capacity, \
}, \
.bitset = & type##_bitset, \
.objects = type##_storage, \
}; \
type * pool_get_##type() \
{ \
int free_offset = pool_bm_get_free_idx(&type##_pool.bm); \
int free_offset = bitset_allocate_idx(type##_pool.bitset); \
if(free_offset == -1) return NULL; \
return &type##_pool.objects[free_offset]; \
} \
@@ -54,11 +41,22 @@ int pool_bm_get_free_idx(PoolBitmap *bm);
{ \
if(entry == NULL) return; \
int offset = entry - &type##_pool.objects[0]; \
pool_bm_clear_idx(&type##_pool.bm, offset); \
bitset_set_idx(type##_pool.bitset, offset, false); \
} \
int pool_idx_##type(type *entry) \
{ \
return entry - &type##_pool.objects[0]; \
} \
type *pool_at_##type(int idx) \
{ \
if(idx < 0 || idx >= (type##_pool.bitset)->cap) return NULL; \
return &type##_pool.objects[idx]; \
}
#define POOL_GET(type) pool_get_##type()
#define POOL_FREE(type, obj) pool_free_##type(obj)
#define POOL_IDX(type, obj) pool_idx_##type(obj) // the index of the object
#define POOL_AT(type, idx) pool_at_##type(idx) // the object at
#define POOL_ENTRY(name, capacity) \
POOL_DECLARE_TYPE(name);