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. --- list/include/list.h | 2 +- listpool/include/listpool.h | 57 +++++++++++++++++------------------ listpool/src/listpool.c | 19 ++++++------ mempool/include/mempool.h | 72 ++++++++++++++++++++++++--------------------- 4 files changed, 79 insertions(+), 71 deletions(-) diff --git a/list/include/list.h b/list/include/list.h index b00b48b..945de28 100644 --- a/list/include/list.h +++ b/list/include/list.h @@ -17,5 +17,5 @@ typedef struct list { void list_make(list* list, size_t size); /// Iterates over all the items in the list. -#define list_foreach(LIST, iter) \ +#define list_foreach(LIST, iter) \ for (struct list* iter = LIST; iter; iter = iter->next) diff --git a/listpool/include/listpool.h b/listpool/include/listpool.h index a5e4955..1711449 100644 --- a/listpool/include/listpool.h +++ b/listpool/include/listpool.h @@ -7,11 +7,11 @@ #include /// Define a typed listpool of a given size. -#define DEF_LISTPOOL(POOL, TYPE, NUM_BLOCKS) \ - typedef struct POOL { \ - listpool pool; \ - list nodes[NUM_BLOCKS]; \ - TYPE blocks[NUM_BLOCKS]; \ +#define DEF_LISTPOOL(POOL, TYPE, NUM_BLOCKS) \ + typedef struct POOL { \ + listpool pool; \ + list nodes[NUM_BLOCKS]; \ + TYPE blocks[NUM_BLOCKS]; \ } POOL; /// Creates a new listpool. @@ -20,8 +20,8 @@ assert(POOL); \ const size_t block_size = sizeof((POOL)->blocks[0]); \ const size_t num_blocks = sizeof((POOL)->blocks) / block_size; \ - listpool_make_(&(POOL)->pool, (POOL)->nodes, (POOL)->blocks, num_blocks, \ - block_size); \ + listpool_make_( \ + &(POOL)->pool, (POOL)->nodes, (POOL)->blocks, num_blocks, block_size); \ } /// Allocate a new block. @@ -35,31 +35,31 @@ /// Remove a value from the list. /// Defined here instead of DEF_LISTPOOL_IMPL() because not all types may have /// an operator==. -#define listpool_remove(POOL, VAL) \ - { \ - listpool_foreach(POOL, iter, { \ - if (*iter == VAL) { \ - listpool_free(POOL, &iter); \ - break; \ - } \ - }); \ +#define listpool_remove(POOL, VAL) \ + { \ + listpool_foreach(POOL, iter, { \ + if (*iter == VAL) { \ + listpool_free(POOL, &iter); \ + break; \ + } \ + }); \ } /// Iterate over the used items of the pool. -#define listpool_foreach(POOL, ITER, BODY) \ - for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \ - typeof((POOL)->blocks[0])* ITER = \ - &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \ - (void)ITER; \ - BODY; \ +#define listpool_foreach(POOL, ITER, BODY) \ + for (list* it_ = (POOL)->pool.used; it_; it_ = it_->next) { \ + typeof((POOL)->blocks[0])* ITER = \ + &(POOL)->blocks[it_ - (POOL)->pool.nodes]; \ + (void)ITER; \ + BODY; \ } typedef struct listpool { - size_t block_size_bytes; - size_t num_blocks; - list* free; // Head of the free list. - list* used; // Head of the used list. - list* nodes; // Array of nodes. + size_t block_size_bytes; + size_t num_blocks; + list* free; // Head of the free list. + list* used; // Head of the used list. + list* nodes; // Array of nodes. uint8_t* blocks; // Array of blocks; } listpool; @@ -67,8 +67,9 @@ typedef struct listpool { /// `nodes` must have at least `num_blocks` nodes. /// `blocks` must be at least `num_blocks` * `block_size_bytes` bytes. /// All blocks are zeroed out for convenience. -void listpool_make_(listpool* pool, list* nodes, void* blocks, - size_t num_blocks, size_t block_size_bytes); +void listpool_make_( + listpool* pool, list* nodes, void* blocks, size_t num_blocks, + size_t block_size_bytes); /// Allocate a new block. /// Return 0 if there is no memory left. diff --git a/listpool/src/listpool.c b/listpool/src/listpool.c index 9c86a3b..8e49f32 100644 --- a/listpool/src/listpool.c +++ b/listpool/src/listpool.c @@ -2,15 +2,16 @@ #include -void listpool_make_(listpool* pool, list* nodes, void* blocks, - size_t num_blocks, size_t block_size_bytes) { +void listpool_make_( + listpool* pool, list* nodes, void* blocks, size_t num_blocks, + size_t block_size_bytes) { assert(pool); pool->block_size_bytes = block_size_bytes; - pool->num_blocks = num_blocks; - pool->free = &nodes[0]; - pool->used = 0; - pool->nodes = nodes; - pool->blocks = blocks; + pool->num_blocks = num_blocks; + pool->free = &nodes[0]; + pool->used = 0; + pool->nodes = nodes; + pool->blocks = blocks; list_make(nodes, num_blocks); memset(blocks, 0, num_blocks * block_size_bytes); } @@ -69,9 +70,9 @@ void listpool_free_(listpool* pool, void** block_ptr) { if (!pool->free) { pool->free = item; } else { - item->next = pool->free; + item->next = pool->free; pool->free->prev = item; - pool->free = item; + pool->free = item; } *block_ptr = 0; 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