Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/mem/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
#include "largealloc.h"
#include "mediumslab.h"
#include "pagemap.h"
#include "pooled.h"
#include "remoteallocator.h"
#include "sizeclasstable.h"
#include "slab.h"
#include "typeallocated.h"

#include <array>

Expand Down Expand Up @@ -165,7 +165,7 @@ namespace snmalloc
class PageMap = SNMALLOC_DEFAULT_PAGEMAP,
bool IsQueueInline = true>
class Allocator
: public TypeAllocated<Allocator<MemoryProvider, PageMap, IsQueueInline>>
: public Pooled<Allocator<MemoryProvider, PageMap, IsQueueInline>>
{
LargeAlloc<MemoryProvider> large_allocator;
PageMap page_map;
Expand Down Expand Up @@ -627,7 +627,7 @@ namespace snmalloc
}

template<class A, class MemProvider>
friend class TypeAlloc;
friend class Pool;

public:
Allocator(
Expand Down
10 changes: 5 additions & 5 deletions src/mem/globalalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

#include "../ds/helpers.h"
#include "alloc.h"
#include "typealloc.h"
#include "pool.h"

namespace snmalloc
{
template<class MemoryProvider>
class AllocPool : TypeAlloc<Allocator<MemoryProvider>, MemoryProvider>
class AllocPool : Pool<Allocator<MemoryProvider>, MemoryProvider>
{
using Alloc = Allocator<MemoryProvider>;
using Parent = TypeAlloc<Allocator<MemoryProvider>, MemoryProvider>;
using Parent = Pool<Allocator<MemoryProvider>, MemoryProvider>;

public:
static AllocPool* make(MemoryProvider& mp)
Expand All @@ -29,12 +29,12 @@ namespace snmalloc

Alloc* acquire()
{
return Parent::alloc(Parent::memory_provider);
return Parent::acquire(Parent::memory_provider);
}

void release(Alloc* a)
{
Parent::dealloc(a);
Parent::release(a);
}

public:
Expand Down
30 changes: 20 additions & 10 deletions src/mem/typealloc.h → src/mem/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,48 @@

#include "../ds/flaglock.h"
#include "../ds/mpmcstack.h"
#include "typeallocated.h"
#include "pooled.h"

namespace snmalloc
{
/**
* Pool of a particular type of object.
*
* This pool will never return objects to the OS. It maintains a list of all
* objects ever allocated that can be iterated (not concurrency safe). Pooled
* types can be acquired from the pool, and released back to the pool. This is
* concurrency safe.
*
* This is used to bootstrap the allocation of allocators.
**/
template<class T, class MemoryProvider = GlobalVirtual>
class TypeAlloc
class Pool
{
private:
friend TypeAllocated<T>;
friend Pooled<T>;

std::atomic_flag lock = ATOMIC_FLAG_INIT;
MPMCStack<T, PreZeroed> stack;
T* list = nullptr;

TypeAlloc(MemoryProvider& m) : memory_provider(m) {}
Pool(MemoryProvider& m) : memory_provider(m) {}

public:
MemoryProvider& memory_provider;

static TypeAlloc* make(MemoryProvider& memory_provider) noexcept
static Pool* make(MemoryProvider& memory_provider) noexcept
{
auto r = memory_provider.alloc_chunk(sizeof(TypeAlloc));
return new (r) TypeAlloc(memory_provider);
auto r = memory_provider.alloc_chunk(sizeof(Pool));
return new (r) Pool(memory_provider);
}

static TypeAlloc* make() noexcept
static Pool* make() noexcept
{
return make(default_memory_provider);
}

template<typename... Args>
T* alloc(Args&&... args)
T* acquire(Args&&... args)
{
T* p = stack.pop();

Expand All @@ -51,7 +61,7 @@ namespace snmalloc
return p;
}

void dealloc(T* p)
void release(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
Expand Down
6 changes: 4 additions & 2 deletions src/mem/typeallocated.h → src/mem/pooled.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
namespace snmalloc
{
template<class T>
class TypeAllocated
class Pooled
{
private:
template<class TT, class MemoryProvider>
friend class TypeAlloc;
friend class Pool;
template<class TT, Construction c>
friend class MPMCStack;

/// Used by the pool for chaining together entries when not in use.
std::atomic<T*> next = nullptr;
/// Used by the pool to keep the list of all entries ever created.
T* list_next;
};
}