From 44e9abe8888ba82b596d74f82a4861c20a70c47e Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Tue, 12 May 2020 03:21:31 +0000 Subject: [PATCH] Move OS_PAGE_SIZE to PAL --- src/mem/allocconfig.h | 11 ----------- src/mem/sizeclass.h | 2 +- src/pal/pal.h | 21 +++++++++++++++++++++ src/pal/pal_apple.h | 4 ++-- src/pal/pal_bsd.h | 2 +- src/pal/pal_freebsd_kernel.h | 1 - src/pal/pal_linux.h | 9 +++++---- src/pal/pal_open_enclave.h | 3 +++ src/pal/pal_plain.h | 1 - src/pal/pal_posix.h | 11 ++++++----- src/pal/pal_windows.h | 11 ++++++----- 11 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/mem/allocconfig.h b/src/mem/allocconfig.h index 2e9bb6d..9704b94 100644 --- a/src/mem/allocconfig.h +++ b/src/mem/allocconfig.h @@ -92,18 +92,7 @@ namespace snmalloc // Used to isolate values on cache lines to prevent false sharing. static constexpr size_t CACHELINE_SIZE = 64; - // Used to keep Superslab metadata committed. - static constexpr size_t OS_PAGE_SIZE = 0x1000; static constexpr size_t PAGE_ALIGNED_SIZE = OS_PAGE_SIZE << INTERMEDIATE_BITS; - // Some system headers (e.g. Linux' sys/user.h, FreeBSD's machine/param.h) - // define `PAGE_SIZE` as a macro. We don't use `PAGE_SIZE` as our variable - // name, to avoid conflicts, but if we do see a macro definition then check - // that our value matches the platform's expected value. -#ifdef PAGE_SIZE - static_assert( - PAGE_SIZE == OS_PAGE_SIZE, - "Page size from system header does not match snmalloc config page size."); -#endif // Minimum allocation size is space for two pointers. static_assert(bits::next_pow2_const(sizeof(void*)) == sizeof(void*)); diff --git a/src/mem/sizeclass.h b/src/mem/sizeclass.h index 5b200d1..e5bb6e3 100644 --- a/src/mem/sizeclass.h +++ b/src/mem/sizeclass.h @@ -1,6 +1,6 @@ #pragma once -#include "../pal/pal_consts.h" +#include "../pal/pal.h" #include "allocconfig.h" namespace snmalloc diff --git a/src/pal/pal.h b/src/pal/pal.h index ed5ea11..d8a106c 100644 --- a/src/pal/pal.h +++ b/src/pal/pal.h @@ -59,4 +59,25 @@ namespace snmalloc */ template constexpr static bool pal_supports = (PAL::pal_features & F) == F; + + // Used to keep Superslab metadata committed. + static constexpr size_t OS_PAGE_SIZE = Pal::page_size; + + static_assert( + bits::next_pow2_const(OS_PAGE_SIZE) == OS_PAGE_SIZE, + "OS_PAGE_SIZE must be a power of two"); + static_assert( + OS_PAGE_SIZE % Aal::smallest_page_size == 0, + "The smallest architectural page size must divide OS_PAGE_SIZE"); + + // Some system headers (e.g. Linux' sys/user.h, FreeBSD's machine/param.h) + // define `PAGE_SIZE` as a macro. We don't use `PAGE_SIZE` as our variable + // name, to avoid conflicts, but if we do see a macro definition then check + // that our value matches the platform's expected value. +#ifdef PAGE_SIZE + static_assert( + PAGE_SIZE == OS_PAGE_SIZE, + "Page size from system header does not match snmalloc config page size."); +#endif + } // namespace snmalloc diff --git a/src/pal/pal_apple.h b/src/pal/pal_apple.h index b27dd43..0f10ee4 100644 --- a/src/pal/pal_apple.h +++ b/src/pal/pal_apple.h @@ -32,9 +32,9 @@ namespace snmalloc template void zero(void* p, size_t size) { - if (page_aligned || is_aligned_block(p, size)) + if (page_aligned || is_aligned_block(p, size)) { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); void* r = mmap( p, size, diff --git a/src/pal/pal_bsd.h b/src/pal/pal_bsd.h index 2641a03..c446dcb 100644 --- a/src/pal/pal_bsd.h +++ b/src/pal/pal_bsd.h @@ -33,7 +33,7 @@ namespace snmalloc */ void notify_not_using(void* p, size_t size) noexcept { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); madvise(p, size, MADV_FREE); } }; diff --git a/src/pal/pal_freebsd_kernel.h b/src/pal/pal_freebsd_kernel.h index 6327942..84f4841 100644 --- a/src/pal/pal_freebsd_kernel.h +++ b/src/pal/pal_freebsd_kernel.h @@ -1,7 +1,6 @@ #pragma once #include "../ds/bits.h" -#include "../mem/allocconfig.h" #if defined(FreeBSD_KERNEL) extern "C" diff --git a/src/pal/pal_linux.h b/src/pal/pal_linux.h index d4c20a7..3ed63a3 100644 --- a/src/pal/pal_linux.h +++ b/src/pal/pal_linux.h @@ -2,7 +2,6 @@ #if defined(__linux__) # include "../ds/bits.h" -# include "../mem/allocconfig.h" # include "pal_posix.h" # include @@ -26,6 +25,8 @@ namespace snmalloc */ static constexpr uint64_t pal_features = PALPOSIX::pal_features; + static constexpr size_t page_size = 0x1000; + /** * OS specific function for zeroing memory. * @@ -41,12 +42,12 @@ namespace snmalloc // MADV_DONTNEED. switch back to memset only for QEMU. # ifndef SNMALLOC_QEMU_WORKAROUND if ( - (page_aligned || is_aligned_block(p, size)) && - (size > SLAB_SIZE)) + (page_aligned || is_aligned_block(p, size)) && + (size > 16 * page_size)) { // Only use this on large allocations as memset faster, and doesn't // introduce IPI so faster for small allocations. - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); madvise(p, size, MADV_DONTNEED); } else diff --git a/src/pal/pal_open_enclave.h b/src/pal/pal_open_enclave.h index 25983a4..77101db 100644 --- a/src/pal/pal_open_enclave.h +++ b/src/pal/pal_open_enclave.h @@ -19,6 +19,9 @@ namespace snmalloc * PAL supports. */ static constexpr uint64_t pal_features = 0; + + static constexpr size_t page_size = 0x1000; + [[noreturn]] static void error(const char* const str) { UNUSED(str); diff --git a/src/pal/pal_plain.h b/src/pal/pal_plain.h index b55edb4..a7cd199 100644 --- a/src/pal/pal_plain.h +++ b/src/pal/pal_plain.h @@ -1,7 +1,6 @@ #pragma once #include "../ds/bits.h" -#include "../mem/allocconfig.h" namespace snmalloc { diff --git a/src/pal/pal_posix.h b/src/pal/pal_posix.h index e1bef12..12a85a9 100644 --- a/src/pal/pal_posix.h +++ b/src/pal/pal_posix.h @@ -1,7 +1,6 @@ #pragma once #include "../ds/address.h" -#include "../mem/allocconfig.h" #if defined(BACKTRACE_HEADER) # include BACKTRACE_HEADER #endif @@ -39,6 +38,8 @@ namespace snmalloc */ static constexpr uint64_t pal_features = LazyCommit; + static constexpr size_t page_size = 0x1000; + static void print_stack_trace() { #ifdef BACKTRACE_HEADER @@ -72,7 +73,7 @@ namespace snmalloc */ void notify_not_using(void* p, size_t size) noexcept { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); #ifdef USE_POSIX_COMMIT_CHECKS mprotect(p, size, PROT_NONE); #else @@ -92,7 +93,7 @@ namespace snmalloc void notify_using(void* p, size_t size) noexcept { SNMALLOC_ASSERT( - is_aligned_block(p, size) || (zero_mem == NoZero)); + is_aligned_block(p, size) || (zero_mem == NoZero)); #ifdef USE_POSIX_COMMIT_CHECKS mprotect(p, size, PROT_READ | PROT_WRITE); @@ -119,9 +120,9 @@ namespace snmalloc template void zero(void* p, size_t size) noexcept { - if (page_aligned || is_aligned_block(p, size)) + if (page_aligned || is_aligned_block(p, size)) { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); void* r = mmap( p, size, diff --git a/src/pal/pal_windows.h b/src/pal/pal_windows.h index e325d3a..f8a5325 100644 --- a/src/pal/pal_windows.h +++ b/src/pal/pal_windows.h @@ -2,7 +2,6 @@ #include "../ds/address.h" #include "../ds/bits.h" -#include "../mem/allocconfig.h" #ifdef _WIN32 # ifndef _MSC_VER @@ -83,6 +82,8 @@ namespace snmalloc # endif ; + static constexpr size_t page_size = 0x1000; + /** * Check whether the low memory state is still in effect. This is an * expensive operation and should not be on any fast paths. @@ -115,7 +116,7 @@ namespace snmalloc /// Notify platform that we will not be using these pages void notify_not_using(void* p, size_t size) noexcept { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); BOOL ok = VirtualFree(p, size, MEM_DECOMMIT); @@ -128,7 +129,7 @@ namespace snmalloc void notify_using(void* p, size_t size) noexcept { SNMALLOC_ASSERT( - is_aligned_block(p, size) || (zero_mem == NoZero)); + is_aligned_block(p, size) || (zero_mem == NoZero)); void* r = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE); @@ -140,9 +141,9 @@ namespace snmalloc template void zero(void* p, size_t size) noexcept { - if (page_aligned || is_aligned_block(p, size)) + if (page_aligned || is_aligned_block(p, size)) { - SNMALLOC_ASSERT(is_aligned_block(p, size)); + SNMALLOC_ASSERT(is_aligned_block(p, size)); notify_not_using(p, size); notify_using(p, size); }