From 007aa328fcf973b47fcfd8225ab9077cb3c45145 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 23 Jan 2023 16:54:36 -0800 Subject: Format. --- mempool/include/mempool.h | 72 +++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 33 deletions(-) (limited to 'mempool/include/mempool.h') diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h index 41d56e5..f2b20b9 100644 --- a/mempool/include/mempool.h +++ b/mempool/include/mempool.h @@ -6,21 +6,22 @@ #include /// Define a typed mempool of the given number of blocks. -#define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \ - typedef struct POOL { \ - mempool pool; \ - BlockInfo block_info[NUM_BLOCKS]; \ - TYPE blocks[NUM_BLOCKS]; \ +#define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \ + typedef struct POOL { \ + mempool pool; \ + BlockInfo block_info[NUM_BLOCKS]; \ + TYPE blocks[NUM_BLOCKS]; \ } POOL; /// Create a new pool. -#define mempool_make(POOL) \ - { \ - assert(POOL); \ - const size_t block_size = sizeof((POOL)->blocks[0]); \ - const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \ - mempool_make_(&(POOL)->pool, (POOL)->block_info, (POOL)->blocks, \ - num_blocks, block_size); \ +#define mempool_make(POOL) \ + { \ + assert(POOL); \ + const size_t block_size = sizeof((POOL)->blocks[0]); \ + const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \ + mempool_make_( \ + &(POOL)->pool, (POOL)->block_info, (POOL)->blocks, num_blocks, \ + block_size); \ } /// Allocate a new block. @@ -29,30 +30,34 @@ /// Free the block. /// The block pointer is conveniently set to 0. -#define mempool_free(POOL, BLOCK_PTR) \ - assert(*BLOCK_PTR); \ +#define mempool_free(POOL, BLOCK_PTR) \ + assert(*BLOCK_PTR); \ mempool_free_(&(POOL)->pool, (void**)BLOCK_PTR) /// Return the ith block. /// The block must have been allocated. -#define mempool_get_block(POOL, INDEX) \ +#define mempool_get_block(POOL, INDEX) \ ((typeof((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) /// Get the index to the given block. -#define mempool_get_block_index(POOL, BLOCK_PTR) \ +#define mempool_get_block_index(POOL, BLOCK_PTR) \ mempool_get_block_index_(&(POOL)->pool, BLOCK_PTR) /// Iterate over the used blocks of the pool. -#define mempool_foreach(POOL, ITER, BODY) \ - for (size_t i = 0; \ - i < (sizeof((POOL)->blocks) / sizeof(typeof((POOL)->blocks[0]))); \ - ++i) { \ - if (!(POOL)->block_info[i].used) { \ - continue; \ - } \ - typeof((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ - (void)ITER; \ - BODY; \ +/// +/// The caller can use 'i' as the index of the current block. +/// +/// It is valid to mempool_free() the object at each step of the iteration. +#define mempool_foreach(POOL, ITER, BODY) \ + for (size_t i = 0; \ + i < (sizeof((POOL)->blocks) / sizeof(typeof((POOL)->blocks[0]))); \ + ++i) { \ + if (!(POOL)->block_info[i].used) { \ + continue; \ + } \ + typeof((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ + (void)ITER; \ + BODY; \ } typedef struct BlockInfo { @@ -60,20 +65,21 @@ typedef struct BlockInfo { } BlockInfo; typedef struct mempool { - size_t block_size_bytes; - size_t num_blocks; - size_t next_free_block; - bool full; + size_t block_size_bytes; + size_t num_blocks; + size_t next_free_block; + bool full; BlockInfo* block_info; - uint8_t* blocks; + uint8_t* blocks; } mempool; /// Create a pool allocator from user-provided memory. /// `BlockInfo` must hold at least `num_blocks` entries. /// `blocks` must be at least `num_blocks` * `block_size_bytes` bytes. /// All blocks are zeroed out for convenience. -void mempool_make_(mempool*, BlockInfo*, void* blocks, size_t num_blocks, - size_t block_size_bytes); +void mempool_make_( + mempool*, BlockInfo*, void* blocks, size_t num_blocks, + size_t block_size_bytes); /// Allocate a new block. /// Return 0 if there is no memory left. -- cgit v1.2.3