in src/backend/pagemap.h [153:206]
std::enable_if_t<!has_bounds_> init()
{
static_assert(
has_bounds_ == has_bounds, "Don't set SFINAE template parameter!");
static constexpr size_t REQUIRED_SIZE = required_size();
#ifdef SNMALLOC_CHECK_CLIENT
// Allocate a power of two extra to allow the placement of the
// pagemap be difficult to guess.
size_t additional_size = bits::next_pow2(REQUIRED_SIZE) * 4;
size_t request_size = REQUIRED_SIZE + additional_size;
#else
size_t request_size = REQUIRED_SIZE;
#endif
auto new_body_untyped = PAL::reserve(request_size);
if (new_body_untyped == nullptr)
{
PAL::error("Failed to initialise snmalloc.");
}
#ifdef SNMALLOC_CHECK_CLIENT
// Begin pagemap at random offset within the additionally allocated space.
static_assert(bits::is_pow2(sizeof(T)), "Next line assumes this.");
size_t offset = get_entropy64<PAL>() & (additional_size - sizeof(T));
auto new_body =
reinterpret_cast<T*>(pointer_offset(new_body_untyped, offset));
if constexpr (pal_supports<LazyCommit, PAL>)
{
void* start_page = pointer_align_down<OS_PAGE_SIZE>(new_body);
void* end_page = pointer_align_up<OS_PAGE_SIZE>(
pointer_offset(new_body, REQUIRED_SIZE));
// Only commit readonly memory for this range, if the platform supports
// lazy commit. Otherwise, this would be a lot of memory to have
// mapped.
PAL::notify_using_readonly(
start_page, pointer_diff(start_page, end_page));
}
#else
auto new_body = reinterpret_cast<T*>(new_body_untyped);
#endif
// Ensure bottom page is committed
// ASSUME: new memory is zeroed.
PAL::template notify_using<NoZero>(
pointer_align_down<OS_PAGE_SIZE>(new_body), OS_PAGE_SIZE);
// Set up zero page
new_body[0] = body[0];
body = new_body;
body_opt = new_body;
}