From 686cb3321fc0a198e0628501ca341fcbcb4872bc Mon Sep 17 00:00:00 2001 From: Matthew Parkinson Date: Fri, 16 Jul 2021 09:51:29 +0100 Subject: [PATCH] Use SFINAE for varying API on pagemap --- src/backend/pagemap.h | 73 ++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index 843e8df..727e3b4 100644 --- a/src/backend/pagemap.h +++ b/src/backend/pagemap.h @@ -51,52 +51,47 @@ namespace snmalloc public: constexpr FlatPagemap() = default; - template - void init(ASM* a, address_t b = 0, size_t s = 0) + template + std::enable_if_t init(ASM* a, address_t b, size_t s) { - if constexpr (has_bounds) - { + static_assert( + has_bounds_ == has_bounds, "Don't set SFINAE template parameter!"); #ifdef SNMALLOC_TRACING - std::cout << "Pagemap.init " << (void*)b << " (" << s << ")" - << std::endl; + std::cout << "Pagemap.init " << (void*)b << " (" << s << ")" << std::endl; #endif - SNMALLOC_ASSERT(s != 0); - // Align the start and end. We won't store for the very ends as they - // are not aligned to a chunk boundary. - base = bits::align_up(b, bits::one_at_bit(GRANULARITY_BITS)); - auto end = bits::align_down(b + s, bits::one_at_bit(GRANULARITY_BITS)); - size = end - base; - body = a->template reserve( - bits::next_pow2((size >> SHIFT) * sizeof(T))) - .template as_static() - .unsafe_ptr(); - ; - } - else - { - // The parameters should not be set without has_bounds. - UNUSED(s); - UNUSED(b); - SNMALLOC_ASSERT(s == 0); - SNMALLOC_ASSERT(b == 0); + SNMALLOC_ASSERT(s != 0); + // Align the start and end. We won't store for the very ends as they + // are not aligned to a chunk boundary. + base = bits::align_up(b, bits::one_at_bit(GRANULARITY_BITS)); + auto end = bits::align_down(b + s, bits::one_at_bit(GRANULARITY_BITS)); + size = end - base; + body = a->template reserve( + bits::next_pow2((size >> SHIFT) * sizeof(T))) + .template as_static() + .unsafe_ptr(); + } - static constexpr size_t COVERED_BITS = - bits::ADDRESS_BITS - GRANULARITY_BITS; - static constexpr size_t ENTRIES = bits::one_at_bit(COVERED_BITS); - auto new_body = (a->template reserve(ENTRIES * sizeof(T))) - .template as_static() - .unsafe_ptr(); + template + std::enable_if_t init(ASM* a) + { + static_assert( + has_bounds_ == has_bounds, "Don't set SFINAE template parameter!"); + static constexpr size_t COVERED_BITS = + bits::ADDRESS_BITS - GRANULARITY_BITS; + static constexpr size_t ENTRIES = bits::one_at_bit(COVERED_BITS); + auto new_body = (a->template reserve(ENTRIES * sizeof(T))) + .template as_static() + .unsafe_ptr(); - // Ensure bottom page is committed - commit_entry(&new_body[0]); + // Ensure bottom page is committed + commit_entry(&new_body[0]); - // Set up zero page - new_body[0] = body[0]; + // Set up zero page + new_body[0] = body[0]; - body = new_body; - // TODO this is pretty sparse, should we ignore huge pages for it? - // madvise(body, size, MADV_NOHUGEPAGE); - } + body = new_body; + // TODO this is pretty sparse, should we ignore huge pages for it? + // madvise(body, size, MADV_NOHUGEPAGE); } /**