From 263e9562c0679efbd320a42475f563ae716db270 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Fri, 20 Nov 2020 02:08:23 +0000 Subject: [PATCH] NFC: Feed Pagemap its primitive allocator as template arg This will let us use Pagemaps further down the dependency stack (specifically, we're going to want a Pagemap inside the AddressSpaceManager) by letting us manually tie the knot rather than rely on GlobalVirtual and default_memory_provider() being defined by the time we want a Pagemap. --- src/mem/chunkmap.h | 2 +- src/mem/largealloc.h | 9 +++++++++ src/mem/pagemap.h | 21 ++++++++++++++++++--- src/test/func/pagemap/pagemap.cc | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/mem/chunkmap.h b/src/mem/chunkmap.h index 8d9f976..37a35db 100644 --- a/src/mem/chunkmap.h +++ b/src/mem/chunkmap.h @@ -81,7 +81,7 @@ namespace snmalloc using ChunkmapPagemap = std::conditional_t< CHUNKMAP_USE_FLATPAGEMAP, FlatPagemap, - Pagemap>; + Pagemap>; struct ForChunkmap {}; diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index c3fbdc8..b3433b5 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -390,4 +390,13 @@ namespace snmalloc { return *(Singleton::get()); } + + struct DefaultPrimAlloc + { + template + static T* alloc_chunk(Args&&... args) + { + return default_memory_provider().alloc_chunk(args...); + } + }; } // namespace snmalloc diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index ad462b7..f15502e 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -53,8 +53,24 @@ namespace snmalloc * allocators do not interact with this directly but rather via the * static ChunkMap object, which encapsulates knowledge about the * pagemap's parametric type T. + * + * The other template paramters are... + * + * GRANULARITY_BITS: the log2 of the size in bytes of the address space + * granule associated with each entry. + * + * default_content: An initial value of T (typically "0" or something akin) + * + * PrimAlloc: A class used to source PageMap-internal memory; it must have a + * method callable as if it had the following type: + * + * template static T* alloc_chunk(void); */ - template + template< + size_t GRANULARITY_BITS, + typename T, + T default_content, + typename PrimAlloc> class Pagemap { private: @@ -154,8 +170,7 @@ namespace snmalloc if (e->compare_exchange_strong( value, LOCKED_ENTRY, std::memory_order_relaxed)) { - auto& v = default_memory_provider(); - value = v.alloc_chunk(); + value = PrimAlloc::template alloc_chunk(); e->store(value, std::memory_order_release); } else diff --git a/src/test/func/pagemap/pagemap.cc b/src/test/func/pagemap/pagemap.cc index 571cff0..ad96170 100644 --- a/src/test/func/pagemap/pagemap.cc +++ b/src/test/func/pagemap/pagemap.cc @@ -15,7 +15,7 @@ using namespace snmalloc; using T = size_t; static constexpr size_t GRANULARITY_BITS = 9; static constexpr T PRIME = 251; -Pagemap pagemap_test; +Pagemap pagemap_test; int main(int argc, char** argv) {