Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/core/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,103 @@ struct GC
gc_runFinalizers( segment );
}
}

/**
* Pure variants of C's memory allocation functions `malloc`, `calloc` and
* `realloc`. Purity is achieved via resetting the `errno` to it's value prior
* to being called, removing the function's global state mutation.
*
* See_Also:
* $(LINK2 https://dlang.org/spec/function.html#pure-functions, D's rules for purity),
* which allow for memory allocation under specific circumstances.
*/
void* pureMalloc(size_t size) @trusted pure @nogc nothrow
{
const errno = fakePureGetErrno();
void* ret = fakePureMalloc(size);
if (!ret || errno != 0)
{
cast(void)fakePureSetErrno(errno);
}
return ret;
}
/// ditto
void* pureCalloc(size_t nmemb, size_t size) @trusted pure @nogc nothrow
{
const errno = fakePureGetErrno();
void* ret = fakePureCalloc(nmemb, size);
if (!ret || errno != 0)
{
cast(void)fakePureSetErrno(errno);
}
return ret;
}
/// ditto
void* pureRealloc(void* ptr, size_t size) pure @nogc nothrow
{
const errno = fakePureGetErrno();
void* ret = fakePureRealloc(ptr, size);
if (!ret || errno != 0)
{
cast(void)fakePureSetErrno(errno);
}
return ret;
}

///
nothrow @nogc unittest
{
import core.stdc.stdlib : free;

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);
free(buf.ptr);
}

pure @nogc nothrow unittest
{
const int errno = fakePureGetErrno();

void* x = pureMalloc(10); // normal allocation
assert(errno == fakePureGetErrno()); // errno shouldn't change
assert(x !is null); // allocation should succeed

x = pureRealloc(x, 10); // normal reallocation
assert(errno == fakePureGetErrno()); // errno shouldn't change
assert(x !is null); // allocation should succeed

fakePureFree(x);

void* y = pureCalloc(10, 1); // normal zeroed allocation
assert(errno == fakePureGetErrno()); // errno shouldn't change
assert(y !is null); // allocation should succeed

fakePureFree(y);

void* z = pureMalloc(size_t.max); // won't affect `errno`
assert(errno == fakePureGetErrno()); // errno shouldn't change
assert(z is null);
}

// locally purified for internal use here only
extern (C) private pure @system @nogc nothrow
{
pragma(mangle, "getErrno") int fakePureGetErrno();
pragma(mangle, "setErrno") int fakePureSetErrno(int);

pragma(mangle, "malloc") void* fakePureMalloc(size_t);
pragma(mangle, "calloc") void* fakePureCalloc(size_t nmemb, size_t size);
pragma(mangle, "realloc") void* fakePureRealloc(void* ptr, size_t size);

pragma(mangle, "free") void fakePureFree(void* ptr); // needed by unittests
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add one unittest to cover all, or at best short ddoc unittests showing the use of each inside a pure function