Skip to content

Commit

Permalink
pool_foreach, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Isty001 committed Apr 14, 2017
1 parent c5a6758 commit 5ec16e4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
29 changes: 23 additions & 6 deletions src/mem_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typedef struct block Block;


struct buffer {
void *memory;
void *start;
void *current;
void *end;
Buffer *next;
};
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
9 changes: 8 additions & 1 deletion src/mem_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
*/
Expand Down
28 changes: 28 additions & 0 deletions test/mem_pool_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5ec16e4

Please sign in to comment.