From fe3b42e1cf2d26b07001a092dc32254eefd55f1a Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Mon, 17 Jun 2024 10:02:16 +0100 Subject: [PATCH 1/3] 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. --- src/snmalloc/pal/pal_posix.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 097d18f61..84b711083 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -130,7 +130,11 @@ namespace snmalloc #endif ; +#ifdef PAGESIZE + static constexpr size_t 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 From 4ac25310a16e819e0a58f2249134947fbe9497b2 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Fri, 28 Jun 2024 12:26:02 +0100 Subject: [PATCH 2/3] Allow pagesize to come from cmake. --- CMakeLists.txt | 4 ++++ src/snmalloc/pal/pal_posix.h | 8 ++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 745ae198f..73fbf16fe 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") + if(MSVC AND SNMALLOC_STATIC_LIBRARY AND (SNMALLOC_STATIC_LIBRARY_PREFIX STREQUAL "")) message(FATAL_ERROR "Empty static library prefix not supported on MSVC") endif() @@ -249,6 +251,8 @@ endif() add_as_define_value(SNMALLOC_MIN_ALLOC_SIZE) add_as_define_value(SNMALLOC_MIN_ALLOC_STEP_SIZE) +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 84b711083..1df0e5bca 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,11 @@ namespace snmalloc | Entropy #endif ; - -#ifdef PAGESIZE +#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 = PAGESIZE; #else static constexpr size_t page_size = Aal::smallest_page_size; From bda20b7c34bc730a2f7c0a04016cd3bbff00e43e Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Fri, 28 Jun 2024 15:21:20 +0100 Subject: [PATCH 3/3] Update src/snmalloc/pal/pal_posix.h Co-authored-by: Nathaniel Filardo <105816689+nwf-msr@users.noreply.github.com> --- src/snmalloc/pal/pal_posix.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/snmalloc/pal/pal_posix.h b/src/snmalloc/pal/pal_posix.h index 1df0e5bca..2685ba9f4 100644 --- a/src/snmalloc/pal/pal_posix.h +++ b/src/snmalloc/pal/pal_posix.h @@ -135,7 +135,7 @@ namespace snmalloc 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 = 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