ucx/mempool.c

changeset 314
8722a668fb2a
parent 255
bf19378aed58
child 335
c1bc13faadaa
--- a/ucx/mempool.c	Fri Sep 22 20:19:00 2017 +0200
+++ b/ucx/mempool.c	Fri Sep 22 20:42:33 2017 +0200
@@ -93,25 +93,30 @@
 }
 
 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) {
+        size_t newcap = pool->size*2;
+        if (newcap < pool->size || ucx_mempool_chcap(pool, newcap)) {
             return NULL;
         }
     }
 
-    ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
+    void *p = malloc(sizeof(ucx_destructor) + n);
+    ucx_memchunk *mem = (ucx_memchunk*)p;
     if (!mem) {
         return NULL;
     }
@@ -147,7 +152,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);
     }
@@ -172,7 +177,7 @@
     }
     fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
             (intptr_t)ptr, (intptr_t)pool);
-    exit(EXIT_FAILURE);
+    abort();
 }
 
 void ucx_mempool_destroy(UcxMempool *pool) {

mercurial