1 module lifetime.common;
2 
3 public import lwdr.tracking;
4 
5 extern (C) void _d_delmemory(void* *p)
6 {
7     if (*p)
8     {
9         lwdrInternal_free(*p);
10         *p = null;
11     }
12 }
13 
14 // strip const/immutable/shared/inout from type info
15 inout(TypeInfo) unqualify(inout(TypeInfo) cti) pure nothrow @nogc
16 {
17     TypeInfo ti = cast() cti;
18     while (ti)
19     {
20         // avoid dynamic type casts
21         auto tti = typeid(ti);
22         if (tti is typeid(TypeInfo_Const))
23             ti = (cast(TypeInfo_Const)cast(void*)ti).base;
24         else if (tti is typeid(TypeInfo_Invariant))
25             ti = (cast(TypeInfo_Invariant)cast(void*)ti).base;
26         else if (tti is typeid(TypeInfo_Shared))
27             ti = (cast(TypeInfo_Shared)cast(void*)ti).base;
28         else if (tti is typeid(TypeInfo_Inout))
29             ti = (cast(TypeInfo_Inout)cast(void*)ti).base;
30         else
31             break;
32     }
33     return ti;
34 }
35 
36 // size used to store the TypeInfo at the end of an allocation for structs that have a destructor
37 size_t structTypeInfoSize(const TypeInfo ti) pure nothrow @nogc
38 {
39     if (ti && typeid(ti) is typeid(TypeInfo_Struct)) // avoid a complete dynamic type cast
40     {
41         auto sti = cast(TypeInfo_Struct)cast(void*)ti;
42         if (sti.xdtor)
43             return size_t.sizeof;
44     }
45     return 0;
46 }
47 
48 extern(C) void rt_finalize(void* p, bool det = true, bool resetMemory = true) nothrow @nogc @trusted
49 {
50 	auto ppv = cast(void**)p;
51 	if(!p || !*ppv) return;
52 
53 	auto pc = cast(ClassInfo*)*ppv;
54 	if(det)
55 	{
56 		auto c = *pc;
57 		do
58 		{
59 			if(c.destructor)
60 				(cast(void function(Object) @nogc nothrow)c.destructor)(cast(Object)p);
61 		}
62 		while((c = c.base) !is null);
63 
64 		if(resetMemory)
65 		{
66 			p[0 .. pc.m_init.length] = (*pc).m_init[];
67 		}
68 	}
69 }
70 
71 package void zeroMem(void* ptr, const size_t length) pure nothrow @nogc {
72     ubyte* ptru = cast(ubyte*)ptr;
73     foreach(i; 0 .. length) {
74         ptru[i] = 0;
75 	}
76 }