10 namespace lockfree {
template<
class>
class FreeList; }
39 MemPool(
const vector<tuple<szt,szt>>& buckets,
const Id&
id =
idnull,
szt align =
alignof(
double));
42 void*
alloc(
szt size,
uint8 align = 1,
const char* srcFile =
nullptr,
int srcLine = 0);
44 void free(
void* ptr_);
66 const Id&
id()
const {
return _id; }
86 explicit operator bool()
const {
return index !=
Int(-1); }
131 void validate(
int sig) {
assert(
debug.
sig == sig,
"Error: invalid block signature. Block overwritten by overflow or in unexpected state (eg. freed twice)."); }
147 static uint8* blockData(BlockHeader* header) {
return reinterpret_cast<uint8*
>(header) +
sizeof(BlockHeader); }
152 _blockSize(blockSize),
153 _blockCountInit(blockCount),
157 _freeHead(TaggedHandle()),
166 for (
auto i :
range(1, _chunkCount.load()))
delete_(_chunks[i].data());
170 void initChunk(
uint8* chunk,
szt chunkSize,
szt blockCount);
174 void reserve(
szt capacity);
178 void free(BlockHeader* header);
180 szt blockOffsetMax()
const {
return _pool._blockAlign-1; }
181 szt blockStride()
const {
return (
szt)(intptr_t)
alignCeil((
void*)(_blockSize +
sizeof(BlockHeader)), _pool._blockAlign); }
184 BlockHeader* deref(Handle handle)
const
186 assert(handle && handle.chunk() < _chunkCount);
187 auto& chunk = _chunks[handle.chunk()];
188 assert(blockStride()*handle.block() < chunk.size());
189 uint8* blockData =
alignCeil(chunk.data() +
sizeof(BlockHeader), _pool._blockAlign);
190 return blockHeader(blockData + blockStride()*handle.block());
195 const szt _blockSize;
196 const szt _blockCountInit;
198 array<Buffer<uint8>, numeral<uint64>().sizeBits> _chunks;
199 Atomic<uint8> _chunkCount;
200 Atomic<szt> _chunkSizeTotal;
201 Atomic<TaggedHandle> _freeHead;
202 Atomic<szt> _freeCount;
203 TaggedHandle _usedHead;
204 Atomic<szt> _usedCount;
230 void validate(
int sig) {
assert(
debug.
sig == sig,
"Error: invalid block signature. Block overwritten by overflow or in unexpected state (eg. freed twice)."); }
246 static uint8* blockData(BlockHeader* header) {
return reinterpret_cast<uint8*
>(header) +
sizeof(BlockHeader); }
258 void free(BlockHeader* header);
261 Atomic<szt> _allocTotal;
262 BlockHeader* _usedHead;
263 Atomic<szt> _usedCount;
273 const szt _blockAlign;
275 vector<UniquePtr<Bucket>> _buckets;
276 std::map<szt, Bucket*> _bucketMap;
277 UniquePtr<uint8> _bucketChunk;
278 UniquePtr<Heap> _heap;
287 template<
template<
class>
class Subclass,
class T>
297 {
return static_cast<pointer>(this->
subc().pool().alloc(
sizeof(T)*n, 1, srcFile, srcLine)); }
pointer allocate(size_type n, const char *srcFile, int srcLine, const void *=0)
Definition: Pool.h:296
void validate() const
Ensure that all used/free blocks are valid (check signatures)
Definition: Pool.cpp:308
Enables blocks to be referenced via index rather than pointer so that they can include a tag while st...
Definition: Pool.h:75
Holds block handle and tag to prevent lock-free ABA issues.
Definition: Pool.h:95
T * alignCeil(T *p, szt bytes)
Align a pointer to the next byte boundary bytes. Does nothing if p is already on boundary. Alignment must be a power of two.
Definition: Allocator.h:43
Int index
Definition: Pool.h:91
bool operator==(TaggedHandle rhs) const
Definition: Pool.h:102
const Handle & handle() const
Definition: Pool.h:105
uint8 chunk() const
Definition: Pool.h:88
void free(void *ptr_)
Free a memory block allocated from the pool.
Definition: Pool.cpp:261
String printStats() const
Print statistics about pool.
Definition: Pool.cpp:326
MemPool(const vector< tuple< szt, szt >> &buckets, const Id &id=idnull, szt align=alignof(double))
Definition: Pool.cpp:209
Inherit to declare that class is not copyable.
Definition: Meta.h:286
void delete_(T *&p)
Destruct object, free memory and set pointer to null.
Definition: Allocator.h:75
Handle(nullptr_t)
Definition: Pool.h:81
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
T * pointer
Definition: Allocator.h:104
std::enable_if< mt::isIterator< Iter1 >::value, Range_< Iter1, Iter2 > >::type range(Iter1 &&first, Iter2 &&last)
Range from iterators [first, last)
Definition: Range.h:116
Int block() const
Definition: Pool.h:89
Subclass< T > & subc()
Definition: Allocator.h:123
TaggedHandle(Handle handle, Int tag)
Definition: Pool.h:98
szt allocBytes() const
Calc total bytes allocated by pool.
Definition: Pool.cpp:278
unsigned char uint8
Definition: Core.h:12
szt size_type
Definition: Allocator.h:108
Handle & handle()
Definition: Pool.h:106
std::allocator compatible allocator
Definition: Allocator.h:100
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
void deallocate(pointer p, size_type)
Definition: Pool.h:298
#define idnull
Null id.
Definition: Id.h:124
MemPool allocator.
Definition: Pool.h:288
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
Handle()
Definition: Pool.h:80
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
Memory pool.
Definition: Pool.h:29
szt usedBytes() const
Calc total bytes used in pool.
Definition: Pool.cpp:286
bool operator!=(TaggedHandle rhs) const
Definition: Pool.h:103
bool operator!=(Handle rhs) const
Definition: Pool.h:85
String printUsed() const
Print all used blocks.
Definition: Pool.cpp:391
int size(const StdContainer &cont)
Safely get the size of a std container as a signed integer.
Definition: StdUtil.h:19
Int nextTag() const
Definition: Pool.h:107
TaggedHandle()
Definition: Pool.h:97
Int tag
Definition: Pool.h:109
pointer allocate(size_type n, const void *=0)
Definition: Pool.h:295
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
Handle(uint8 chunk, Int block)
Definition: Pool.h:82
platform::SwapMaxType SwapMaxType
Largest atomically-swappable type.
Definition: Atomic.h:105
Global Honeycomb namespace.
const Id & id() const
Definition: Pool.h:66
mt::uintBySize< sizeof(atomic::SwapMaxType)/2 >::type Int
Definition: Pool.h:78
bool operator==(Handle rhs) const
Definition: Pool.h:84
TaggedHandle & operator=(Handle rhs)
Definition: Pool.h:100
szt freeBytes() const
Calc total bytes free in pool.
Definition: Pool.h:51