Merge pull request #370 from ihaller/ihaller/reverse-lookup

Added the ability to perform a reverse lookup in the Pagemap
This commit is contained in:
Istvan Haller
2021-08-06 17:00:34 +01:00
committed by GitHub

View File

@@ -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