Major refactor of snmalloc (#343)

# Pagemap
 
The Pagemap now stores all the meta-data for the object allocation. The meta-data in the pagemap is effectively a triple of the sizeclass, the remote allocator, and a pointer to a 64 byte block of meta-data for this chunk of memory. By storing the pointer to a block, it allows the pagemap to handle multiple slab sizes without branching on the fast path. There is one entry in the pagemap per 16KiB of address space, but by using the same entry in the pagemap for 4 adjacent entries, then we can treat a 64KiB range can be treated as a single slab of allocations.

This change also means there is almost no capability amplification required by the implementation on CHERI for finding meta-data. The only amplification is required, when we change the way a chunk is used to a size of object allocation.


# Backend

There is a second major aspect of the refactor that there is now a narrow API that abstracts the Pagemap, PAL and address space management. This should better enable the compartmentalisation and makes it easier to produce alternative backends for various research directions. This is a template parameter that can be used to specialised by the front-end in different ways.

# Thread local state

The thread local state has been refactored into two components, one (called 'localalloc') that is stored directly in the TLS and is constant initialised, and one that is allocated in the address space (called 'coreallloc') which is lazily created and pooled.

# Difference

This removes Superslabs/Medium slabs as there meta-data is now part of the pagemap.
This commit is contained in:
Matthew Parkinson
2021-07-12 15:53:36 +01:00
committed by GitHub
parent 18d7cc99b6
commit f0e2ab702a
83 changed files with 4404 additions and 5769 deletions

View File

@@ -61,8 +61,7 @@ namespace snmalloc
DefaultPal;
#endif
[[noreturn]] SNMALLOC_SLOW_PATH inline SNMALLOC_COLD void
error(const char* const str)
[[noreturn]] SNMALLOC_SLOW_PATH inline void error(const char* const str)
{
Pal::error(str);
}
@@ -77,16 +76,16 @@ namespace snmalloc
* disruption to PALs for platforms that do not support StrictProvenance AALs.
*/
template<typename PAL = Pal, typename AAL = Aal, typename T, capptr_bounds B>
static SNMALLOC_FAST_PATH typename std::enable_if_t<
static inline typename std::enable_if_t<
!aal_supports<StrictProvenance, AAL>,
CapPtr<T, capptr_export_type<B>()>>
capptr_export(CapPtr<T, B> p)
{
return CapPtr<T, capptr_export_type<B>()>(p.unsafe_capptr);
return CapPtr<T, capptr_export_type<B>()>(p.unsafe_ptr());
}
template<typename PAL = Pal, typename AAL = Aal, typename T, capptr_bounds B>
static SNMALLOC_FAST_PATH typename std::enable_if_t<
static inline typename std::enable_if_t<
aal_supports<StrictProvenance, AAL>,
CapPtr<T, capptr_export_type<B>()>>
capptr_export(CapPtr<T, B> p)
@@ -107,7 +106,7 @@ namespace snmalloc
{
static_assert(
!page_aligned || B == CBArena || B == CBChunkD || B == CBChunk);
PAL::template zero<page_aligned>(p.unsafe_capptr, sz);
PAL::template zero<page_aligned>(p.unsafe_ptr(), sz);
}
static_assert(

View File

@@ -3,6 +3,9 @@
#pragma once
#include "../aal/aal.h"
#include "pal_consts.h"
#include <cstring>
namespace snmalloc

View File

@@ -1,5 +1,8 @@
#pragma once
#ifdef SNMALLOC_TRACING
# include <iostream>
#endif
#include "../ds/address.h"
#if defined(BACKTRACE_HEADER)
# include BACKTRACE_HEADER
@@ -255,7 +258,7 @@ namespace snmalloc
// Magic number for over-allocating chosen by the Pal
// These should be further refined based on experiments.
constexpr size_t min_size =
bits::is64() ? bits::one_at_bit(32) : bits::one_at_bit(28);
bits::is64() ? bits::one_at_bit(31) : bits::one_at_bit(27);
for (size_t size_request = bits::max(size, min_size);
size_request >= size;
@@ -270,7 +273,13 @@ namespace snmalloc
0);
if (p != MAP_FAILED)
{
#ifdef SNMALLOC_TRACING
std::cout << "Pal_posix reserved: " << p << " (" << size_request
<< ")" << std::endl;
#endif
return {p, size_request};
}
}
OS::error("Out of memory");