-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f696d5c
commit 668b6a9
Showing
6 changed files
with
247 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module numem.core.memory.alloc; | ||
public import core.stdc.stdlib : free, malloc, exit; | ||
import std.traits; | ||
|
||
@nogc nothrow: | ||
|
||
/** | ||
Destroy element with a destructor. | ||
*/ | ||
@trusted | ||
void destruct(T)(ref T obj_) { | ||
|
||
static if(isPointer!T || is(T == class)) { | ||
if (cast(void*)obj_) { | ||
|
||
// Don't try to run dtor on null object. | ||
static if (__traits(hasMember, T, "__dtor")) { | ||
assumeNothrowNoGC!(typeof(&obj_.__dtor))(&obj_.__dtor)(); | ||
} else static if (__traits(hasMember, T, "__xdtor")) { | ||
assumeNothrowNoGC!(typeof(&obj_.__xdtor))(&obj_.__xdtor)(); | ||
} | ||
|
||
free(cast(void*)obj_); | ||
obj_ = null; | ||
} | ||
} else { | ||
|
||
// Item is a struct, we can destruct it. | ||
static if (__traits(hasMember, T, "__dtor")) { | ||
assumeNothrowNoGC!(typeof(&obj_.__dtor))(&obj_.__dtor)(); | ||
} else static if (__traits(hasMember, T, "__xdtor")) { | ||
assumeNothrowNoGC!(typeof(&obj_.__xdtor))(&obj_.__xdtor)(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Forces a function to assume that it's nogc compatible. | ||
*/ | ||
auto assumeNoGC(T) (T t) { | ||
static if (isFunctionPointer!T || isDelegate!T) { | ||
enum attrs = functionAttributes!T | FunctionAttribute.nogc; | ||
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; | ||
} else static assert(false); | ||
} | ||
|
||
/** | ||
Forces a function to assume that it's nothrow nogc compatible. | ||
*/ | ||
auto assumeNothrowNoGC(T) (T t) { | ||
static if (isFunctionPointer!T || isDelegate!T) { | ||
enum attrs = functionAttributes!T | FunctionAttribute.nogc | FunctionAttribute.nothrow_; | ||
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t; | ||
} else static assert(false); | ||
} |
Oops, something went wrong.