PureMallocator

The C heap allocator purified.

TODO Remove when https://github.com/dlang/phobos/pull/6411 has been merged that adds calloc.

Members

Functions

allocate
void[] allocate(size_t bytes)
deallocate
bool deallocate(void[] b)

Standard allocator methods per the semantics defined above. The deallocate and reallocate methods are @system because they may move memory around, leaving dangling pointers in user code. Somewhat paradoxically, malloc is @safe but that's only useful to safe programs that can afford to leak memory allocated.

deallocatePtr
bool deallocatePtr(void* b)

Deallocate using a pointer only like what free does.

reallocate
bool reallocate(void[] b, size_t s)

Standard allocator methods per the semantics defined above. The deallocate and reallocate methods are @system because they may move memory around, leaving dangling pointers in user code. Somewhat paradoxically, malloc is @safe but that's only useful to safe programs that can afford to leak memory allocated.

Static variables

instance
PureMallocator instance;

Returns the global instance of this allocator type. The C heap allocator is thread-safe, therefore all of its methods and it itself are x.

Variables

alignment
enum uint alignment;

* The alignment is a static constant equal to platformAlignment, which * ensures proper alignment for any D data type.

Examples

auto buf = PureMallocator.instance.allocate(16);
assert(&buf[0]);
assert(buf.length);

assert(PureMallocator.instance.deallocate(buf));

import std.experimental.allocator : makeArray;
PureMallocator.instance.makeArray!int(64, 42);

Meta