Skip to content

Commit

Permalink
Cleanup destruct function
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Nov 10, 2024
1 parent fa5ea34 commit 1c48754
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions source/numem/core/memory/alloc.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@ private extern (D) alias fp_t = void function (Object);
@trusted
void destruct(T)(ref T obj_) {

static if (is(T == class)) {
static if (isPointer!T || is(T == class)) {
if (obj_ !is null) {
auto cInfo = cast(ClassInfo)typeid(T);
auto c = cInfo;
if (cInfo) {
auto c = cInfo;

// Call destructors in order of most specific
// to least-specific
do {
if (c.destructor)
(cast(fp_t)c.destructor)(cast(Object)obj_);
} while((c = c.base) !is null);
// Call destructors in order of most specific
// to least-specific
do {
if (c.destructor)
(cast(fp_t)c.destructor)(cast(Object)obj_);
} while((c = c.base) !is null);

} else {

free(cast(void*)obj_);
obj_ = null;
}
} else static if(isPointer!T) {
if (cast(void*)obj_) {

// 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)();
// Item is a struct, we can destruct it directly.
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_);
Expand Down

0 comments on commit 1c48754

Please sign in to comment.