diff options
Diffstat (limited to 'mempool')
| -rw-r--r-- | mempool/include/mempool.h | 44 | ||||
| -rw-r--r-- | mempool/test/mempool_test.c | 2 |
2 files changed, 24 insertions, 22 deletions
diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h index 8251a70..23786b3 100644 --- a/mempool/include/mempool.h +++ b/mempool/include/mempool.h | |||
| @@ -19,19 +19,22 @@ | |||
| 19 | #include <stdint.h> | 19 | #include <stdint.h> |
| 20 | 20 | ||
| 21 | /// Define a statically-allocated, typed pool of the given number of blocks. | 21 | /// Define a statically-allocated, typed pool of the given number of blocks. |
| 22 | #define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \ | 22 | #define DEF_MEMPOOL(POOL, TYPE, NUM_BLOCKS) \ |
| 23 | typedef struct POOL { \ | 23 | typedef struct POOL { \ |
| 24 | mempool pool; \ | 24 | mempool pool; \ |
| 25 | BlockInfo block_info[NUM_BLOCKS]; \ | 25 | BlockInfo block_info[NUM_BLOCKS]; \ |
| 26 | TYPE blocks[NUM_BLOCKS]; \ | 26 | TYPE blocks[NUM_BLOCKS]; \ |
| 27 | /* For uniformity with the dynamically-allocated pool. */ \ | ||
| 28 | TYPE* object; \ | ||
| 27 | } POOL; | 29 | } POOL; |
| 28 | 30 | ||
| 29 | /// Define a dynamically-allocated, typed pool. | 31 | /// Define a dynamically-allocated, typed pool. |
| 30 | #define DEF_MEMPOOL_DYN(POOL, TYPE) \ | 32 | #define DEF_MEMPOOL_DYN(POOL, TYPE) \ |
| 31 | typedef struct POOL { \ | 33 | typedef struct POOL { \ |
| 32 | mempool pool; \ | 34 | mempool pool; \ |
| 33 | BlockInfo* block_info; \ | 35 | /* Does not point anywhere. Storing a pointer here so that we can recall \ |
| 34 | TYPE* blocks; \ | 36 | * the type. */ \ |
| 37 | TYPE* object; \ | ||
| 35 | } POOL; | 38 | } POOL; |
| 36 | 39 | ||
| 37 | /// Initialize a statically-allocated pool. | 40 | /// Initialize a statically-allocated pool. |
| @@ -74,7 +77,7 @@ | |||
| 74 | /// Return the ith block. | 77 | /// Return the ith block. |
| 75 | /// The block must have been allocated. | 78 | /// The block must have been allocated. |
| 76 | #define mempool_get_block(POOL, INDEX) \ | 79 | #define mempool_get_block(POOL, INDEX) \ |
| 77 | ((__typeof__((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) | 80 | ((__typeof__((POOL)->object[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) |
| 78 | 81 | ||
| 79 | /// Get the index to the given block. | 82 | /// Get the index to the given block. |
| 80 | #define mempool_get_block_index(POOL, BLOCK_PTR) \ | 83 | #define mempool_get_block_index(POOL, BLOCK_PTR) \ |
| @@ -88,16 +91,15 @@ | |||
| 88 | /// The caller can use 'i' as the index of the current block. | 91 | /// The caller can use 'i' as the index of the current block. |
| 89 | /// | 92 | /// |
| 90 | /// It is valid to mempool_free() the object at each step of the iteration. | 93 | /// It is valid to mempool_free() the object at each step of the iteration. |
| 91 | #define mempool_foreach(POOL, ITER, BODY) \ | 94 | #define mempool_foreach(POOL, ITER, BODY) \ |
| 92 | for (size_t i = 0; \ | 95 | for (size_t i = 0; i < (POOL)->pool.num_blocks; ++i) { \ |
| 93 | i < (sizeof((POOL)->blocks) / sizeof(__typeof__((POOL)->blocks[0]))); \ | 96 | if (!(POOL)->pool.block_info[i].used) { \ |
| 94 | ++i) { \ | 97 | continue; \ |
| 95 | if (!(POOL)->block_info[i].used) { \ | 98 | } \ |
| 96 | continue; \ | 99 | __typeof__((POOL)->object[0])* ITER = \ |
| 97 | } \ | 100 | &(((__typeof__((POOL)->object[0])*)(POOL)->pool.blocks))[i]; \ |
| 98 | __typeof__((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ | 101 | (void)ITER; \ |
| 99 | (void)ITER; \ | 102 | BODY; \ |
| 100 | BODY; \ | ||
| 101 | } | 103 | } |
| 102 | 104 | ||
| 103 | // ----------------------------------------------------------------------------- | 105 | // ----------------------------------------------------------------------------- |
diff --git a/mempool/test/mempool_test.c b/mempool/test/mempool_test.c index e6c24a4..d5ed1ea 100644 --- a/mempool/test/mempool_test.c +++ b/mempool/test/mempool_test.c | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #define NUM_BLOCKS 10 | 5 | #define NUM_BLOCKS 10 |
| 6 | 6 | ||
| 7 | DEF_MEMPOOL(test_pool, int, NUM_BLOCKS); | 7 | DEF_MEMPOOL(test_pool, int, NUM_BLOCKS) |
| 8 | 8 | ||
| 9 | static int count(test_pool* pool) { | 9 | static int count(test_pool* pool) { |
| 10 | int count = 0; | 10 | int count = 0; |
