fail again

Wed, 14 Aug 2013 15:24:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 14 Aug 2013 15:24:14 +0200
changeset 143
b843d463ac58
parent 142
ee8cb27d8b8e (current diff)
parent 141
c466e2a6cbd0 (diff)
child 144
b6dcc9d112eb

fail again

     1.1 --- a/ucx/mempool.c	Wed Aug 14 15:22:35 2013 +0200
     1.2 +++ b/ucx/mempool.c	Wed Aug 14 15:24:14 2013 +0200
     1.3 @@ -90,16 +90,18 @@
     1.4  }
     1.5  
     1.6  void *ucx_mempool_malloc(UcxMempool *pool, size_t n) {
     1.7 +    if (pool->ndata >= pool->size) {
     1.8 +        // The hard coded 16 is documented for this function and ucx_mempool_new
     1.9 +        if (ucx_mempool_chcap(pool, pool->size + 16) == EXIT_FAILURE) {
    1.10 +            return NULL;
    1.11 +        }
    1.12 +    }
    1.13 +
    1.14      ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
    1.15      if (!mem) {
    1.16          return NULL;
    1.17      }
    1.18  
    1.19 -    if (pool->ndata >= pool->size) {
    1.20 -        // The hard coded 16 is documented for this function and ucx_mempool_new
    1.21 -        ucx_mempool_chcap(pool, pool->size + 16);
    1.22 -     }
    1.23 -
    1.24      mem->destructor = NULL;
    1.25      pool->data[pool->ndata] = mem;
    1.26      pool->ndata++;
    1.27 @@ -131,7 +133,7 @@
    1.28          }
    1.29          fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
    1.30            (intptr_t)ptr, (intptr_t)pool);
    1.31 -        exit(1);
    1.32 +        exit(EXIT_FAILURE);
    1.33      } else {
    1.34          return newm + sizeof(ucx_destructor);
    1.35      }
    1.36 @@ -142,12 +144,13 @@
    1.37      for(size_t i=0 ; i<pool->ndata ; i++) {
    1.38          if(chunk == pool->data[i]) {
    1.39              if(chunk->destructor != NULL) {
    1.40 -                chunk->destructor(&chunk->c);
    1.41 +                chunk->destructor(&(chunk->c));
    1.42              }
    1.43              free(chunk);
    1.44              size_t last_index = pool->ndata - 1;
    1.45              if(i != last_index) {
    1.46                  pool->data[i] = pool->data[last_index];
    1.47 +                pool->data[last_index] = NULL;
    1.48              }
    1.49              pool->ndata--;
    1.50              return;
     2.1 --- a/ucx/mempool.h	Wed Aug 14 15:22:35 2013 +0200
     2.2 +++ b/ucx/mempool.h	Wed Aug 14 15:24:14 2013 +0200
     2.3 @@ -92,6 +92,19 @@
     2.4  int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
     2.5  
     2.6  /**
     2.7 + * Changes the pool size to the next smallest multiple of 16.
     2.8 + * 
     2.9 + * You may use this macro, to reduce the pool size after freeing
    2.10 + * many pooled memory items.
    2.11 + * 
    2.12 + * @param pool the pool to clamp
    2.13 + * @return <code>EXIT_SUCCESS</code> on success or
    2.14 + * <code>EXIT_FAILURE</code> on failure
    2.15 + */
    2.16 +#define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \
    2.17 +        (pool->ndata & ~0xF)+0x10)
    2.18 +
    2.19 +/**
    2.20   * Allocates pooled memory.
    2.21   * 
    2.22   * @param pool the memory pool
    2.23 @@ -115,6 +128,11 @@
    2.24  /**
    2.25   * Reallocates pooled memory.
    2.26   * 
    2.27 + * If the memory to be reallocated is not contained by the specified pool, this
    2.28 + * function will possibly fail. In case the memory had to be moved to another
    2.29 + * location, this function will print out a message to <code>stderr</code>
    2.30 + * and exit the program with error code <code>EXIT_FAILURE</code>.
    2.31 + * 
    2.32   * @param pool the memory pool
    2.33   * @param ptr a pointer to the memory that shall be reallocated
    2.34   * @param n the new size of the memory

mercurial