From ab4fe848045a322c3568d7fb22e575616ccf4835 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Wed, 25 Sep 2024 11:27:31 +0100 Subject: [PATCH] Provide option to CMake for Page Size (#664) * Pickup page size from unistd.h This uses the PAGESIZE constant from the unistd.h on POSIX. This should make the code more resilient to being compiled on platforms with different page sizes. * Allow pagesize to come from cmake. * Update src/snmalloc/pal/pal_posix.h Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com> --------- Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com> --- CMakeLists.txt | 4 ++++ src/snmalloc/pal/pal_posix.h | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ec0a5e9..a5aa318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,8 @@ endif() set(SNMALLOC_MIN_ALLOC_SIZE "" CACHE STRING "Minimum allocation bytes (power of 2)") set(SNMALLOC_MIN_ALLOC_STEP_SIZE "" CACHE STRING "Minimum allocation step (power of 2)") +set(SNMALLOC_PAGESIZE "" CACHE STRING "Page size in bytes") + set(SNMALLOC_DEALLOC_BATCH_RING_ASSOC "" CACHE STRING "Associativity of deallocation batch cache; 0 to disable") set(SNMALLOC_DEALLOC_BATCH_RING_SET_BITS "" CACHE STRING "Logarithm of number of deallocation batch cache associativity sets") @@ -257,6 +259,8 @@ add_as_define_value(SNMALLOC_MIN_ALLOC_STEP_SIZE) add_as_define_value(SNMALLOC_DEALLOC_BATCH_RING_ASSOC) add_as_define_value(SNMALLOC_DEALLOC_BATCH_RING_SET_BITS) +add_as_define_value(SNMALLOC_PAGESIZE) + target_compile_definitions(snmalloc INTERFACE $<$:MALLOC_USABLE_SIZE_QUALIFIER=const>) # In debug and CI builds, link the backtrace library so that we can get stack diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 097d18f..2685ba9 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -8,6 +8,7 @@ #endif #include #include +#include #include #include #include @@ -129,8 +130,15 @@ namespace snmalloc | Entropy #endif ; - +#ifdef SNMALLOC_PAGESIZE + static_assert( + bits::is_pow2(SNMALLOC_PAGESIZE), "Page size must be a power of 2"); + static constexpr size_t page_size = SNMALLOC_PAGESIZE; +#elif defined(PAGESIZE) + static constexpr size_t page_size = max(Aal::smallest_page_size, PAGESIZE); +#else static constexpr size_t page_size = Aal::smallest_page_size; +#endif /** * Address bits are potentially mediated by some POSIX OSes, but generally