From 9a3d12a72441dd364934d49f65953f19f9fa3e00 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Mon, 30 Jun 2025 21:23:07 +0100 Subject: [PATCH] Make Secondary Allocator a template parameter (#774) This refactors the use of the Secondary Allocator, so that out of tree implementations can be used. --- src/snmalloc/backend/fixedglobalconfig.h | 5 ++++- src/snmalloc/backend/globalconfig.h | 11 ++++++---- src/snmalloc/global/globalalloc.h | 4 ++-- src/snmalloc/mem/corealloc.h | 7 +++---- src/snmalloc/mem/secondary.h | 20 ------------------- src/snmalloc/snmalloc.h | 10 ++++++++++ src/test/func/domestication/domestication.cc | 2 ++ src/test/func/fixed_region/fixed_region.cc | 1 - .../fixed_region_alloc/fixed_region_alloc.cc | 1 - 9 files changed, 28 insertions(+), 33 deletions(-) delete mode 100644 src/snmalloc/mem/secondary.h diff --git a/src/snmalloc/backend/fixedglobalconfig.h b/src/snmalloc/backend/fixedglobalconfig.h index 8718a11..5bd3b68 100644 --- a/src/snmalloc/backend/fixedglobalconfig.h +++ b/src/snmalloc/backend/fixedglobalconfig.h @@ -1,6 +1,7 @@ #pragma once #include "../backend_helpers/backend_helpers.h" +#include "../mem/secondary/default.h" #include "snmalloc/stl/type_traits.h" #include "standard_range.h" @@ -13,12 +14,14 @@ namespace snmalloc */ template< SNMALLOC_CONCEPT(IsPAL) PAL, - typename ClientMetaDataProvider = NoClientMetaDataProvider> + typename ClientMetaDataProvider = NoClientMetaDataProvider, + typename SecondaryAllocator_ = DefaultSecondaryAllocator> class FixedRangeConfig final : public CommonConfig { public: using PagemapEntry = DefaultPagemapEntry; using ClientMeta = ClientMetaDataProvider; + using SecondaryAllocator = SecondaryAllocator_; private: using ConcretePagemap = diff --git a/src/snmalloc/backend/globalconfig.h b/src/snmalloc/backend/globalconfig.h index 929d9f5..208210b 100644 --- a/src/snmalloc/backend/globalconfig.h +++ b/src/snmalloc/backend/globalconfig.h @@ -3,7 +3,7 @@ #include "../backend_helpers/backend_helpers.h" #include "backend.h" #include "meta_protected_range.h" -#include "snmalloc/mem/secondary.h" +#include "snmalloc/mem/secondary/default.h" #include "standard_range.h" namespace snmalloc @@ -22,16 +22,19 @@ namespace snmalloc * The Configuration sets up a Pagemap for the backend to use, and the state * required to build new allocators (GlobalPoolState). */ - template + template< + typename ClientMetaDataProvider = NoClientMetaDataProvider, + typename SecondaryAllocator_ = DefaultSecondaryAllocator> class StandardConfigClientMeta final : public CommonConfig { - using GlobalPoolState = - PoolState>>; + using GlobalPoolState = PoolState>>; public: using Pal = DefaultPal; using PagemapEntry = DefaultPagemapEntry; using ClientMeta = ClientMetaDataProvider; + using SecondaryAllocator = SecondaryAllocator_; private: using ConcretePagemap = diff --git a/src/snmalloc/global/globalalloc.h b/src/snmalloc/global/globalalloc.h index fa769e3..95e9649 100644 --- a/src/snmalloc/global/globalalloc.h +++ b/src/snmalloc/global/globalalloc.h @@ -302,9 +302,9 @@ namespace snmalloc const auto& entry = Config_::Backend::get_metaentry(address_cast(p_raw)); if (SNMALLOC_UNLIKELY( - !SecondaryAllocator::pass_through && !entry.is_owned() && + !Config::SecondaryAllocator::pass_through && !entry.is_owned() && p_raw != nullptr)) - return SecondaryAllocator::alloc_size(p_raw); + return Config::SecondaryAllocator::alloc_size(p_raw); // TODO What's the domestication policy here? At the moment we just // probe the pagemap with the raw address, without checks. There could // be implicit domestication through the `Config::Pagemap` or diff --git a/src/snmalloc/mem/corealloc.h b/src/snmalloc/mem/corealloc.h index 1b7f7f5..5b837f1 100644 --- a/src/snmalloc/mem/corealloc.h +++ b/src/snmalloc/mem/corealloc.h @@ -6,7 +6,6 @@ #include "metadata.h" #include "pool.h" #include "remotecache.h" -#include "secondary.h" #include "sizeclasstable.h" #include "snmalloc/stl/new.h" #include "ticker.h" @@ -653,7 +652,7 @@ namespace snmalloc } // Check if secondary allocator wants to offer the memory - void* result = SecondaryAllocator::allocate( + void* result = Config::SecondaryAllocator::allocate( [size]() -> stl::Pair { return {size, natural_alignment(size)}; }); @@ -710,7 +709,7 @@ namespace snmalloc SNMALLOC_FAST_PATH capptr::Alloc small_refill(smallsizeclass_t sizeclass, freelist::Iter<>& fast_free_list) { - void* result = SecondaryAllocator::allocate( + void* result = Config::SecondaryAllocator::allocate( [sizeclass]() -> stl::Pair { auto size = sizeclass_to_size(sizeclass); return {size, natural_alignment(size)}; @@ -1285,7 +1284,7 @@ namespace snmalloc } dealloc_cheri_checks(p_tame.unsafe_ptr()); - SecondaryAllocator::deallocate(p_tame.unsafe_ptr()); + Config::SecondaryAllocator::deallocate(p_tame.unsafe_ptr()); } /** diff --git a/src/snmalloc/mem/secondary.h b/src/snmalloc/mem/secondary.h deleted file mode 100644 index 9f3dab3..0000000 --- a/src/snmalloc/mem/secondary.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include "snmalloc/ds_core/defines.h" -#include "snmalloc/ds_core/ptrwrap.h" - -#ifdef SNMALLOC_ENABLE_GWP_ASAN_INTEGRATION -# include "snmalloc/mem/secondary/gwp_asan.h" - -namespace snmalloc -{ - using SecondaryAllocator = GwpAsanSecondaryAllocator; -} // namespace snmalloc -#else -# include "snmalloc/mem/secondary/default.h" - -namespace snmalloc -{ - using SecondaryAllocator = DefaultSecondaryAllocator; -} // namespace snmalloc -#endif diff --git a/src/snmalloc/snmalloc.h b/src/snmalloc/snmalloc.h index deea23a..4781dc7 100644 --- a/src/snmalloc/snmalloc.h +++ b/src/snmalloc/snmalloc.h @@ -6,13 +6,23 @@ // Provides the global configuration for the snmalloc implementation. #include "backend/globalconfig.h" +#ifdef SNMALLOC_ENABLE_GWP_ASAN_INTEGRATION +# include "snmalloc/mem/secondary/gwp_asan.h" +#endif + namespace snmalloc { // If you define SNMALLOC_PROVIDE_OWN_CONFIG then you must provide your own // definition of `snmalloc::Alloc` before including any files that include // `snmalloc.h` or consume the global allocation APIs. #ifndef SNMALLOC_PROVIDE_OWN_CONFIG +# ifdef SNMALLOC_ENABLE_GWP_ASAN_INTEGRATION + using Config = snmalloc::StandardConfigClientMeta< + NoClientMetaDataProvider, + GwpAsanSecondaryAllocator>; +# else using Config = snmalloc::StandardConfigClientMeta; +# endif #endif /** * Create allocator type for this configuration. diff --git a/src/test/func/domestication/domestication.cc b/src/test/func/domestication/domestication.cc index 7deba71..1c2eb9f 100644 --- a/src/test/func/domestication/domestication.cc +++ b/src/test/func/domestication/domestication.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include // Specify type of allocator @@ -18,6 +19,7 @@ namespace snmalloc using Pal = DefaultPal; using PagemapEntry = DefaultPagemapEntry; using ClientMeta = NoClientMetaDataProvider; + using SecondaryAllocator = DefaultSecondaryAllocator; private: using ConcretePagemap = diff --git a/src/test/func/fixed_region/fixed_region.cc b/src/test/func/fixed_region/fixed_region.cc index 25d0606..840c6e9 100644 --- a/src/test/func/fixed_region/fixed_region.cc +++ b/src/test/func/fixed_region/fixed_region.cc @@ -1,4 +1,3 @@ -#include "snmalloc/mem/secondary.h" #include "test/setup.h" #include diff --git a/src/test/func/fixed_region_alloc/fixed_region_alloc.cc b/src/test/func/fixed_region_alloc/fixed_region_alloc.cc index d919c5d..a4d7bc6 100644 --- a/src/test/func/fixed_region_alloc/fixed_region_alloc.cc +++ b/src/test/func/fixed_region_alloc/fixed_region_alloc.cc @@ -1,4 +1,3 @@ -#include "snmalloc/mem/secondary.h" #include "test/setup.h" #include