diff --git a/src/backend/pagemap.h b/src/backend/pagemap.h index d640ae1..4a92345 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. + */ + [[nodiscard]] 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,19 @@ 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. + */ + [[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); + SNMALLOC_ASSERT( + entry_offset % sizeof(T) == 0 && entry_index < num_entries()); + return base + (entry_index << GRANULARITY_BITS); + } + void set(address_t p, T t) { #ifdef SNMALLOC_TRACING