58 lines · 1.8 KB · D
RawDownload
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 resultof the allocation is null, with the consequence that errno will not bevisibly changed by calling this function. Note that `malloc` can alsoreturn `null` in non-failure situations if given an argument of 0. Hence,it is a programmer error to use this function if the requested allocationsize 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;} // dittovoid* enforceCalloc()(size_t nmemb, size_t size) @nogc nothrow pure @safe{    auto result = fakePureCalloc(nmemb, size);    if (!result) mixin(allocationFailed);    return result;} // dittovoid* 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;}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t