1
0
forked from Alepha/Alepha

Blob based per-thread slab allocator

This permits "stateless" allocators which grab memory from a
`thread_local Alepha::Blob` instance.  Each allocation
sticks a malloc cookie of type
`std::shared_ptr< Alepha::Blob::StorageReservation >`
just before the base of the allocation.

The allocator object knows that it needs to `reinterpret_cast`
the malloc cookie into a shared pointer and run its destructor.
This causes the Blob's underlying reference counted allocation
to be tied to the lifetime of the allocated memory.  The intent
is to permit cheap allocation in one thread and deallocation
in another.  Each deallocation should be a single atomic
dereference operation.  Each allocation should be (usually) a
bit of pointer arithmetic and a single atomic increment operation.

This, hopefully, eliminates significant thread contention for
the global allocation mechanism between various threads in
an intensive multithreaded situation where each processing
thread thread may independently retire data objects allocated by
a single source.
This commit is contained in:
2024-09-05 17:06:52 -04:00
parent d4dfe9f90f
commit 6c165b1603
6 changed files with 191 additions and 3 deletions

View File

@ -17,6 +17,8 @@ static_assert( __cplusplus > 2020'99 );
#include "Buffer.h"
// TODO: Put this into the `Alepha::Memory::` namespace.
// TODO: Consider whether a "republish" into `Alepha::` namespace is a good idea.
namespace Alepha::Hydrogen ::detail:: Blob_m
{
inline namespace exports
@ -94,6 +96,9 @@ namespace Alepha::Hydrogen ::detail:: Blob_m
public:
~Blob() { reset(); }
using StorageReservation= IndirectStorage;
const StorageReservation &reservation() const { return storage; }
auto
swap_lens() noexcept
{
@ -218,7 +223,10 @@ namespace Alepha::Hydrogen ::detail:: Blob_m
* inside a large single physical backing. This helps maintain zero-copy semantics.
*
* @param amount The amount of data to carve off.
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes.
* @param alignment The size alignment that the new base should be at (the extra padding is
* considered part of the resulting `Blob`).
* @return A new `Blob` object referring to the same physical data, scoped to `amount` bytes (with
* possible extra space, due to alignment).
*/
Blob
carveHead( const std::size_t amount )