src/buffer.c

changeset 1501
e60860db189f
parent 1319
aa1f580f8f59
--- a/src/buffer.c	Thu Nov 20 20:06:20 2025 +0100
+++ b/src/buffer.c	Sat Nov 22 18:49:39 2025 +0100
@@ -44,16 +44,27 @@
     }
     return ps;
 }
-#define SYSTEM_PAGE_SIZE system_page_size()
 #else
 #include <unistd.h>
-#define SYSTEM_PAGE_SIZE sysconf(_SC_PAGESIZE)
+static unsigned long system_page_size() {
+    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 int buffer_copy_on_write(CxBuffer* buffer) {
     if (0 == (buffer->flags & CX_BUFFER_COPY_ON_WRITE)) return 0;
     void *newspace = cxMalloc(buffer->allocator, buffer->capacity);
-    if (NULL == newspace) return -1;
+    if (NULL == newspace) return -1;  // LCOV_EXCL_LINE
     memcpy(newspace, buffer->space, buffer->size);
     buffer->space = newspace;
     buffer->flags &= ~CX_BUFFER_COPY_ON_WRITE;
@@ -78,9 +89,7 @@
     buffer->flags = flags;
     if (!space) {
         buffer->bytes = cxMalloc(allocator, capacity);
-        if (buffer->bytes == NULL) {
-            return -1; // LCOV_EXCL_LINE
-        }
+        if (buffer->bytes == NULL) return -1; // LCOV_EXCL_LINE
         buffer->flags |= CX_BUFFER_FREE_CONTENTS;
     } else {
         buffer->bytes = space;
@@ -122,7 +131,7 @@
         allocator = cxDefaultAllocator;
     }
     CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer));
-    if (buf == NULL) return NULL;
+    if (buf == NULL) return NULL; // LCOV_EXCL_LINE
     if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) {
         return buf;
     } else {
@@ -208,7 +217,7 @@
         return 0;
     }
 
-    unsigned long pagesize = SYSTEM_PAGE_SIZE;
+    unsigned long pagesize = system_page_size();
     // if page size is larger than 64 KB - for some reason - truncate to 64 KB
     if (pagesize > 65536) pagesize = 65536;
     if (newcap < pagesize) {

mercurial