# HG changeset patch # User Mike Becker # Date 1764947499 -3600 # Node ID 2cbdb482d325dec3c8e0fec2eaa34690c6b9be5b # Parent fd948e5af26eb82763a5ccc9aca1642e8fbc8a44 add cx_system_page_size() to allocator.h resolves #763 diff -r fd948e5af26e -r 2cbdb482d325 CHANGELOG --- 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 ------------------------ diff -r fd948e5af26e -r 2cbdb482d325 docs/Writerside/topics/about.md --- 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() diff -r fd948e5af26e -r 2cbdb482d325 docs/Writerside/topics/allocator.h.md --- 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 diff -r fd948e5af26e -r 2cbdb482d325 docs/Writerside/writerside.cfg --- 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 @@ - + \ No newline at end of file diff -r fd948e5af26e -r 2cbdb482d325 src/allocator.c --- 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 #include +#ifdef _WIN32 +#include +#include +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 +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 diff -r fd948e5af26e -r 2cbdb482d325 src/buffer.c --- 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 #include -#ifdef _WIN32 -#include -#include -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 -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) { diff -r fd948e5af26e -r 2cbdb482d325 src/cx/allocator.h --- 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. * diff -r fd948e5af26e -r 2cbdb482d325 tests/test_buffer.c --- 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 -#include -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 -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));