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

--- a/ucx/mempool.c	Wed Aug 14 15:22:35 2013 +0200
+++ b/ucx/mempool.c	Wed Aug 14 15:24:14 2013 +0200
@@ -90,16 +90,18 @@
 }
 
 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) {
+            return NULL;
+        }
+    }
+
     ucx_memchunk *mem = (ucx_memchunk*)malloc(sizeof(ucx_destructor) + n);
     if (!mem) {
         return NULL;
     }
 
-    if (pool->ndata >= pool->size) {
-        // The hard coded 16 is documented for this function and ucx_mempool_new
-        ucx_mempool_chcap(pool, pool->size + 16);
-     }
-
     mem->destructor = NULL;
     pool->data[pool->ndata] = mem;
     pool->ndata++;
@@ -131,7 +133,7 @@
         }
         fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
           (intptr_t)ptr, (intptr_t)pool);
-        exit(1);
+        exit(EXIT_FAILURE);
     } else {
         return newm + sizeof(ucx_destructor);
     }
@@ -142,12 +144,13 @@
     for(size_t i=0 ; i<pool->ndata ; i++) {
         if(chunk == pool->data[i]) {
             if(chunk->destructor != NULL) {
-                chunk->destructor(&chunk->c);
+                chunk->destructor(&(chunk->c));
             }
             free(chunk);
             size_t last_index = pool->ndata - 1;
             if(i != last_index) {
                 pool->data[i] = pool->data[last_index];
+                pool->data[last_index] = NULL;
             }
             pool->ndata--;
             return;
--- a/ucx/mempool.h	Wed Aug 14 15:22:35 2013 +0200
+++ b/ucx/mempool.h	Wed Aug 14 15:24:14 2013 +0200
@@ -92,6 +92,19 @@
 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 <code>EXIT_SUCCESS</code> on success or
+ * <code>EXIT_FAILURE</code> on failure
+ */
+#define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \
+        (pool->ndata & ~0xF)+0x10)
+
+/**
  * Allocates pooled memory.
  * 
  * @param pool the memory pool
@@ -115,6 +128,11 @@
 /**
  * Reallocates pooled memory.
  * 
+ * If the memory to be reallocated is not contained by the specified pool, this
+ * function will possibly fail. In case the memory had to be moved to another
+ * location, this function will print out a message to <code>stderr</code>
+ * and exit the program with error code <code>EXIT_FAILURE</code>.
+ * 
  * @param pool the memory pool
  * @param ptr a pointer to the memory that shall be reallocated
  * @param n the new size of the memory

mercurial