From 22e2b5b689ab9558ee71c370743f565ffde7977c Mon Sep 17 00:00:00 2001 From: Istvan Haller Date: Thu, 5 Aug 2021 18:42:17 +0100 Subject: [PATCH 1/3] Added reverse lookup to Pagemap --- src/backend/pagemap.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index d640ae1..ccf262a 100644 --- a/src/backend/pagemap.h +++ b/src/backend/pagemap.h @@ -185,6 +185,21 @@ namespace snmalloc body = new_body; } + /** + * Get the number of entries. + */ + constexpr size_t num_entries() const + { + if constexpr (has_bounds) + { + return size >> GRANULARITY_BITS; + } + else + { + return bits::one_at_bit(bits::ADDRESS_BITS - GRANULARITY_BITS); + } + } + /** * If the location has not been used before, then * `potentially_out_of_range` should be set to true. @@ -223,6 +238,21 @@ namespace snmalloc return body[p >> SHIFT]; } + /** + * Return the starting address corresponding to a given entry within the + * Pagemap. Also checks that the reference actually points to a valid entry. + */ + address_t get_address(const T& t) const + { + address_t entry_offset = address_cast(&t) - address_cast(body); + address_t entry_index = entry_offset / sizeof(T); + if (entry_offset % sizeof(T) != 0 || entry_index >= num_entries()) + { + PAL::error("Internal error: Invalid Pagemap entry for reverse lookup."); + } + return base + (entry_index << GRANULARITY_BITS); + } + void set(address_t p, T t) { #ifdef SNMALLOC_TRACING From df62abac5535660fdacdcc991255b3f28ea2a5e7 Mon Sep 17 00:00:00 2001 From: Istvan Haller Date: Fri, 6 Aug 2021 15:08:24 +0100 Subject: [PATCH 2/3] Added nodiscard --- src/backend/pagemap.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index ccf262a..62c97fd 100644 --- a/src/backend/pagemap.h +++ b/src/backend/pagemap.h @@ -188,7 +188,7 @@ namespace snmalloc /** * Get the number of entries. */ - constexpr size_t num_entries() const + [[nodiscard]] constexpr size_t num_entries() const { if constexpr (has_bounds) { @@ -242,7 +242,7 @@ namespace snmalloc * Return the starting address corresponding to a given entry within the * Pagemap. Also checks that the reference actually points to a valid entry. */ - address_t get_address(const T& t) const + [[nodiscard]] address_t get_address(const T& t) const { address_t entry_offset = address_cast(&t) - address_cast(body); address_t entry_index = entry_offset / sizeof(T); From e45354b3ed93876f64a8477883cefbca9932b810 Mon Sep 17 00:00:00 2001 From: Istvan Haller Date: Fri, 6 Aug 2021 16:19:35 +0100 Subject: [PATCH 3/3] Changed error handling to assert --- src/backend/pagemap.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index 62c97fd..4a92345 100644 --- a/src/backend/pagemap.h +++ b/src/backend/pagemap.h @@ -246,10 +246,8 @@ namespace snmalloc { address_t entry_offset = address_cast(&t) - address_cast(body); address_t entry_index = entry_offset / sizeof(T); - if (entry_offset % sizeof(T) != 0 || entry_index >= num_entries()) - { - PAL::error("Internal error: Invalid Pagemap entry for reverse lookup."); - } + SNMALLOC_ASSERT( + entry_offset % sizeof(T) == 0 && entry_index < num_entries()); return base + (entry_index << GRANULARITY_BITS); }