From 22e2b5b689ab9558ee71c370743f565ffde7977c Mon Sep 17 00:00:00 2001 From: Istvan Haller Date: Thu, 5 Aug 2021 18:42:17 +0100 Subject: [PATCH] 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