src/mempool.c

changeset 1328
2cf66dee40b8
parent 1325
20caf6efaf07
child 1329
343eac5ac824
--- a/src/mempool.c	Fri May 23 12:44:24 2025 +0200
+++ b/src/mempool.c	Fri May 23 13:36:11 2025 +0200
@@ -39,11 +39,12 @@
     size_t newcap = pool->capacity >= 1000 ?
         pool->capacity + 1000 : pool->capacity * 2;
     size_t newmsize;
+    // LCOV_EXCL_START
     if (pool->capacity > newcap
             || cx_szmul(newcap, sizeof(void*), &newmsize)) {
         errno = EOVERFLOW;
         return 1;
-    }
+    } // LCOV_EXCL_STOP
     void **newdata = cxReallocDefault(pool->data, newmsize);
     if (newdata == NULL) return 1;
     pool->data = newdata;
@@ -59,11 +60,12 @@
     // we do not expect so many registrations
     size_t newcap = pool->registered_capacity + 8;
     size_t newmsize;
+    // LCOV_EXCL_START
     if (pool->registered_capacity > newcap || cx_szmul(newcap,
             sizeof(struct cx_mempool_foreign_memory_s), &newmsize)) {
         errno = EOVERFLOW;
         return 1;
-    }
+    } // LCOV_EXCL_STOP
     void *newdata = cxReallocDefault(pool->registered, newmsize);
     if (newdata == NULL) return 1;
     pool->registered = newdata;
@@ -78,7 +80,7 @@
     struct cx_mempool_s *pool = p;
 
     if (cx_mempool_ensure_capacity(pool, pool->size + 1)) {
-        return NULL;
+        return NULL; // LCOV_EXCL_LINE
     }
 
     struct cx_mempool_memory_s *mem =
@@ -107,33 +109,6 @@
     return ptr;
 }
 
-static void *cx_mempool_realloc_simple(
-        void *p,
-        void *ptr,
-        size_t n
-) {
-    struct cx_mempool_s *pool = p;
-
-    const unsigned overhead = sizeof(struct cx_mempool_memory_s);
-    struct cx_mempool_memory_s *mem =
-        (void *) (((char *) ptr) - overhead);
-    struct cx_mempool_memory_s *newm =
-        cxReallocDefault(mem, n + overhead);
-
-    if (newm == NULL) return NULL;
-    if (mem != newm) {
-        for (size_t i = 0; i < pool->size; i++) {
-            if (pool->data[i] == mem) {
-                pool->data[i] = newm;
-                return ((char*)newm) + overhead;
-            }
-        }
-        abort(); // LCOV_EXCL_LINE
-    } else {
-        return ptr;
-    }
-}
-
 static void cx_mempool_free_simple(
         void *p,
         void *ptr
@@ -168,6 +143,41 @@
     abort(); // LCOV_EXCL_LINE
 }
 
+static void *cx_mempool_realloc_simple(
+        void *p,
+        void *ptr,
+        size_t n
+) {
+    if (ptr == NULL) {
+        return cx_mempool_malloc_simple(p, n);
+    }
+    if (n == 0) {
+        cx_mempool_free_simple(p, ptr);
+        return NULL;
+    }
+    struct cx_mempool_s *pool = p;
+
+    const unsigned overhead = sizeof(struct cx_mempool_memory_s);
+    struct cx_mempool_memory_s *mem =
+        (void *) (((char *) ptr) - overhead);
+    struct cx_mempool_memory_s *newm =
+        cxReallocDefault(mem, n + overhead);
+
+    if (newm == NULL) return NULL;
+    if (mem != newm) {
+        for (size_t i = 0; i < pool->size; i++) {
+            if (pool->data[i] == mem) {
+                pool->data[i] = newm;
+                return ((char*)newm) + overhead;
+            }
+        }
+        abort(); // LCOV_EXCL_LINE
+    } else {
+        // unfortunately glibc() realloc seems to always move
+        return ptr;  // LCOV_EXCL_LINE
+    }
+}
+
 static void cx_mempool_free_all_simple(const struct cx_mempool_s *pool) {
     const bool has_destr = pool->destr;
     const bool has_destr2 = pool->destr2;
@@ -200,11 +210,11 @@
     struct cx_mempool_s *pool = p;
 
     if (cx_mempool_ensure_capacity(pool, pool->size + 1)) {
-        return NULL;
+        return NULL;  // LCOV_EXCL_LINE
     }
 
     struct cx_mempool_memory2_s *mem =
-        cxMallocDefault(sizeof(struct cx_mempool_memory_s) + n);
+        cxMallocDefault(sizeof(struct cx_mempool_memory2_s) + n);
     if (mem == NULL) return NULL;
     mem->destructor = NULL;
     mem->data = NULL;
@@ -230,33 +240,6 @@
     return ptr;
 }
 
-static void *cx_mempool_realloc_advanced(
-        void *p,
-        void *ptr,
-        size_t n
-) {
-    struct cx_mempool_s *pool = p;
-
-    const unsigned overhead = sizeof(struct cx_mempool_memory2_s);
-    struct cx_mempool_memory2_s *mem =
-        (void *) (((char *) ptr) - overhead);
-    struct cx_mempool_memory2_s *newm =
-        cxReallocDefault(mem, n + overhead);
-
-    if (newm == NULL) return NULL;
-    if (mem != newm) {
-        for (size_t i = 0; i < pool->size; i++) {
-            if (pool->data[i] == mem) {
-                pool->data[i] = newm;
-                return ((char*)newm) + overhead;
-            }
-        }
-        abort(); // LCOV_EXCL_LINE
-    } else {
-        return ptr;
-    }
-}
-
 static void cx_mempool_free_advanced(
         void *p,
         void *ptr
@@ -291,6 +274,41 @@
     abort(); // LCOV_EXCL_LINE
 }
 
+static void *cx_mempool_realloc_advanced(
+        void *p,
+        void *ptr,
+        size_t n
+) {
+    if (ptr == NULL) {
+        return cx_mempool_malloc_advanced(p, n);
+    }
+    if (n == 0) {
+        cx_mempool_free_advanced(p, ptr);
+        return NULL;
+    }
+    struct cx_mempool_s *pool = p;
+
+    const unsigned overhead = sizeof(struct cx_mempool_memory2_s);
+    struct cx_mempool_memory2_s *mem =
+        (void *) (((char *) ptr) - overhead);
+    struct cx_mempool_memory2_s *newm =
+        cxReallocDefault(mem, n + overhead);
+
+    if (newm == NULL) return NULL;
+    if (mem != newm) {
+        for (size_t i = 0; i < pool->size; i++) {
+            if (pool->data[i] == mem) {
+                pool->data[i] = newm;
+                return ((char*)newm) + overhead;
+            }
+        }
+        abort(); // LCOV_EXCL_LINE
+    } else {
+        // unfortunately glibc() realloc seems to always move
+        return ptr;  // LCOV_EXCL_LINE
+    }
+}
+
 static void cx_mempool_free_all_advanced(const struct cx_mempool_s *pool) {
     const bool has_destr = pool->destr;
     const bool has_destr2 = pool->destr2;
@@ -324,7 +342,7 @@
     struct cx_mempool_s *pool = p;
 
     if (cx_mempool_ensure_capacity(pool, pool->size + 1)) {
-        return NULL;
+        return NULL; // LCOV_EXCL_LINE
     }
 
     void *mem = cxMallocDefault(n);
@@ -351,27 +369,6 @@
     return ptr;
 }
 
-static void *cx_mempool_realloc_pure(
-        void *p,
-        void *ptr,
-        size_t n
-) {
-    struct cx_mempool_s *pool = p;
-    void *newm = cxReallocDefault(ptr, n);
-    if (newm == NULL) return NULL;
-    if (ptr != newm) {
-        for (size_t i = 0; i < pool->size; i++) {
-            if (pool->data[i] == ptr) {
-                pool->data[i] = newm;
-                return newm;
-            }
-        }
-        abort(); // LCOV_EXCL_LINE
-    } else {
-        return ptr;
-    }
-}
-
 static void cx_mempool_free_pure(
         void *p,
         void *ptr
@@ -400,6 +397,35 @@
     abort(); // LCOV_EXCL_LINE
 }
 
+static void *cx_mempool_realloc_pure(
+        void *p,
+        void *ptr,
+        size_t n
+) {
+    if (ptr == NULL) {
+        return cx_mempool_malloc_pure(p, n);
+    }
+    if (n == 0) {
+        cx_mempool_free_pure(p, ptr);
+        return NULL;
+    }
+    struct cx_mempool_s *pool = p;
+    void *newm = cxReallocDefault(ptr, n);
+    if (newm == NULL) return NULL;
+    if (ptr != newm) {
+        for (size_t i = 0; i < pool->size; i++) {
+            if (pool->data[i] == ptr) {
+                pool->data[i] = newm;
+                return newm;
+            }
+        }
+        abort(); // LCOV_EXCL_LINE
+    } else {
+        // unfortunately glibc() realloc seems to always move
+        return ptr;  // LCOV_EXCL_LINE
+    }
+}
+
 static void cx_mempool_free_all_pure(const struct cx_mempool_s *pool) {
     const bool has_destr = pool->destr;
     const bool has_destr2 = pool->destr2;
@@ -526,9 +552,10 @@
     if (capacity == 0) capacity = 16;
     size_t poolsize;
     if (cx_szmul(capacity, sizeof(void*), &poolsize)) {
+        // LCOV_EXCL_START
         errno = EOVERFLOW;
         return NULL;
-    }
+    }  // LCOV_EXCL_STOP
 
     CxAllocator *provided_allocator = cxMallocDefault(sizeof(CxAllocator));
     if (provided_allocator == NULL) { // LCOV_EXCL_START
@@ -536,10 +563,10 @@
     } // LCOV_EXCL_STOP
 
     CxMempool *pool = cxCallocDefault(1, sizeof(CxMempool));
-    if (pool == NULL) {
+    if (pool == NULL) {  // LCOV_EXCL_START
         cxFreeDefault(provided_allocator);
         return NULL;
-    }
+    }  // LCOV_EXCL_STOP
 
     provided_allocator->data = pool;
     pool->allocator = provided_allocator;

mercurial