├── README.md ├── Makefile ├── pool.h ├── test.c ├── LICENSE └── pool.c /README.md: -------------------------------------------------------------------------------- 1 | # pool 2 | A generic C memory pool. 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | test: 2 | gcc -g test.c pool.c -o test 3 | 4 | clean: 5 | rm -rf test *.o 6 | -------------------------------------------------------------------------------- /pool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #define POOL_BLOCKS_INITIAL 1 6 | 7 | typedef struct poolFreed{ 8 | struct poolFreed *nextFree; 9 | } poolFreed; 10 | 11 | typedef struct { 12 | uint32_t elementSize; 13 | uint32_t blockSize; 14 | uint32_t used; 15 | int32_t block; 16 | poolFreed *freed; 17 | uint32_t blocksUsed; 18 | uint8_t **blocks; 19 | } pool; 20 | 21 | void poolInitialize(pool *p, const uint32_t elementSize, const uint32_t blockSize); 22 | void poolFreePool(pool *p); 23 | 24 | #ifndef DISABLE_MEMORY_POOLING 25 | void *poolMalloc(pool *p); 26 | void poolFree(pool *p, void *ptr); 27 | #else 28 | #include 29 | #define poolMalloc(p) malloc((p)->blockSize) 30 | #define poolFree(p, d) free(d) 31 | #endif 32 | void poolFreeAll(pool *p); -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | /* test program to test memory pool */ 2 | 3 | #include 4 | #include 5 | #include "pool.h" 6 | 7 | #define SUCCESS 0 8 | #define FAILURE -1 9 | 10 | int test_pool(int element_size, int block_size) 11 | { 12 | pool pool_ptr; 13 | int *test_ptr1 = NULL; 14 | int *test_ptr2 = NULL; 15 | 16 | /* init memory pool with given parameters */ 17 | poolInitialize(&pool_ptr, element_size, block_size); 18 | 19 | /* allocate memory from memory pool */ 20 | test_ptr1 = poolMalloc(&pool_ptr); 21 | test_ptr2 = poolMalloc(&pool_ptr); 22 | 23 | /* test allocated memory validity */ 24 | if(!(test_ptr1 && test_ptr2)) 25 | { 26 | printf("memory allocation failure \n"); 27 | 28 | return FAILURE; 29 | } 30 | 31 | /* free memory allocated from memory pool */ 32 | poolFree(&pool_ptr, test_ptr1); 33 | 34 | /* free all memory used by this pool */ 35 | poolFreePool(&pool_ptr); 36 | 37 | return SUCCESS; 38 | } 39 | 40 | int main() 41 | { 42 | if(test_pool(4, 8) == FAILURE) 43 | printf("test_pool failure %s %d \n", __FILE__, __LINE__); 44 | 45 | if(test_pool(8, 8) == FAILURE) 46 | printf("test_pool failure %s %d \n", __FILE__, __LINE__); 47 | 48 | if(test_pool(16, 8) == FAILURE) 49 | printf("test_pool failure %s %d \n", __FILE__, __LINE__); 50 | 51 | if(test_pool(32, 8) == FAILURE) 52 | printf("test_pool failure %s %d \n", __FILE__, __LINE__); 53 | 54 | 55 | return SUCCESS; 56 | } 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Job Talle 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /pool.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "pool.h" 5 | 6 | #ifndef max 7 | #define max(a,b) ((a)<(b)?(b):(a)) 8 | #endif 9 | 10 | void poolInitialize(pool *p, const uint32_t elementSize, const uint32_t blockSize) 11 | { 12 | uint32_t i; 13 | 14 | p->elementSize = max(elementSize, sizeof(poolFreed)); 15 | p->blockSize = blockSize; 16 | 17 | poolFreeAll(p); 18 | 19 | p->blocksUsed = POOL_BLOCKS_INITIAL; 20 | p->blocks = malloc(sizeof(uint8_t*)* p->blocksUsed); 21 | 22 | for(i = 0; i < p->blocksUsed; ++i) 23 | p->blocks[i] = NULL; 24 | } 25 | 26 | void poolFreePool(pool *p) 27 | { 28 | uint32_t i; 29 | for(i = 0; i < p->blocksUsed; ++i) { 30 | if(p->blocks[i] == NULL) 31 | break; 32 | else 33 | free(p->blocks[i]); 34 | } 35 | 36 | free(p->blocks); 37 | } 38 | 39 | #ifndef DISABLE_MEMORY_POOLING 40 | void *poolMalloc(pool *p) 41 | { 42 | if(p->freed != NULL) { 43 | void *recycle = p->freed; 44 | p->freed = p->freed->nextFree; 45 | return recycle; 46 | } 47 | 48 | if(++p->used == p->blockSize) { 49 | p->used = 0; 50 | if(++p->block == (int32_t)p->blocksUsed) { 51 | uint32_t i; 52 | 53 | p->blocksUsed <<= 1; 54 | p->blocks = realloc(p->blocks, sizeof(uint8_t*)* p->blocksUsed); 55 | 56 | for(i = p->blocksUsed >> 1; i < p->blocksUsed; ++i) 57 | p->blocks[i] = NULL; 58 | } 59 | 60 | if(p->blocks[p->block] == NULL) 61 | p->blocks[p->block] = malloc(p->elementSize * p->blockSize); 62 | } 63 | 64 | return p->blocks[p->block] + p->used * p->elementSize; 65 | } 66 | 67 | void poolFree(pool *p, void *ptr) 68 | { 69 | poolFreed *pFreed = p->freed; 70 | 71 | p->freed = ptr; 72 | p->freed->nextFree = pFreed; 73 | } 74 | #endif 75 | 76 | void poolFreeAll(pool *p) 77 | { 78 | p->used = p->blockSize - 1; 79 | p->block = -1; 80 | p->freed = NULL; 81 | } 82 | --------------------------------------------------------------------------------