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
80
src/mem/scopedalloc.h
Normal file
80
src/mem/scopedalloc.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* This header requires that Alloc has been defined.
|
||||
*/
|
||||
|
||||
namespace snmalloc
|
||||
{
|
||||
/**
|
||||
* RAII wrapper around an `Alloc`. This class gets an allocator from the
|
||||
* global pool and wraps it so that `Alloc` methods can be called
|
||||
* directly via the `->` operator on this class. When this object is
|
||||
* destroyed, it returns the allocator to the global pool.
|
||||
*
|
||||
* This does not depend on thread-local storage working, so can be used for
|
||||
* bootstrapping.
|
||||
*/
|
||||
struct ScopedAllocator
|
||||
{
|
||||
/**
|
||||
* The allocator that this wrapper will use.
|
||||
*/
|
||||
Alloc alloc;
|
||||
|
||||
/**
|
||||
* Constructor. Claims an allocator from the global pool
|
||||
*/
|
||||
ScopedAllocator() = default;
|
||||
|
||||
/**
|
||||
* Copying is not supported, it could easily lead to accidental sharing of
|
||||
* allocators.
|
||||
*/
|
||||
ScopedAllocator(const ScopedAllocator&) = delete;
|
||||
|
||||
/**
|
||||
* Moving is not supported, though it would be easy to add if there's a use
|
||||
* case for it.
|
||||
*/
|
||||
ScopedAllocator(ScopedAllocator&&) = delete;
|
||||
|
||||
/**
|
||||
* Copying is not supported, it could easily lead to accidental sharing of
|
||||
* allocators.
|
||||
*/
|
||||
ScopedAllocator& operator=(const ScopedAllocator&) = delete;
|
||||
|
||||
/**
|
||||
* Moving is not supported, though it would be easy to add if there's a use
|
||||
* case for it.
|
||||
*/
|
||||
ScopedAllocator& operator=(ScopedAllocator&&) = delete;
|
||||
|
||||
/**
|
||||
* Destructor. Returns the allocator to the pool.
|
||||
*/
|
||||
~ScopedAllocator()
|
||||
{
|
||||
alloc.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Arrow operator, allows methods exposed by `Alloc` to be called on the
|
||||
* wrapper.
|
||||
*/
|
||||
Alloc* operator->()
|
||||
{
|
||||
return &alloc;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Returns a new scoped allocator. When the `ScopedAllocator` goes out of
|
||||
* scope, the underlying `Alloc` will be returned to the pool.
|
||||
*/
|
||||
inline ScopedAllocator get_scoped_allocator()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
} // namespace snmalloc
|
||||
Reference in New Issue
Block a user