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