# HG changeset patch # User Mike Becker # Date 1376478816 -7200 # Node ID c466e2a6cbd02bcab87c419aaa3579b57df71a08 # Parent 15f871f50bfddc023fdb7fba7a267c4a5aae948b added mempool clamp + some minor fixes diff -r 15f871f50bfd -r c466e2a6cbd0 ucx/mempool.c --- a/ucx/mempool.c Tue Aug 13 14:20:12 2013 +0200 +++ b/ucx/mempool.c Wed Aug 14 13:13:36 2013 +0200 @@ -90,16 +90,18 @@ } void *ucx_mempool_malloc(UcxMempool *pool, size_t n) { + if (pool->ndata >= pool->size) { + // The hard coded 16 is documented for this function and ucx_mempool_new + if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) { + return NULL; + } + } + ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n); if (!mem) { return NULL; } - if (pool->ndata >= pool->size) { - // The hard coded 16 is documented for this function and ucx_mempool_new - ucx_mempool_chcap(pool, pool->size + 16); - } - mem->destructor = NULL; pool->data[pool->ndata] = mem; pool->ndata++; @@ -131,7 +133,7 @@ } fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", (intptr_t)ptr, (intptr_t)pool); - exit(1); + exit(EXIT_FAILURE); } else { return newm + sizeof(ucx_destructor); } @@ -142,12 +144,13 @@ for(size_t i=0 ; indata ; i++) { if(chunk == pool->data[i]) { if(chunk->destructor != NULL) { - chunk->destructor(&chunk->c); + chunk->destructor(&(chunk->c)); } free(chunk); size_t last_index = pool->ndata - 1; if(i != last_index) { pool->data[i] = pool->data[last_index]; + pool->data[last_index] = NULL; } pool->ndata--; return; diff -r 15f871f50bfd -r c466e2a6cbd0 ucx/mempool.h --- a/ucx/mempool.h Tue Aug 13 14:20:12 2013 +0200 +++ b/ucx/mempool.h Wed Aug 14 13:13:36 2013 +0200 @@ -92,6 +92,19 @@ int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); /** + * Changes the pool size to the next smallest multiple of 16. + * + * You may use this macro, to reduce the pool size after freeing + * many pooled memory items. + * + * @param pool the pool to clamp + * @return EXIT_SUCCESS on success or + * EXIT_FAILURE on failure + */ +#define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \ + (pool->ndata & ~0xF)+0x10) + +/** * Allocates pooled memory. * * @param pool the memory pool @@ -115,6 +128,11 @@ /** * Reallocates pooled memory. * + * If the memory to be reallocated is not contained by the specified pool, this + * function will possibly fail. In case the memory had to be moved to another + * location, this function will print out a message to stderr + * and exit the program with error code EXIT_FAILURE. + * * @param pool the memory pool * @param ptr a pointer to the memory that shall be reallocated * @param n the new size of the memory