Hi,
I am trying to create an allocator that don't use the GC, and I have
issues for the initialization of member before calling the constructor.
Here is my actual code :
mixin template NogcAllocator(T)
{
static T nogcNew(T, Args...)(Args args) @nogc
{
import core.stdc.stdlib : malloc;
import std.traits;
T instance;
instance = cast(T)malloc(__traits(classInstanceSize, T));
foreach (string member; __traits(allMembers, T))
{
static if (isType!(__traits(getMember, T, member)))
__traits(getMember, instance, member) =
typeof(__traits(getMember, T, member)).init;
}
instance.__ctor(args);
return instance;
}
static void nogcDelete(T)(T instance) @nogc
{
import core.stdc.stdlib : free;
instance.__dtor();
free(instance);
}
}
unittest
{
struct Dummy {
int field1 = 10;
int field2 = 11;
}
class MyClass {
mixin NogcAllocator!MyClass;
int a = 0;
int[] b = [1, 2, 3];
Dummy c = Dummy(4, 5);
int d = 6;
this() @nogc {
}
this(int val) @nogc {
d = val;
}
}
MyClass first = MyClass.nogcNew!MyClass();
MyClass second = MyClass.nogcNew!MyClass(7);
assert(first.a == 0);
assert(first.b == [1, 2, 3]);
assert(first.c.field1 == 4);
assert(first.d == 6);
assert(second.c.field1 == 4);
assert(second.d == 7);
}
And the compilation errors :
..\src\core\nogc_memory.d(16): Error: no property 'this' for type
'core.nogc_memory.__unittestL39_3.MyClass'
..\src\core\nogc_memory.d(17): Error: type Monitor is not an expression
..\src\core\nogc_memory.d(63): Error: template instance
core.nogc_memory.__unittestL39_3.MyClass.NogcAllocator!(MyClass).nogcNew!(MyClass)
error instantiating
..\src\core\nogc_memory.d(16): Error: no property 'this' for type
'core.nogc_memory.__unittestL39_3.MyClass'
..\src\core\nogc_memory.d(17): Error: type Monitor is not an expression
..\src\core\nogc_memory.d(64): Error: template instance
core.nogc_memory.__unittestL39_3.MyClass.NogcAllocator!(MyClass).nogcNew!(MyClass,
int) error instantiating
I don't understand my mistake with the getMember and isType traits.
And I am curious about of what is the Monitor.