Fri, 05 Dec 2025 16:11:39 +0100
add cx_system_page_size() to allocator.h
resolves #763
--- a/CHANGELOG Thu Dec 04 18:57:54 2025 +0100 +++ b/CHANGELOG Fri Dec 05 16:11:39 2025 +0100 @@ -1,3 +1,8 @@ +Version 4.0 - tbd +------------------------ + +* adds cx_system_page_size() to allocator.h + Version 3.2 - 2025-11-30 ------------------------
--- a/docs/Writerside/topics/about.md Thu Dec 04 18:57:54 2025 +0100 +++ b/docs/Writerside/topics/about.md Fri Dec 05 16:11:39 2025 +0100 @@ -26,6 +26,10 @@ ## Changelog +### Version 4.0 - preview {collapsible="true"} + +* adds cx_system_page_size() to allocator.h + ### Version 3.2 - 2025-11-30 {collapsible="true"} * adds cxMempoolTransfer() and cxMempoolTransferObject()
--- a/docs/Writerside/topics/allocator.h.md Thu Dec 04 18:57:54 2025 +0100 +++ b/docs/Writerside/topics/allocator.h.md Fri Dec 05 16:11:39 2025 +0100 @@ -40,6 +40,8 @@ void cxFree(const CxAllocator *allocator, void *mem); +void cx_system_page_size(void); + void *cx_zalloc(size_t n); int cx_reallocate(void **mem, size_t n); @@ -105,6 +107,9 @@ > with the **same** allocator that was used to allocate the memory. {style="warning"} +The function `cx_system_page_size()` offers a cross-platform way to retrieve the memory page size in bytes. +If, for some reason, the page size cannot be determined, a default of 4096 bytes is returned. + ## Custom Allocator If you want to define your own allocator, you need to initialize the `CxAllocator` structure
--- a/docs/Writerside/writerside.cfg Thu Dec 04 18:57:54 2025 +0100 +++ b/docs/Writerside/writerside.cfg Fri Dec 05 16:11:39 2025 +0100 @@ -5,5 +5,5 @@ <topics dir="topics" web-path="topics"/> <categories src="c.list"/> <images dir="images" web-path="images"/> - <instance src="ucx.tree" version="3.2"/> + <instance src="ucx.tree" version="4.0"/> </ihp> \ No newline at end of file
--- a/src/allocator.c Thu Dec 04 18:57:54 2025 +0100 +++ b/src/allocator.c Fri Dec 05 16:11:39 2025 +0100 @@ -31,6 +31,35 @@ #include <errno.h> #include <string.h> +#ifdef _WIN32 +#include <Windows.h> +#include <sysinfoapi.h> +unsigned long 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 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
--- a/src/buffer.c Thu Dec 04 18:57:54 2025 +0100 +++ b/src/buffer.c Fri Dec 05 16:11:39 2025 +0100 @@ -32,35 +32,6 @@ #include <string.h> #include <errno.h> -#ifdef _WIN32 -#include <Windows.h> -#include <sysinfoapi.h> -static unsigned long system_page_size(void) { - static unsigned long ps = 0; - if (ps == 0) { - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - ps = sysinfo.dwPageSize; - } - return ps; -} -#else -#include <unistd.h> -static unsigned long 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 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); @@ -262,7 +233,7 @@ } static size_t cx_buffer_calculate_minimum_capacity(size_t mincap) { - unsigned long pagesize = system_page_size(); + unsigned long pagesize = cx_system_page_size(); // if page size is larger than 64 KB - for some reason - truncate to 64 KB if (pagesize > 65536) pagesize = 65536; if (mincap < pagesize) {
--- a/src/cx/allocator.h Thu Dec 04 18:57:54 2025 +0100 +++ b/src/cx/allocator.h Fri Dec 05 16:11:39 2025 +0100 @@ -146,6 +146,17 @@ const CxAllocator *allocator, void *data); /** + * Returns the system's memory page size. + * + * If the page size cannot be retrieved from the system, + * a default of 4096 bytes is assumed. + * + * @return the system's memory page size in bytes + */ +cx_attr_nodiscard +CX_EXPORT unsigned long cx_system_page_size(void); + +/** * Reallocate a previously allocated block and changes the pointer in-place, * if necessary. *
--- a/tests/test_buffer.c Thu Dec 04 18:57:54 2025 +0100 +++ b/tests/test_buffer.c Fri Dec 05 16:11:39 2025 +0100 @@ -32,35 +32,6 @@ #include "cx/buffer.h" -#ifdef _WIN32 -#include <Windows.h> -#include <sysinfoapi.h> -static unsigned long system_page_size(void) { - static unsigned long ps = 0; - if (ps == 0) { - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - ps = sysinfo.dwPageSize; - } - return ps; -} -#else -#include <unistd.h> -static unsigned long 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 - CX_TEST(test_buffer_init_wrap_space) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -234,8 +205,8 @@ CX_TEST_ASSERT(buf.capacity == 256); // greater than page size, new capacity should be multiple of pagesize - cxBufferMinimumCapacity(&buf, system_page_size() + 200); - CX_TEST_ASSERT(buf.capacity == system_page_size()*2); + cxBufferMinimumCapacity(&buf, cx_system_page_size() + 200); + CX_TEST_ASSERT(buf.capacity == cx_system_page_size()*2); cxBufferDestroy(&buf); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));