The OpenD Programming Language

pureCalloc

Pure variants of C's memory allocation functions malloc, calloc, and realloc and deallocation function free.

UNIX 98 requires that errno be set to ENOMEM upon failure. Purity is achieved by saving and restoring the value of errno, thus behaving as if it were never changed.

@trusted pure @nogc nothrow
void*
pureCalloc
()
(
size_t nmemb
,
size_t size
)

Examples

ubyte[] fun(size_t n) pure
{
    void* p = pureMalloc(n);
    p !is null || n == 0 || assert(0);
    scope(failure) p = pureRealloc(p, 0);
    p = pureRealloc(p, n *= 2);
    p !is null || n == 0 || assert(0);
    return cast(ubyte[]) p[0 .. n];
}

auto buf = fun(100);
assert(buf.length == 200);
pureFree(buf.ptr);

See Also

D's rules for purity, which allow for memory allocation under specific circumstances.

Meta