ucx/mempool.c

changeset 135
a0aa1c15f46b
parent 131
fc3af16818a3
child 138
7800811078b8
     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          }

mercurial