Fri, 09 Aug 2013 14:36:54 +0200
documented mempool + some fixes
ucx/mempool.c | file | annotate | diff | comparison | revisions | |
ucx/mempool.h | file | annotate | diff | comparison | revisions | |
ucx/test.h | file | annotate | diff | comparison | revisions | |
ucx/utils.h | file | annotate | diff | comparison | revisions |
1.1 --- a/ucx/mempool.c Fri Aug 09 11:32:10 2013 +0200 1.2 +++ b/ucx/mempool.c Fri Aug 09 14:36:54 2013 +0200 1.3 @@ -53,7 +53,9 @@ 1.4 1.5 UcxMempool *ucx_mempool_new(size_t n) { 1.6 UcxMempool *pool = (UcxMempool*)malloc(sizeof(UcxMempool)); 1.7 - if (pool == NULL) return NULL; 1.8 + if (!pool) { 1.9 + return NULL; 1.10 + } 1.11 1.12 pool->data = (void**) malloc(n * sizeof(void*)); 1.13 if (pool->data == NULL) { 1.14 @@ -68,20 +70,23 @@ 1.15 1.16 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { 1.17 void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); 1.18 - if (data == NULL) { 1.19 - return 1; 1.20 - } else { 1.21 + if (data) { 1.22 pool->data = data; 1.23 pool->size = newcap; 1.24 return EXIT_SUCCESS; 1.25 + } else { 1.26 + return EXIT_FAILURE; 1.27 } 1.28 } 1.29 1.30 void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { 1.31 ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); 1.32 - if (mem == NULL) return NULL; 1.33 + if (!mem) { 1.34 + return NULL; 1.35 + } 1.36 1.37 if (pool->ndata >= pool->size) { 1.38 + // The hard coded 16 is documented for this function and ucx_mempool_new 1.39 ucx_mempool_chcap(pool, pool->size + 16); 1.40 } 1.41 1.42 @@ -89,12 +94,12 @@ 1.43 pool->data[pool->ndata] = mem; 1.44 pool->ndata++; 1.45 1.46 - return &mem->c; 1.47 + return &(mem->c); 1.48 } 1.49 1.50 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize) { 1.51 void *ptr = ucx_mempool_malloc(pool, nelem*elsize); 1.52 - if(ptr == NULL) { 1.53 + if (!ptr) { 1.54 return NULL; 1.55 } 1.56 memset(ptr, 0, nelem * elsize); 1.57 @@ -104,7 +109,9 @@ 1.58 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n) { 1.59 char *mem = ((char*)ptr) - sizeof(ucx_destructor); 1.60 char *newm = (char*) realloc(mem, n + sizeof(ucx_destructor)); 1.61 - if (newm == NULL) return NULL; 1.62 + if (!newm) { 1.63 + return NULL; 1.64 + } 1.65 if (mem != newm) { 1.66 for(size_t i=0 ; i < pool->ndata ; i++) { 1.67 if(pool->data[i] == mem) { 1.68 @@ -138,7 +145,7 @@ 1.69 } 1.70 fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", 1.71 (intptr_t)ptr, (intptr_t)pool); 1.72 - exit(1); 1.73 + exit(EXIT_FAILURE); 1.74 } 1.75 1.76 void ucx_mempool_destroy(UcxMempool *pool) { 1.77 @@ -146,8 +153,8 @@ 1.78 for(size_t i=0 ; i<pool->ndata ; i++) { 1.79 chunk = (ucx_memchunk*) pool->data[i]; 1.80 if(chunk) { 1.81 - if(chunk->destructor != NULL) { 1.82 - chunk->destructor(&chunk->c); 1.83 + if(chunk->destructor) { 1.84 + chunk->destructor(&(chunk->c)); 1.85 } 1.86 free(chunk); 1.87 }
2.1 --- a/ucx/mempool.h Fri Aug 09 11:32:10 2013 +0200 2.2 +++ b/ucx/mempool.h Fri Aug 09 14:36:54 2013 +0200 2.3 @@ -26,6 +26,15 @@ 2.4 * POSSIBILITY OF SUCH DAMAGE. 2.5 */ 2.6 2.7 +/** 2.8 + * @file mempool.h 2.9 + * 2.10 + * Memory pool implementation. 2.11 + * 2.12 + * @author Mike Becker 2.13 + * @author Olaf Wintermann 2.14 + */ 2.15 + 2.16 #ifndef UCX_MEMPOOL_H 2.17 #define UCX_MEMPOOL_H 2.18 2.19 @@ -37,29 +46,154 @@ 2.20 extern "C" { 2.21 #endif 2.22 2.23 +/** 2.24 + * A function pointer to a destructor function. 2.25 + * @see ucx_mempool_setdestr() 2.26 + * @see ucx_mempool_regdestr() 2.27 + */ 2.28 typedef void(*ucx_destructor)(void*); 2.29 2.30 +/** 2.31 + * UCX mempool structure. 2.32 + */ 2.33 typedef struct { 2.34 + /** List of pointers to pooled memory. */ 2.35 void **data; 2.36 + /** Count of pooled memory items. */ 2.37 size_t ndata; 2.38 + /** Memory pool size. */ 2.39 size_t size; 2.40 } UcxMempool; 2.41 2.42 +/** Shorthand for a new default memory pool with a capacity of 16 elements. */ 2.43 +#define ucx_mempool_new_default() ucx_mempool_new(16) 2.44 2.45 -#define ucx_mempool_new_default() ucx_mempool_new(16) 2.46 + 2.47 +/** 2.48 + * Creates a memory pool with the specified initial size. 2.49 + * 2.50 + * As the created memory pool automatically grows in size by 16 elements, when 2.51 + * trying to allocate memory on a full pool, it is recommended that you use 2.52 + * a multiple of 16 for the initial size. 2.53 + * 2.54 + * @param n initial pool size (should be a multiple of 16) 2.55 + * @return a pointer to the new memory pool 2.56 + */ 2.57 UcxMempool *ucx_mempool_new(size_t n); 2.58 + 2.59 +/** 2.60 + * Resizes a memory pool. 2.61 + * 2.62 + * @param pool the pool to resize 2.63 + * @param newcap the new capacity 2.64 + * @return <code>EXIT_SUCCESS</code> on success or 2.65 + * <code>EXIT_FAILURE</code> on failure 2.66 + */ 2.67 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); 2.68 2.69 +/** 2.70 + * Allocates pooled memory. 2.71 + * 2.72 + * @param pool the memory pool 2.73 + * @param n amount of memory to allocate 2.74 + * @return a pointer to the allocated memory 2.75 + * @see ucx_allocator_malloc() 2.76 + */ 2.77 void *ucx_mempool_malloc(UcxMempool *pool, size_t n); 2.78 +/** 2.79 + * Allocates a pooled memory array. 2.80 + * 2.81 + * The contents of the allocated memory is set to zero. 2.82 + * 2.83 + * @param pool the memory pool 2.84 + * @param nelem amount of elements to allocate 2.85 + * @param elsize amount of memory per element 2.86 + * @return a pointer to the allocated memory 2.87 + * @see ucx_allocator_calloc() 2.88 + */ 2.89 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); 2.90 +/** 2.91 + * Reallocates pooled memory. 2.92 + * 2.93 + * @param pool the memory pool 2.94 + * @param ptr a pointer to the memory that shall be reallocated 2.95 + * @param n the new size of the memory 2.96 + * @return a pointer to the the location of the memory 2.97 + * @see ucx_allocator_realloc() 2.98 + */ 2.99 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); 2.100 +/** 2.101 + * Frees pooled memory. 2.102 + * 2.103 + * Before freeing the memory, the specified destructor function (if any) 2.104 + * is called. 2.105 + * 2.106 + * If you specify memory, that is not pooled by the specified memory pool, this 2.107 + * is considered as a severe error and an error message is written to 2.108 + * <code>stderr</code> and the application is shut down with code 2.109 + * <code>EXIT_FAILURE</code>. 2.110 + * 2.111 + * @param pool the memory pool 2.112 + * @param ptr a pointer to the memory that shall be freed 2.113 + * @see ucx_mempool_set_destr() 2.114 + */ 2.115 void ucx_mempool_free(UcxMempool *pool, void *ptr); 2.116 2.117 +/** 2.118 + * Destroys a memory pool. 2.119 + * 2.120 + * For each element the destructor function (if any) is called and the element 2.121 + * is freed. 2.122 + * 2.123 + * Each of the registered destructor function that has no corresponding element 2.124 + * within the pool (namely those registered by ucx_mempool_reg_destr) is 2.125 + * called interleaving with the element destruction, but with guarantee to the 2.126 + * order in which they were registered (FIFO order). 2.127 + * 2.128 + * 2.129 + * @param pool the mempool to destroy 2.130 + */ 2.131 void ucx_mempool_destroy(UcxMempool *pool); 2.132 2.133 +/** 2.134 + * Sets a destructor function for the specified memory. 2.135 + * 2.136 + * The destructor is automatically called when the memory is freed or the 2.137 + * pool is destroyed. 2.138 + * 2.139 + * The only requirement for the specified memory is, that it <b>MUST</b> be 2.140 + * pooled memory by an UcxMempool or an element-compatible mempool. The pointer 2.141 + * to the destructor function is saved in a reserved area before the actual 2.142 + * memory. 2.143 + * 2.144 + * @param ptr pooled memory 2.145 + * @param func a pointer to the destructor function 2.146 + * @see ucx_mempool_free() 2.147 + * @see ucx_mempool_destroy() 2.148 + */ 2.149 void ucx_mempool_set_destr(void *ptr, ucx_destructor func); 2.150 + 2.151 +/** 2.152 + * Registers a destructor function for the specified (non-pooled) memory. 2.153 + * 2.154 + * This is useful, if you have memory that has not been allocated by a mempool, 2.155 + * but shall be managed by a mempool. 2.156 + * 2.157 + * This function creates an entry in the specified mempool and the memory will 2.158 + * therefore (logically) convert to pooled memory. 2.159 + * 2.160 + * @param pool the memory pool 2.161 + * @param ptr data the destructor is registered for 2.162 + * @param destr a pointer to the destructor function 2.163 + */ 2.164 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); 2.165 2.166 +/** 2.167 + * Creates an UcxAllocator based on an UcxMempool. 2.168 + * 2.169 + * @param pool the mempool to create the UcxAllocator for 2.170 + * @return a new UcxAllocator based on the specified pool 2.171 + */ 2.172 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool); 2.173 2.174 #ifdef __cplusplus
3.1 --- a/ucx/test.h Fri Aug 09 11:32:10 2013 +0200 3.2 +++ b/ucx/test.h Fri Aug 09 14:36:54 2013 +0200 3.3 @@ -82,9 +82,10 @@ 3.4 3.5 #ifndef __FUNCTION__ 3.6 /** 3.7 - * Alias for the __func__ preprocessor macro. 3.8 - * Some compilers use __func__ and others use __FUNC__. We use __FUNC__ so we 3.9 - * define it for those compilers which use __func__. 3.10 + * Alias for the <code>__func__</code> preprocessor macro. 3.11 + * Some compilers use <code>__func__</code> and others use __FUNC__. 3.12 + * We use __FUNC__ so we define it for those compilers which use 3.13 + * <code>__func__</code>. 3.14 */ 3.15 #define __FUNCTION__ __func__ 3.16 #endif 3.17 @@ -127,7 +128,8 @@ 3.18 * 3.19 * @param suite the suite, the test function shall be added to 3.20 * @param test the test function to register 3.21 - * @return EXIT_SUCCESS on success or EXIT_FAILURE on failure 3.22 + * @return <code>EXIT_SUCCESS</code> on success or 3.23 + * <code>EXIT_FAILURE</code> on failure 3.24 */ 3.25 int ucx_test_register(UcxTestSuite* suite, UcxTest test); 3.26 /**
4.1 --- a/ucx/utils.h Fri Aug 09 11:32:10 2013 +0200 4.2 +++ b/ucx/utils.h Fri Aug 09 14:36:54 2013 +0200 4.3 @@ -26,6 +26,15 @@ 4.4 * POSSIBILITY OF SUCH DAMAGE. 4.5 */ 4.6 4.7 +/** 4.8 + * @file utils.h 4.9 + * 4.10 + * Common utilities like compare and copy functions. 4.11 + * 4.12 + * @author Mike Becker 4.13 + * @author Olaf Wintermann 4.14 + */ 4.15 + 4.16 #ifndef UCX_UTILS_H 4.17 #define UCX_UTILS_H 4.18