src/allocator.c

changeset 963
2f601274bbac
parent 935
312fb24c14de
--- a/src/allocator.c	Thu Oct 31 14:54:44 2024 +0100
+++ b/src/allocator.c	Thu Oct 31 17:53:55 2024 +0100
@@ -27,6 +27,9 @@
  */
 
 #include "cx/allocator.h"
+#include "cx/utils.h"
+
+#include <errno.h>
 
 __attribute__((__malloc__, __alloc_size__(2)))
 static void *cx_malloc_stdlib(
@@ -89,6 +92,27 @@
     }
 }
 
+#undef cx_reallocatearray
+int cx_reallocatearray(
+        void **mem,
+        size_t nmemb,
+        size_t size
+) {
+    size_t n;
+    if (cx_szmul(nmemb, size, &n)) {
+        errno = ENOMEM;
+        return 1;
+    } else {
+        void *nmem = realloc(*mem, n);
+        if (nmem == NULL) {
+            return 1;
+        } else {
+            *mem = nmem;
+            return 0;
+        }
+    }
+}
+
 // IMPLEMENTATION OF HIGH LEVEL API
 
 void *cxMalloc(
@@ -106,6 +130,22 @@
     return allocator->cl->realloc(allocator->data, mem, n);
 }
 
+void *cxReallocArray(
+        const CxAllocator *allocator,
+        void *mem,
+        size_t nmemb,
+        size_t size
+) {
+    size_t n;
+    if (cx_szmul(nmemb, size, &n)) {
+        errno = ENOMEM;
+        return NULL;
+    } else {
+        return allocator->cl->realloc(allocator->data, mem, n);
+    }
+}
+
+#undef cxReallocate
 int cxReallocate(
         const CxAllocator *allocator,
         void **mem,
@@ -120,6 +160,22 @@
     }
 }
 
+#undef cxReallocateArray
+int cxReallocateArray(
+        const CxAllocator *allocator,
+        void **mem,
+        size_t nmemb,
+        size_t size
+) {
+    void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
+    if (nmem == NULL) {
+        return 1;
+    } else {
+        *mem = nmem;
+        return 0;
+    }
+}
+
 void *cxCalloc(
         const CxAllocator *allocator,
         size_t nelem,

mercurial