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:
committed by
GitHub
parent
18d7cc99b6
commit
f0e2ab702a
@@ -17,39 +17,30 @@ namespace snmalloc
|
||||
*
|
||||
* This is used to bootstrap the allocation of allocators.
|
||||
*/
|
||||
template<class T, class MemoryProvider = GlobalVirtual>
|
||||
class Pool
|
||||
template<class T>
|
||||
class PoolState
|
||||
{
|
||||
private:
|
||||
friend Pooled<T>;
|
||||
template<SNMALLOC_CONCEPT(ConceptPAL) PAL, typename ArenaMap>
|
||||
friend class MemoryProviderStateMixin;
|
||||
friend SNMALLOC_DEFAULT_MEMORY_PROVIDER;
|
||||
template<typename TT>
|
||||
friend class Pool;
|
||||
|
||||
private:
|
||||
std::atomic_flag lock = ATOMIC_FLAG_INIT;
|
||||
MPMCStack<T, PreZeroed> stack;
|
||||
T* list = nullptr;
|
||||
|
||||
Pool(MemoryProvider& m) : memory_provider(m) {}
|
||||
T* list{nullptr};
|
||||
|
||||
public:
|
||||
MemoryProvider& memory_provider;
|
||||
constexpr PoolState() = default;
|
||||
};
|
||||
|
||||
static Pool* make(MemoryProvider& memory_provider) noexcept
|
||||
template<typename T>
|
||||
class Pool
|
||||
{
|
||||
public:
|
||||
template<typename SharedStateHandle, typename... Args>
|
||||
static T* acquire(SharedStateHandle h, Args&&... args)
|
||||
{
|
||||
return memory_provider.template alloc_chunk<Pool, 0, MemoryProvider&>(
|
||||
memory_provider);
|
||||
}
|
||||
|
||||
static Pool* make() noexcept
|
||||
{
|
||||
return Pool::make(default_memory_provider());
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
T* acquire(Args&&... args)
|
||||
{
|
||||
T* p = stack.pop();
|
||||
PoolState<T>& pool = h.pool();
|
||||
T* p = pool.stack.pop();
|
||||
|
||||
if (p != nullptr)
|
||||
{
|
||||
@@ -57,13 +48,12 @@ namespace snmalloc
|
||||
return p;
|
||||
}
|
||||
|
||||
p = memory_provider
|
||||
.template alloc_chunk<T, bits::next_pow2_const(sizeof(T))>(
|
||||
std::forward<Args...>(args)...);
|
||||
p = ChunkAllocator::alloc_meta_data<T>(
|
||||
h, nullptr, std::forward<Args>(args)...);
|
||||
|
||||
FlagLock f(lock);
|
||||
p->list_next = list;
|
||||
list = p;
|
||||
FlagLock f(pool.lock);
|
||||
p->list_next = pool.list;
|
||||
pool.list = p;
|
||||
|
||||
p->set_in_use();
|
||||
return p;
|
||||
@@ -74,20 +64,22 @@ namespace snmalloc
|
||||
*
|
||||
* Do not return objects from `extract`.
|
||||
*/
|
||||
void release(T* p)
|
||||
template<typename SharedStateHandle>
|
||||
static void release(SharedStateHandle h, T* p)
|
||||
{
|
||||
// The object's destructor is not run. If the object is "reallocated", it
|
||||
// is returned without the constructor being run, so the object is reused
|
||||
// without re-initialisation.
|
||||
p->reset_in_use();
|
||||
stack.push(p);
|
||||
h.pool().stack.push(p);
|
||||
}
|
||||
|
||||
T* extract(T* p = nullptr)
|
||||
template<typename SharedStateHandle>
|
||||
static T* extract(SharedStateHandle h, T* p = nullptr)
|
||||
{
|
||||
// Returns a linked list of all objects in the stack, emptying the stack.
|
||||
if (p == nullptr)
|
||||
return stack.pop_all();
|
||||
return h.pool().stack.pop_all();
|
||||
|
||||
return p->next;
|
||||
}
|
||||
@@ -97,17 +89,19 @@ namespace snmalloc
|
||||
*
|
||||
* Do not return objects from `acquire`.
|
||||
*/
|
||||
void restore(T* first, T* last)
|
||||
template<typename SharedStateHandle>
|
||||
static void restore(SharedStateHandle h, T* first, T* last)
|
||||
{
|
||||
// Pushes a linked list of objects onto the stack. Use to put a linked
|
||||
// list returned by extract back onto the stack.
|
||||
stack.push(first, last);
|
||||
h.pool().stack.push(first, last);
|
||||
}
|
||||
|
||||
T* iterate(T* p = nullptr)
|
||||
template<typename SharedStateHandle>
|
||||
static T* iterate(SharedStateHandle h, T* p = nullptr)
|
||||
{
|
||||
if (p == nullptr)
|
||||
return list;
|
||||
return h.pool().list;
|
||||
|
||||
return p->list_next;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user