removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two

Mon, 06 Mar 2017 16:09:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 06 Mar 2017 16:09:42 +0100
changeset 241
661f33ef20d8
parent 240
8f937a3a6d11
child 242
a3597d704421

removes unused ucx_mempool_clamp() and changes storage behavior of mempool to increase the capacity by a factor of two

test/mpool_tests.c file | annotate | diff | comparison | revisions
ucx/mempool.c file | annotate | diff | comparison | revisions
ucx/mempool.h file | annotate | diff | comparison | revisions
     1.1 --- a/test/mpool_tests.c	Mon Mar 06 15:37:40 2017 +0100
     1.2 +++ b/test/mpool_tests.c	Mon Mar 06 16:09:42 2017 +0100
     1.3 @@ -67,7 +67,7 @@
     1.4      intptr_t *test = (intptr_t*) ucx_mempool_malloc(pool, sizeof(intptr_t));
     1.5      
     1.6      UCX_TEST_ASSERT(pool->ndata == 2, "counter not incremented");
     1.7 -    UCX_TEST_ASSERT(pool->size == 17, "chcap not called");
     1.8 +    UCX_TEST_ASSERT(pool->size == 2, "chcap not called");
     1.9      
    1.10      intptr_t *pooladdr =
    1.11              (intptr_t*)((char*)pool->data[1] + sizeof(ucx_destructor));
     2.1 --- a/ucx/mempool.c	Mon Mar 06 15:37:40 2017 +0100
     2.2 +++ b/ucx/mempool.c	Mon Mar 06 16:09:42 2017 +0100
     2.3 @@ -93,20 +93,24 @@
     2.4  }
     2.5  
     2.6  int ucx_mempool_chcap(UcxMempool *pool, size_t newcap) {
     2.7 +    if (newcap < pool->ndata) {
     2.8 +        return 1;
     2.9 +    }
    2.10 +    
    2.11      void **data = (void**) realloc(pool->data, newcap*sizeof(void*));
    2.12      if (data) {
    2.13          pool->data = data; 
    2.14          pool->size = newcap;
    2.15 -        return EXIT_SUCCESS;
    2.16 +        return 0;
    2.17      } else {
    2.18 -        return EXIT_FAILURE;
    2.19 +        return 1;
    2.20      }
    2.21  }
    2.22  
    2.23  void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
    2.24      if (pool->ndata >= pool->size) {
    2.25 -        // The hard coded 16 is documented for this function and ucx_mempool_new
    2.26 -        if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) {
    2.27 +        if (pool->size*2 < pool->size /* overflow check */
    2.28 +                || ucx_mempool_chcap(pool, pool->size*2)) {
    2.29              return NULL;
    2.30          }
    2.31      }
    2.32 @@ -147,7 +151,7 @@
    2.33          }
    2.34          fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
    2.35            (intptr_t)ptr, (intptr_t)pool);
    2.36 -        exit(EXIT_FAILURE);
    2.37 +        abort();
    2.38      } else {
    2.39          return newm + sizeof(ucx_destructor);
    2.40      }
     3.1 --- a/ucx/mempool.h	Mon Mar 06 15:37:40 2017 +0100
     3.2 +++ b/ucx/mempool.h	Mon Mar 06 16:09:42 2017 +0100
     3.3 @@ -70,39 +70,29 @@
     3.4  /**
     3.5   * Creates a memory pool with the specified initial size.
     3.6   * 
     3.7 - * As the created memory pool automatically grows in size by 16 elements, when
     3.8 + * As the created memory pool automatically grows in size by factor two when
     3.9   * trying to allocate memory on a full pool, it is recommended that you use
    3.10 - * a multiple of 16 for the initial size.
    3.11 + * a power of two for the initial size.
    3.12   * 
    3.13 - * @param n initial pool size (should be a multiple of 16)
    3.14 + * @param n initial pool size (should be a power of two, e.g. 16)
    3.15   * @return a pointer to the new memory pool
    3.16 + * @see ucx_mempool_new_default()
    3.17   */
    3.18  UcxMempool *ucx_mempool_new(size_t n);
    3.19  
    3.20  /**
    3.21   * Resizes a memory pool.
    3.22   * 
    3.23 + * This function will fail if the new capacity is not sufficient for the
    3.24 + * present data.
    3.25 + * 
    3.26   * @param pool the pool to resize
    3.27   * @param newcap the new capacity
    3.28 - * @return <code>EXIT_SUCCESS</code> on success or
    3.29 - * <code>EXIT_FAILURE</code> on failure
    3.30 + * @return zero on success or non-zero on failure
    3.31   */
    3.32  int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
    3.33  
    3.34  /**
    3.35 - * Changes the pool size to the next smallest multiple of 16.
    3.36 - * 
    3.37 - * You may use this macro, to reduce the pool size after freeing
    3.38 - * many pooled memory items.
    3.39 - * 
    3.40 - * @param pool the pool to clamp
    3.41 - * @return <code>EXIT_SUCCESS</code> on success or
    3.42 - * <code>EXIT_FAILURE</code> on failure
    3.43 - */
    3.44 -#define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \
    3.45 -        (pool->ndata & ~0xF)+0x10)
    3.46 -
    3.47 -/**
    3.48   * Allocates pooled memory.
    3.49   * 
    3.50   * @param pool the memory pool
    3.51 @@ -145,7 +135,7 @@
    3.52   * is called.
    3.53   * 
    3.54   * If you specify memory, that is not pooled by the specified memory pool, the
    3.55 - * behavior is undefined.
    3.56 + * program will terminate with a call to <code>abort()</code>.
    3.57   * 
    3.58   * @param pool the memory pool
    3.59   * @param ptr a pointer to the memory that shall be freed

mercurial