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.
This commit is contained in:
Nathaniel Filardo
2020-11-20 02:08:23 +00:00
committed by Matthew Parkinson
parent 83b72722cf
commit 263e9562c0
4 changed files with 29 additions and 5 deletions

View File

@@ -81,7 +81,7 @@ namespace snmalloc
using ChunkmapPagemap = std::conditional_t<
CHUNKMAP_USE_FLATPAGEMAP,
FlatPagemap<SUPERSLAB_BITS, uint8_t>,
Pagemap<SUPERSLAB_BITS, uint8_t, 0>>;
Pagemap<SUPERSLAB_BITS, uint8_t, 0, DefaultPrimAlloc>>;
struct ForChunkmap
{};

View File

@@ -390,4 +390,13 @@ namespace snmalloc
{
return *(Singleton<GlobalVirtual*, GlobalVirtual::make>::get());
}
struct DefaultPrimAlloc
{
template<typename T, size_t alignment, typename... Args>
static T* alloc_chunk(Args&&... args)
{
return default_memory_provider().alloc_chunk<T, alignment>(args...);
}
};
} // namespace snmalloc

View File

@@ -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<typename T, size_t alignment> static T* alloc_chunk(void);
*/
template<size_t GRANULARITY_BITS, typename T, T default_content>
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<PagemapEntry, OS_PAGE_SIZE>();
value = PrimAlloc::template alloc_chunk<PagemapEntry, OS_PAGE_SIZE>();
e->store(value, std::memory_order_release);
}
else

View File

@@ -15,7 +15,7 @@ using namespace snmalloc;
using T = size_t;
static constexpr size_t GRANULARITY_BITS = 9;
static constexpr T PRIME = 251;
Pagemap<GRANULARITY_BITS, T, 0> pagemap_test;
Pagemap<GRANULARITY_BITS, T, 0, DefaultPrimAlloc> pagemap_test;
int main(int argc, char** argv)
{