-
-
Notifications
You must be signed in to change notification settings - Fork 700
/
memory.d
58 lines (50 loc) · 1.77 KB
/
memory.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module std.internal.memory;
package(std):
version (D_Exceptions)
{
import core.exception : onOutOfMemoryError;
private enum allocationFailed = `onOutOfMemoryError();`;
}
else
{
private enum allocationFailed = `assert(0, "Memory allocation failed");`;
}
// (below comments are non-DDOC, but are written in similar style)
/+
Mnemonic for `enforce!OutOfMemoryError(malloc(size))` that (unlike malloc)
can be considered pure because it causes the program to abort if the result
of the allocation is null, with the consequence that errno will not be
visibly changed by calling this function. Note that `malloc` can also
return `null` in non-failure situations if given an argument of 0. Hence,
it is a programmer error to use this function if the requested allocation
size is logically permitted to be zero. `enforceCalloc` and `enforceRealloc`
work analogously.
All these functions are usable in `betterC`.
+/
void* enforceMalloc()(size_t size) @nogc nothrow pure @safe
{
auto result = fakePureMalloc(size);
if (!result) mixin(allocationFailed);
return result;
}
// ditto
void* enforceCalloc()(size_t nmemb, size_t size) @nogc nothrow pure @safe
{
auto result = fakePureCalloc(nmemb, size);
if (!result) mixin(allocationFailed);
return result;
}
// ditto
void* enforceRealloc()(return scope void* ptr, size_t size) @nogc nothrow pure @system
{
auto result = fakePureRealloc(ptr, size);
if (!result) mixin(allocationFailed);
return result;
}
// Purified for local use only.
extern (C) @nogc nothrow pure private
{
pragma(mangle, "malloc") void* fakePureMalloc(size_t) @safe;
pragma(mangle, "calloc") void* fakePureCalloc(size_t nmemb, size_t size) @safe;
pragma(mangle, "realloc") void* fakePureRealloc(return scope void* ptr, size_t size) @system;
}