Honeycomb  0.1
Component-Model Framework
FreeList.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Memory/Pool.h"
5 
6 namespace honey { namespace lockfree
7 {
8 
10 
15 template<class T>
16 class FreeList : mt::NoCopy
17 {
18  template<class> friend class FreeListAllocator;
19 public:
20  typedef T value_type;
23 
24  FreeList(szt capacity = 0) : _pool({make_tuple(sizeof(T), capacity)}) {}
25 
27  void reserve(szt capacity) { _pool._buckets[0]->reserve(capacity); }
29  szt capacity() const { return _pool._buckets[0]->_blockCount; }
30 
32  T* alloc() { return static_cast<T*>(_pool.alloc(sizeof(T))); }
34  template<class... Args>
35  T* construct(Args&&... args) { return new (alloc()) T{forward<Args>(args)...}; }
36 
38  void free(T* ptr) { assert(ptr); _pool.free(ptr); }
40  void destroy(T* ptr) { assert(ptr); ptr->~T(); free(ptr); }
41 
43  Handle handle(T* ptr) const { return ptr ? MemPool::Bucket::blockHeader(reinterpret_cast<uint8*>(ptr))->handle : nullptr; }
45  T* deref(Handle handle) const { return reinterpret_cast<T*>(MemPool::Bucket::blockData(_pool._buckets[0]->deref(handle))); }
46 
47 private:
48  MemPool _pool;
49 };
50 
51 template<class T>
52 class FreeListAllocator : public MemPoolAllocator<FreeListAllocator, T>
53 {
54 public:
55  FreeListAllocator() = default;
56  template<class U>
58 
59  MemPool& pool() { return _freeList._pool; }
60 
61 private:
62  FreeList<T> _freeList;
63 };
64 
65 } }
Enables blocks to be referenced via index rather than pointer so that they can include a tag while st...
Definition: Pool.h:75
FreeList(szt capacity=0)
Definition: FreeList.h:24
Holds block handle and tag to prevent lock-free ABA issues.
Definition: Pool.h:95
Node * construct(Args &&...args)
Construct object and remove from free list.
Definition: FreeList.h:35
void reserve(szt capacity)
Ensure that enough storage is allocated for a number of objects.
Definition: FreeList.h:27
void free(void *ptr_)
Free a memory block allocated from the pool.
Definition: Pool.cpp:261
void free(Node *ptr)
Add object to free list without destroying it.
Definition: FreeList.h:38
MemPool::Bucket::TaggedHandle TaggedHandle
Definition: FreeList.h:22
Definition: FreeList.h:52
Lock-free freelist, allocates re-usable objects and provides automatic storage expansion for concurre...
Definition: Pool.h:10
void * alloc(szt size, uint8 align=1, const char *srcFile=nullptr, int srcLine=0)
Allocate a size bytes block of memory at byte boundary align. alignment must be a power of two...
Definition: Pool.cpp:249
FreeListAllocator(const FreeListAllocator< U > &)
Definition: FreeList.h:57
friend class FreeListAllocator
Definition: FreeList.h:18
Node value_type
Definition: FreeList.h:20
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
MemPool allocator.
Definition: Pool.h:288
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
void destroy(Node *ptr)
Destroy object and add to free list.
Definition: FreeList.h:40
Memory pool.
Definition: Pool.h:29
Node * alloc()
Remove object from free list without constructing it.
Definition: FreeList.h:32
szt capacity() const
The number of objects for which storage is allocated.
Definition: FreeList.h:29
Node * deref(Handle handle) const
Get object from compressed handle.
Definition: FreeList.h:45
MemPool::Bucket::Handle Handle
Definition: FreeList.h:21
Handle handle(Node *ptr) const
Get compressed handle for object.
Definition: FreeList.h:43
Global Honeycomb namespace.
MemPool & pool()
Definition: FreeList.h:59