diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index de01af4..6100257 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -132,11 +132,11 @@ namespace snmalloc * Primitive allocator for structure that are required before * the allocator can be running. */ - template - void* alloc_chunk(size_t size) + template + T* alloc_chunk(Args&&... args) { // Cache line align - size = bits::align_up(size, 64); + size_t size = bits::align_up(sizeof(T), 64); void* p; { @@ -169,7 +169,7 @@ namespace snmalloc PAL::template notify_using( (void*)page_start, page_end - page_start); - return p; + return new (p) T(std::forward(args)...); } /** diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index dbb7c4b..accf6a8 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -95,6 +95,9 @@ namespace snmalloc static_assert( sizeof(PagemapEntry) == sizeof(Leaf), "Should be the same size"); + static_assert( + sizeof(PagemapEntry) == PAGEMAP_NODE_SIZE, "Should be the same size"); + // Init removed as not required as this is only ever a global // cl is generating a memset of zero, which will be a problem // in libc/ucrt bring up. On ucrt this will run after the first @@ -121,8 +124,7 @@ namespace snmalloc value, (PagemapEntry*)LOCKED_ENTRY, std::memory_order_relaxed)) { auto& v = default_memory_provider; - value = - (PagemapEntry*)v.alloc_chunk(PAGEMAP_NODE_SIZE); + value = v.alloc_chunk(); e->store(value, std::memory_order_release); } else diff --git a/src/mem/pool.h b/src/mem/pool.h index 5fed053..076caba 100644 --- a/src/mem/pool.h +++ b/src/mem/pool.h @@ -21,6 +21,8 @@ namespace snmalloc { private: friend Pooled; + template + friend class MemoryProviderStateMixin; std::atomic_flag lock = ATOMIC_FLAG_INIT; MPMCStack stack; @@ -33,8 +35,7 @@ namespace snmalloc static Pool* make(MemoryProvider& memory_provider) noexcept { - auto r = memory_provider.alloc_chunk(sizeof(Pool)); - return new (r) Pool(memory_provider); + return memory_provider.template alloc_chunk(memory_provider); } static Pool* make() noexcept @@ -50,10 +51,8 @@ namespace snmalloc if (p != nullptr) return p; - p = (T*)memory_provider - .template alloc_chunk(sizeof(T)); - - new (p) T(std::forward(args)...); + p = memory_provider + .template alloc_chunk(std::forward(args)...); FlagLock f(lock); p->list_next = list;