Skip to content

Commit 5ec16e4

Browse files
committed
pool_foreach, readme
1 parent c5a6758 commit 5ec16e4

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,31 @@ Get a block:
2828
void *ptr = pool_alloc(pool);
2929
```
3030

31-
When not needed anymore, give it back to the pool, and make it reusable:
31+
You can iterate through all the blocks allocated with the given pool like this:
3232

3333
```c
34-
pool_free(pool, ptr1);
34+
static void callback(void *item)
35+
{
36+
//
37+
}
38+
39+
pool_foreach(pool, callback);
40+
```
41+
42+
To check if the block is allocated with the pool:
43+
44+
```c
45+
if (pool_has_ptr(pool, ptr)) { /* */ }
46+
```
47+
48+
When not needed anymore, give the pointer back to the pool, and make it reusable. This will return -1
49+
if the pointer is not known by the pool.
50+
51+
```c
52+
pool_free(pool, ptr);
3553
```
3654
37-
If everything is finished, then actually free all the memory allocated:
55+
To actually free all the memory allocated:
3856
3957
```c
4058
pool_destroy(pool);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mem-pool",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"repo": "isty001/mem-pool",
55
"description": "Fix member sized, growing memory pool",
66
"keywords": [

src/mem_pool.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ typedef struct block Block;
2525

2626

2727
struct buffer {
28-
void *memory;
28+
void *start;
29+
void *current;
2930
void *end;
3031
Buffer *next;
3132
};
@@ -48,9 +49,10 @@ struct mem_pool {
4849
static Buffer *new_buffer(MemPool *pool)
4950
{
5051
Buffer *buff = malloc(sizeof(Buffer));
51-
buff->memory = malloc(pool->buff_size);
52+
buff->start = malloc(pool->buff_size);
53+
buff->current = buff->start;
5254
buff->next = NULL;
53-
buff->end = buff->memory + pool->buff_size;
55+
buff->end = buff->current + pool->buff_size;
5456

5557
return buff;
5658
}
@@ -93,8 +95,8 @@ static void *from_free_list(MemPool *pool)
9395

9496
static void *from_buffer(MemPool *pool, Buffer *buff)
9597
{
96-
void *ptr = buff->memory;
97-
buff->memory += pool->memb_size;
98+
void *ptr = buff->current;
99+
buff->current += pool->memb_size;
98100
unlock(pool);
99101

100102
return ptr;
@@ -109,7 +111,7 @@ void *pool_alloc(MemPool *pool)
109111
}
110112
Buffer *buff = pool->buff_last;
111113

112-
if (buff->memory == buff->end) {
114+
if (buff->current == buff->end) {
113115
buff = new_buffer(pool);
114116
pool->buff_last->next = buff;
115117
pool->buff_last = buff;
@@ -140,6 +142,21 @@ bool pool_has_ptr(MemPool *pool, void *ptr)
140142
return false;
141143
}
142144

145+
void pool_foreach(MemPool *pool, PoolForeach callback)
146+
{
147+
lock(pool);
148+
149+
Buffer *buff = pool->buff_head;
150+
151+
while (buff) {
152+
for (void *block = buff->start; block < buff->current; block += pool->memb_size) {
153+
callback(block);
154+
}
155+
buff = buff->next;
156+
}
157+
unlock(pool);
158+
}
159+
143160
int pool_free(MemPool *pool, void *ptr)
144161
{
145162
if (!pool_has_ptr(pool, ptr)) {

src/mem_pool.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
typedef struct mem_pool MemPool;
1010

11+
typedef void (*PoolForeach)(void *block);
12+
1113

1214
/**
1315
* @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);
2123
void *pool_alloc(MemPool *pool);
2224

2325
/**
24-
* @return true if the pointer was allocated by the Pool
26+
* @return true if the block was allocated by the Pool
2527
*/
2628
bool pool_has_ptr(MemPool *pool, void *ptr);
2729

30+
/**
31+
* Iterates through all the blocks allocated with the given pool
32+
*/
33+
void pool_foreach(MemPool *pool, PoolForeach callback);
34+
2835
/**
2936
* Makes the pointer reusable
3037
*/

test/mem_pool_test.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,37 @@ MU_TEST(test_pool)
3838
pool_destroy(pool);
3939
}
4040

41+
static int ADDED = 0;
42+
43+
static void add_items(int *item)
44+
{
45+
ADDED += *item;
46+
}
47+
48+
MU_TEST(test_foreach)
49+
{
50+
MemPool *pool = pool_init(sizeof(int), 2);
51+
int *a, *b, *c;
52+
53+
a = pool_alloc(pool);
54+
*a = 100;
55+
b = pool_alloc(pool);
56+
*b = 200;
57+
c = pool_alloc(pool);
58+
*c = 50;
59+
60+
pool_foreach(pool, (PoolForeach) add_items);
61+
62+
mu_assert_int_eq(350, ADDED);
63+
64+
pool_destroy(pool);
65+
}
66+
4167
int main(void)
4268
{
4369
MU_RUN_TEST(test_pool);
70+
MU_RUN_TEST(test_foreach);
71+
4472
MU_REPORT();
4573

4674
return 0;

0 commit comments

Comments
 (0)