src/mempool.c

changeset 1318
12fa1d37fe48
parent 1283
89935fea4b7c
child 1319
aa1f580f8f59
--- a/src/mempool.c	Thu May 15 15:43:30 2025 +0200
+++ b/src/mempool.c	Thu May 15 16:02:54 2025 +0200
@@ -51,7 +51,8 @@
         errno = EOVERFLOW;
         return 1;
     }
-    struct cx_mempool_memory_s **newdata = realloc(pool->data, newmsize);
+    struct cx_mempool_memory_s **newdata = cxRealloc(
+        cxDefaultAllocator, pool->data, newmsize);
     if (newdata == NULL) return 1;
     pool->data = newdata;
     pool->capacity = newcap;
@@ -68,7 +69,8 @@
         return NULL;
     }
 
-    struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
+    struct cx_mempool_memory_s *mem = cxMalloc(
+        cxDefaultAllocator, sizeof(cx_destructor_func) + n);
     if (mem == NULL) return NULL;
 
     mem->destructor = pool->auto_destr;
@@ -103,7 +105,7 @@
 
     struct cx_mempool_memory_s *mem, *newm;
     mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
-    newm = realloc(mem, n + sizeof(cx_destructor_func));
+    newm = cxRealloc(cxDefaultAllocator, mem, n + sizeof(cx_destructor_func));
 
     if (newm == NULL) return NULL;
     if (mem != newm) {
@@ -134,7 +136,7 @@
             if (mem->destructor) {
                 mem->destructor(mem->c);
             }
-            free(mem);
+            cxFree(cxDefaultAllocator, mem);
             size_t last_index = pool->size - 1;
             if (i != last_index) {
                 pool->data[i] = pool->data[last_index];
@@ -155,11 +157,11 @@
         if (mem->destructor) {
             mem->destructor(mem->c);
         }
-        free(mem);
+        cxFree(cxDefaultAllocator, mem);
     }
-    free(pool->data);
-    free((void*) pool->allocator);
-    free(pool);
+    cxFree(cxDefaultAllocator, pool->data);
+    cxFree(cxDefaultAllocator, (void*) pool->allocator);
+    cxFree(cxDefaultAllocator, pool);
 }
 
 void cxMempoolSetDestructor(
@@ -219,12 +221,12 @@
     }
 
     struct cx_mempool_s *pool =
-            malloc(sizeof(struct cx_mempool_s));
+            cxMalloc(cxDefaultAllocator, sizeof(struct cx_mempool_s));
     if (pool == NULL) return NULL;
 
-    CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
+    CxAllocator *provided_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator));
     if (provided_allocator == NULL) { // LCOV_EXCL_START
-        free(pool);
+        cxFree(cxDefaultAllocator, pool);
         return NULL;
     } // LCOV_EXCL_STOP
     provided_allocator->cl = &cx_mempool_allocator_class;
@@ -232,10 +234,10 @@
 
     pool->allocator = provided_allocator;
 
-    pool->data = malloc(poolsize);
+    pool->data = cxMalloc(cxDefaultAllocator, poolsize);
     if (pool->data == NULL) { // LCOV_EXCL_START
-        free(provided_allocator);
-        free(pool);
+        cxFree(cxDefaultAllocator, provided_allocator);
+        cxFree(cxDefaultAllocator, pool);
         return NULL;
     } // LCOV_EXCL_STOP
 
@@ -246,6 +248,10 @@
     return pool;
 }
 
+static void cx_mempool_free_transferred_allocator(void *al) {
+    cxFree(cxDefaultAllocator, al);
+}
+
 int cxMempoolTransfer(
         CxMempool *source,
         CxMempool *dest
@@ -259,7 +265,7 @@
     }
 
     // allocate a replacement allocator for the source pool
-    CxAllocator *new_source_allocator = malloc(sizeof(CxAllocator));
+    CxAllocator *new_source_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator));
     if (new_source_allocator == NULL) { // LCOV_EXCL_START
         return 1;
     } // LCOV_EXCL_STOP
@@ -274,7 +280,7 @@
     // we have to remove const-ness for this, but that's okay here
     CxAllocator *transferred_allocator = (CxAllocator*) source->allocator;
     transferred_allocator->data = dest;
-    cxMempoolRegister(dest, transferred_allocator, free);
+    cxMempoolRegister(dest, transferred_allocator, cx_mempool_free_transferred_allocator);
 
     // prepare the source pool for re-use
     source->allocator = new_source_allocator;

mercurial