2016-11-08 14:32:25 +00:00
|
|
|
module vibe.internal.allocator;
|
|
|
|
|
|
|
|
public import std.experimental.allocator;
|
|
|
|
public import std.experimental.allocator.building_blocks.allocator_list;
|
|
|
|
public import std.experimental.allocator.building_blocks.null_allocator;
|
|
|
|
public import std.experimental.allocator.building_blocks.region;
|
|
|
|
public import std.experimental.allocator.building_blocks.stats_collector;
|
|
|
|
public import std.experimental.allocator.gc_allocator;
|
|
|
|
public import std.experimental.allocator.mallocator;
|
2017-06-28 12:05:50 +00:00
|
|
|
|
|
|
|
// NOTE: this needs to be used instead of theAllocator due to Phobos issue 17564
|
|
|
|
@property IAllocator vibeThreadAllocator()
|
|
|
|
@safe nothrow @nogc {
|
|
|
|
static IAllocator s_threadAllocator;
|
|
|
|
if (!s_threadAllocator)
|
|
|
|
s_threadAllocator = () @trusted { return allocatorObject(GCAllocator.instance); } ();
|
|
|
|
return s_threadAllocator;
|
|
|
|
}
|
2017-07-20 11:34:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|