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. --- listpool/include/listpool.h | 57 +++++++++++++++++++++++---------------------- listpool/src/listpool.c | 19 ++++++++------- 2 files changed, 39 insertions(+), 37 deletions(-) (limited to 'listpool') 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; -- cgit v1.2.3