# HG changeset patch # User Mike Becker # Date 1488812982 -3600 # Node ID 661f33ef20d85ead42edeca20d79fbfd2a4048d9 # Parent 8f937a3a6d11513776ae6d41001545fe289d18c2 removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two diff -r 8f937a3a6d11 -r 661f33ef20d8 test/mpool_tests.c --- a/test/mpool_tests.c Mon Mar 06 15:37:40 2017 +0100 +++ b/test/mpool_tests.c Mon Mar 06 16:09:42 2017 +0100 @@ -67,7 +67,7 @@ intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t)); UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented"); - UCX_TEST_ASSERT(pool->size == 17, "chcap not called"); + UCX_TEST_ASSERT(pool->size == 2, "chcap not called"); intptr_t *pooladdr = (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor)); diff -r 8f937a3a6d11 -r 661f33ef20d8 ucx/mempool.c --- a/ucx/mempool.c Mon Mar 06 15:37:40 2017 +0100 +++ b/ucx/mempool.c Mon Mar 06 16:09:42 2017 +0100 @@ -93,20 +93,24 @@ } int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) { + if (newcap < pool->ndata) { + return 1; + } + void **data = (void**) realloc(pool->data, newcap*sizeof(void*)); if (data) { pool->data = data; pool->size = newcap; - return EXIT_SUCCESS; + return 0; } else { - return EXIT_FAILURE; + return 1; } } 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) { + if (pool->size*2 < pool->size /* overflow check */ + || ucx_mempool_chcap(pool, pool->size*2)) { return NULL; } } @@ -147,7 +151,7 @@ } fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n", (intptr_t)ptr, (intptr_t)pool); - exit(EXIT_FAILURE); + abort(); } else { return newm + sizeof(ucx_destructor); } diff -r 8f937a3a6d11 -r 661f33ef20d8 ucx/mempool.h --- a/ucx/mempool.h Mon Mar 06 15:37:40 2017 +0100 +++ b/ucx/mempool.h Mon Mar 06 16:09:42 2017 +0100 @@ -70,39 +70,29 @@ /** * Creates a memory pool with the specified initial size. * - * As the created memory pool automatically grows in size by 16 elements, when + * As the created memory pool automatically grows in size by factor two when * trying to allocate memory on a full pool, it is recommended that you use - * a multiple of 16 for the initial size. + * a power of two for the initial size. * - * @param n initial pool size (should be a multiple of 16) + * @param n initial pool size (should be a power of two, e.g. 16) * @return a pointer to the new memory pool + * @see ucx_mempool_new_default() */ UcxMempool *ucx_mempool_new(size_t n); /** * Resizes a memory pool. * + * This function will fail if the new capacity is not sufficient for the + * present data. + * * @param pool the pool to resize * @param newcap the new capacity - * @return EXIT_SUCCESS on success or - * EXIT_FAILURE on failure + * @return zero on success or non-zero on failure */ 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 @@ -145,7 +135,7 @@ * is called. * * If you specify memory, that is not pooled by the specified memory pool, the - * behavior is undefined. + * program will terminate with a call to abort(). * * @param pool the memory pool * @param ptr a pointer to the memory that shall be freed