From a62e77f930c063a407cfff202bdbdd91e601bbb2 Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Fri, 18 Jan 2019 15:22:08 +0000 Subject: [PATCH] Align pagemap entries to OS_PAGE_SIZE Guarantee the page map is page aligned. Fix public API. --- src/mem/largealloc.h | 25 ++++++++++++++++++++----- src/mem/pagemap.h | 17 +++++++++-------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/mem/largealloc.h b/src/mem/largealloc.h index 9619861..d9a80cf 100644 --- a/src/mem/largealloc.h +++ b/src/mem/largealloc.h @@ -51,6 +51,13 @@ namespace snmalloc return std::make_pair(r, size); } + void new_block() + { + auto r_size = reserve_block(); + bump = (size_t)r_size.first; + remaining = r_size.second; + } + public: /** * Stack of large allocations that have been returned for reuse. @@ -61,7 +68,7 @@ namespace snmalloc * Primitive allocator for structure that are required before * the allocator can be running. ***/ - void* alloc_chunk(size_t size) + void* alloc_chunk(size_t size, size_t alignment = 64) { // Cache line align size = bits::align_up(size, 64); @@ -70,12 +77,20 @@ namespace snmalloc { FlagLock f(lock); + auto aligned_bump = bits::align_up(bump, alignment); + if ((aligned_bump - bump) < size) + { + new_block(); + } + else + { + remaining -= aligned_bump - bump; + bump = aligned_bump; + } + if (remaining < size) { - auto r_size = reserve_block(); - - bump = (size_t)r_size.first; - remaining = r_size.second; + new_block(); } p = (void*)bump; diff --git a/src/mem/pagemap.h b/src/mem/pagemap.h index b47bd93..1f4595b 100644 --- a/src/mem/pagemap.h +++ b/src/mem/pagemap.h @@ -86,7 +86,8 @@ namespace snmalloc value, (PagemapEntry*)LOCKED_ENTRY, std::memory_order_relaxed)) { auto& v = default_memory_provider; - value = (PagemapEntry*)v.alloc_chunk(PAGEMAP_NODE_SIZE); + value = + (PagemapEntry*)v.alloc_chunk(PAGEMAP_NODE_SIZE, OS_PAGE_SIZE); e->store(value, std::memory_order_release); } else @@ -160,6 +161,13 @@ namespace snmalloc return &(leaf_ix.first->values[leaf_ix.second]); } + std::atomic* get_ptr(void* p) + { + bool success; + return get_addr(p, success); + } + + public: /** * Returns the index of a pagemap entry within a given page. This is used * in code that propagates changes to the pagemap elsewhere. @@ -182,13 +190,6 @@ namespace snmalloc reinterpret_cast(get_addr(p, success))); } - std::atomic* get_ptr(void* p) - { - bool success; - return get_addr(p, success); - } - - public: T get(void* p) { bool success;