Add makeGCSafe/disposeGCSafe as GC safe variants of make/dispose.

If necessary, these will call GC.addRange/GC.removeRange to avoid dangling GC references.
This commit is contained in:
Sönke Ludwig 2017-07-20 13:34:12 +02:00
parent 4bccf6fcb5
commit cfb4f83113

View file

@ -16,3 +16,26 @@ public import std.experimental.allocator.mallocator;
s_threadAllocator = () @trusted { return allocatorObject(GCAllocator.instance); } ();
return s_threadAllocator;
}
auto makeGCSafe(T, Allocator, A...)(Allocator allocator, A args)
{
import core.memory : GC;
import std.traits : hasIndirections;
auto ret = allocator.make!T(args);
static if (is (T == class)) enum tsize = __traits(classInstanceSize, T);
else enum tsize = T.sizeof;
static if (hasIndirections!T)
GC.addRange(cast(void*)ret, tsize, typeid(T));
return ret;
}
void disposeGCSafe(T, Allocator)(Allocator allocator, T obj)
{
import core.memory : GC;
import std.traits : hasIndirections;
static if (hasIndirections!T)
GC.removeRange(cast(void*)obj);
allocator.dispose(obj);
}