Merge pull request #6 from Microsoft/mjp41/refactor

Rename TypeAlloc to Pool
This commit is contained in:
Matthew Parkinson
2019-01-18 12:34:02 +00:00
committed by GitHub
4 changed files with 32 additions and 20 deletions

View File

@@ -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>
@@ -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;
@@ -627,7 +627,7 @@ namespace snmalloc
}
template<class A, class MemProvider>
friend class TypeAlloc;
friend class Pool;
public:
Allocator(

View File

@@ -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)
@@ -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:

View File

@@ -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();
@@ -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

View File

@@ -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;
};
}