ucx/allocator.c

changeset 992
f421aef8f865
parent 845
f3ab28ed22e5
--- a/ucx/allocator.c	Fri Dec 12 12:00:34 2025 +0100
+++ b/ucx/allocator.c	Fri Dec 12 12:28:32 2025 +0100
@@ -31,6 +31,35 @@
 #include <errno.h>
 #include <string.h>
 
+#ifdef _WIN32
+#include <Windows.h>
+#include <sysinfoapi.h>
+unsigned long cx_system_page_size(void) {
+    static unsigned long ps = 0;
+    if (ps == 0) {
+        SYSTEM_INFO sysinfo;
+        GetSystemInfo(&sysinfo);
+        ps = (unsigned long) sysinfo.dwPageSize;
+    }
+    return ps;
+}
+#else
+#include <unistd.h>
+unsigned long cx_system_page_size(void) {
+    static unsigned long ps = 0;
+    if (ps == 0) {
+        long sc = sysconf(_SC_PAGESIZE);
+        if (sc < 0) {
+            // fallback for systems which do not report a value here
+            ps = 4096; // LCOV_EXCL_LINE
+        } else {
+            ps = (unsigned long) sc;
+        }
+    }
+    return ps;
+}
+#endif
+
 static void *cx_malloc_stdlib(
         cx_attr_unused void *d,
         size_t n
@@ -79,6 +108,11 @@
         void **mem,
         size_t n
 ) {
+    if (n == 0) {
+        free(*mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = realloc(*mem, n);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE
@@ -93,6 +127,11 @@
         size_t nmemb,
         size_t size
 ) {
+    if (nmemb == 0 || size == 0) {
+        free(*mem);
+        *mem = NULL;
+        return 0;
+    }
     size_t n;
     if (cx_szmul(nmemb, size, &n)) {
         errno = EOVERFLOW;
@@ -156,6 +195,11 @@
         void **mem,
         size_t n
 ) {
+    if (n == 0) {
+        cxFree(allocator, *mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = allocator->cl->realloc(allocator->data, *mem, n);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE
@@ -171,6 +215,11 @@
         size_t nmemb,
         size_t size
 ) {
+    if (nmemb == 0 || size == 0) {
+        cxFree(allocator, *mem);
+        *mem = NULL;
+        return 0;
+    }
     void *nmem = cxReallocArray(allocator, *mem, nmemb, size);
     if (nmem == NULL) {
         return 1; // LCOV_EXCL_LINE
@@ -194,3 +243,7 @@
 ) {
     allocator->cl->free(allocator->data, mem);
 }
+
+void cxFreeDefault(void *mem) {
+    cxDefaultAllocator->cl->free(cxDefaultAllocator->data, mem);
+}

mercurial