Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit eedd39f

Browse files
committed
Start to use object.destroy + GC.addrOf
1 parent 78508ef commit eedd39f

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

src/core/memory.d

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -944,8 +944,8 @@ performs the following actions in sequence:
944944
$(UL
945945
$(LI
946946
Calls `.object.destroy(x)` (if `x` is a class or interface object) or
947-
`typeid(*x).destroy(x)` (if `x` is pointer to a struct) to destroy the referred entity.
948-
Arrays of structs call `typeid(*x).destroy(x)` for each element.
947+
`.object.destroy(*x)` (if `x` is pointer to a struct) to destroy the referred entity.
948+
Arrays of structs call `.object.destroy(x)` for each element in the array.
949949
)
950950
$(LI
951951
Frees the memory allocated for `x`. If `x` is a reference to a class
@@ -971,11 +971,11 @@ void __delete(T)(ref T x) @system
971971
// See also: https://github.com/dlang/dmd/blob/v2.078.0/src/dmd/e2ir.d#L3886
972972
static if (is(T == interface))
973973
{
974-
(cast(Object) x).destroy;
974+
.object.destroy(x);
975975
}
976976
else static if (is(T == class))
977977
{
978-
(cast(Object) x).destroy;
978+
.object.destroy(x);
979979
}
980980
else static if (is(T == U*, U))
981981
{
@@ -995,7 +995,12 @@ void __delete(T)(ref T x) @system
995995
static assert(0, "It is not possible to delete: `" ~ T.stringof ~ "`");
996996
}
997997

998-
static if (is(T == interface) || is(T == class) || is(T == U2*, U2) || is(T : E2[], E2))
998+
static if (is(T == interface) || is(T == class))
999+
{
1000+
GC.free(cast(void*) x);
1001+
x = null;
1002+
}
1003+
else static if (is(T == U2*, U2) || is(T : E2[], E2))
9991004
{
10001005
GC.free(&x);
10011006
x = null;
@@ -1018,11 +1023,14 @@ unittest
10181023
B a = b;
10191024
b.test = 10;
10201025

1026+
assert(GC.addrOf(cast(void*) b) != null);
10211027
__delete(b);
10221028
assert(b is null);
10231029
assert(dtorCalled);
1030+
assert(GC.addrOf(cast(void*) b) == null);
10241031
// but be careful, a still points to it
10251032
assert(a !is null);
1033+
assert(GC.addrOf(cast(void*) a) !is null);
10261034
}
10271035

10281036
/// Deleting interfaces
@@ -1049,9 +1057,11 @@ unittest
10491057
A a = new B();
10501058
a.quack();
10511059

1060+
assert(GC.addrOf(cast(void*) a) != null);
10521061
__delete(a);
10531062
assert(a is null);
10541063
assert(dtorCalled);
1064+
assert(GC.addrOf(cast(void*) a) == null);
10551065
}
10561066

10571067
/// Deleting structs
@@ -1067,39 +1077,27 @@ unittest
10671077
}
10681078
}
10691079
auto a = new A("foo");
1070-
__delete(a);
10711080

1081+
assert(GC.addrOf(cast(void*) a) != null);
1082+
__delete(a);
10721083
assert(a is null);
10731084
assert(dtorCalled);
1085+
assert(GC.addrOf(cast(void*) a) == null);
10741086
}
10751087

10761088
/// Deleting arrays
10771089
unittest
10781090
{
10791091
int[] a = [1, 2, 3];
10801092
auto b = a;
1081-
__delete(b);
1082-
assert(b is null);
1083-
}
10841093

1085-
/// Deleting pointers
1086-
unittest
1087-
{
1088-
bool dtorCalled;
1089-
class A
1090-
{
1091-
string test;
1092-
~this()
1093-
{
1094-
dtorCalled = true;
1095-
}
1096-
}
1097-
A a = new A();
1098-
a.test = "exists";
1099-
auto b = &a;
1094+
assert(GC.addrOf(b.ptr) != null);
11001095
__delete(b);
11011096
assert(b is null);
1102-
assert(!dtorCalled);
1097+
assert(GC.addrOf(b.ptr) == null);
1098+
// but be careful, a still points to it
1099+
assert(a !is null);
1100+
assert(GC.addrOf(a.ptr) !is null);
11031101
}
11041102

11051103
/// Deleting arrays of structs
@@ -1115,17 +1113,22 @@ unittest
11151113
}
11161114
}
11171115
auto arr = [A(1), A(2), A(3)];
1116+
1117+
assert(GC.addrOf(arr.ptr) != null);
11181118
__delete(arr);
11191119
assert(dtorCalled == 3);
1120+
assert(GC.addrOf(arr.ptr) == null);
11201121
}
11211122

11221123
// Deleting raw memory
11231124
unittest
11241125
{
11251126
import core.memory : GC;
11261127
auto a = GC.malloc(5);
1128+
assert(GC.addrOf(cast(void*) a) != null);
11271129
__delete(a);
11281130
assert(a is null);
1131+
assert(GC.addrOf(cast(void*) a) == null);
11291132
}
11301133

11311134
// __delete returns with no effect if x is null

0 commit comments

Comments
 (0)