DynamicPool

Memory pool implemented ower linked list of allocated blocks that are being reused. If there is no preallocated block in the pool, it allocates the new one from the heap. When block is returned to the pool, it's just prepended at the start of the unused blocks to be available again.

Unused blocks are freed only when clear() is called on the pool or when pool itself is cleared out by ROI.

Constructors

this
this(typeof(this) rhs)

Copy constructor

Destructor

~this
~this()

Destructor

Members

Functions

alloc
void* alloc(size_t len)

Returns memory block of the request size.

alloc
T* alloc(ARGS args)

Allocates requested type over pooled memory block and returns it. onOutOfMemoryError is called when new memory block fails to be allocated.

capacity
size_t capacity()

Available number of preallocated blocks

clear
void clear()

Clears all currently preallocated memory blocks.

dealloc
void dealloc(void* ptr)
void dealloc(T* p)

Returns block of memory back to the pool. Deallocating pointer to a memory not returned by the pool has undefined behavior.

Examples

DynamicPool!1024 pool; // each block is 1024B large
auto n = pool.alloc!int(42); // allocates whole 1024B block for just an 4B large number
assert(*n == 42);

auto buf = pool.alloc!(ubyte[1024])(); // uses whole block and zeroes the array
foreach (i; 0..1024) assert((*buf)[i] == 0);

void* vbuf = pool.alloc(1024); // uses whole block that we can use as we please - block memory is uninitialized
assert(vbuf !is null);

// FixedPool over DynamicPool memory block
import mempooled.fixed : fixedPool;
auto fpblock = cast(ubyte*)pool.alloc(1024);
auto fpool = fixedPool!(8, 128)(fpblock[0..1024]);
auto x = fpool.alloc!int(666);
assert(*x == 666);
fpool.dealloc(x);

assert(pool.pay.numUsedBlocks == 4);
pool.dealloc(n);
pool.dealloc(buf);
pool.dealloc(vbuf);
pool.dealloc(fpblock);

Meta