src/server/ucx/mempool.c

changeset 88
73b3485e96f1
parent 71
069c152f6272
--- a/src/server/ucx/mempool.c	Thu Jul 11 14:21:23 2013 +0200
+++ b/src/server/ucx/mempool.c	Tue Jul 16 12:14:13 2013 +0200
@@ -112,7 +112,7 @@
                 return newm + sizeof(ucx_destructor);
             }
         }
-        fprintf(stderr, "FATAL: 0x%08" PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
+        fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
           (intptr_t)ptr, (intptr_t)pool);
         exit(1);
     } else {
@@ -120,14 +120,37 @@
     }
 }
 
-void ucx_mempool_free(UcxMempool *pool) {
+void ucx_mempool_free(UcxMempool *pool, void *ptr) {
+    ucx_memchunk *chunk = (ucx_memchunk*)((char*)ptr-sizeof(ucx_destructor));
+    for(size_t i=0 ; i<pool->ndata ; i++) {
+        if(chunk == pool->data[i]) {
+            if(chunk->destructor != NULL) {
+                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->ndata--;
+            return;
+        }
+    }
+    fprintf(stderr, "FATAL: 0x%08"PRIxPTR" not in mpool 0x%08" PRIxPTR"\n",
+            (intptr_t)ptr, (intptr_t)pool);
+    exit(1);
+}
+
+void ucx_mempool_destroy(UcxMempool *pool) {
     ucx_memchunk *chunk;
     for(size_t i=0 ; i<pool->ndata ; i++) {
         chunk = (ucx_memchunk*) pool->data[i];
-        if(chunk->destructor != NULL) {
-            chunk->destructor(&chunk->c);
+        if(chunk) {
+            if(chunk->destructor != NULL) {
+                chunk->destructor(&chunk->c);
+            }
+            free(chunk);
         }
-        free(chunk);
     }
     free(pool->data);
     free(pool);
@@ -145,3 +168,17 @@
     rd->ptr = ptr;
     ucx_mempool_set_destr(rd, ucx_mempool_shared_destr);
 }
+
+UcxAllocator* ucx_mempool_allocator(UcxMempool *pool) {
+    UcxAllocator *allocator = (UcxAllocator*)ucx_mempool_malloc(
+            pool, sizeof(UcxAllocator));
+    if(!allocator) {
+        return NULL;
+    }
+    allocator->malloc = (ucx_allocator_malloc)ucx_mempool_malloc;
+    allocator->calloc = (ucx_allocator_calloc)ucx_mempool_calloc;
+    allocator->realloc = (ucx_allocator_realloc)ucx_mempool_realloc;
+    allocator->free = (ucx_allocator_free)ucx_mempool_free;
+    allocator->pool = pool;
+    return allocator;
+}

mercurial