From 5ec16e430b066e6b84d1346fa81a87fe352ed3b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkasv=C3=B6lgyi=20Istv=C3=A1n?= Date: Fri, 14 Apr 2017 12:24:32 +0200 Subject: [PATCH] pool_foreach, readme --- README.md | 24 +++++++++++++++++++++--- package.json | 2 +- src/mem_pool.c | 29 +++++++++++++++++++++++------ src/mem_pool.h | 9 ++++++++- test/mem_pool_test.c | 28 ++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ee6f97d..e999520 100644 --- a/README.md +++ b/README.md @@ -28,13 +28,31 @@ Get a block: void *ptr = pool_alloc(pool); ``` -When not needed anymore, give it back to the pool, and make it reusable: +You can iterate through all the blocks allocated with the given pool like this: ```c -pool_free(pool, ptr1); +static void callback(void *item) +{ + // +} + +pool_foreach(pool, callback); +``` + +To check if the block is allocated with the pool: + +```c +if (pool_has_ptr(pool, ptr)) { /* */ } +``` + +When not needed anymore, give the pointer back to the pool, and make it reusable. This will return -1 +if the pointer is not known by the pool. + +```c +pool_free(pool, ptr); ``` -If everything is finished, then actually free all the memory allocated: +To actually free all the memory allocated: ```c pool_destroy(pool); diff --git a/package.json b/package.json index 995f87c..df802d0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mem-pool", - "version": "0.1.0", + "version": "0.2.0", "repo": "isty001/mem-pool", "description": "Fix member sized, growing memory pool", "keywords": [ diff --git a/src/mem_pool.c b/src/mem_pool.c index 4b92ec1..1a2960a 100644 --- a/src/mem_pool.c +++ b/src/mem_pool.c @@ -25,7 +25,8 @@ typedef struct block Block; struct buffer { - void *memory; + void *start; + void *current; void *end; Buffer *next; }; @@ -48,9 +49,10 @@ struct mem_pool { static Buffer *new_buffer(MemPool *pool) { Buffer *buff = malloc(sizeof(Buffer)); - buff->memory = malloc(pool->buff_size); + buff->start = malloc(pool->buff_size); + buff->current = buff->start; buff->next = NULL; - buff->end = buff->memory + pool->buff_size; + buff->end = buff->current + pool->buff_size; return buff; } @@ -93,8 +95,8 @@ static void *from_free_list(MemPool *pool) static void *from_buffer(MemPool *pool, Buffer *buff) { - void *ptr = buff->memory; - buff->memory += pool->memb_size; + void *ptr = buff->current; + buff->current += pool->memb_size; unlock(pool); return ptr; @@ -109,7 +111,7 @@ void *pool_alloc(MemPool *pool) } Buffer *buff = pool->buff_last; - if (buff->memory == buff->end) { + if (buff->current == buff->end) { buff = new_buffer(pool); pool->buff_last->next = buff; pool->buff_last = buff; @@ -140,6 +142,21 @@ bool pool_has_ptr(MemPool *pool, void *ptr) return false; } +void pool_foreach(MemPool *pool, PoolForeach callback) +{ + lock(pool); + + Buffer *buff = pool->buff_head; + + while (buff) { + for (void *block = buff->start; block < buff->current; block += pool->memb_size) { + callback(block); + } + buff = buff->next; + } + unlock(pool); +} + int pool_free(MemPool *pool, void *ptr) { if (!pool_has_ptr(pool, ptr)) { diff --git a/src/mem_pool.h b/src/mem_pool.h index dbdd66e..73b202d 100644 --- a/src/mem_pool.h +++ b/src/mem_pool.h @@ -8,6 +8,8 @@ typedef struct mem_pool MemPool; +typedef void (*PoolForeach)(void *block); + /** * @return a new MemPool, with the given block size. If it runs out of space, @@ -21,10 +23,15 @@ MemPool *pool_init(size_t block_size, size_t increase_count); void *pool_alloc(MemPool *pool); /** - * @return true if the pointer was allocated by the Pool + * @return true if the block was allocated by the Pool */ bool pool_has_ptr(MemPool *pool, void *ptr); +/** + * Iterates through all the blocks allocated with the given pool + */ +void pool_foreach(MemPool *pool, PoolForeach callback); + /** * Makes the pointer reusable */ diff --git a/test/mem_pool_test.c b/test/mem_pool_test.c index c4b43b7..6e63b8f 100644 --- a/test/mem_pool_test.c +++ b/test/mem_pool_test.c @@ -38,9 +38,37 @@ MU_TEST(test_pool) pool_destroy(pool); } +static int ADDED = 0; + +static void add_items(int *item) +{ + ADDED += *item; +} + +MU_TEST(test_foreach) +{ + MemPool *pool = pool_init(sizeof(int), 2); + int *a, *b, *c; + + a = pool_alloc(pool); + *a = 100; + b = pool_alloc(pool); + *b = 200; + c = pool_alloc(pool); + *c = 50; + + pool_foreach(pool, (PoolForeach) add_items); + + mu_assert_int_eq(350, ADDED); + + pool_destroy(pool); +} + int main(void) { MU_RUN_TEST(test_pool); + MU_RUN_TEST(test_foreach); + MU_REPORT(); return 0;