Eliminate class allocators and deallocators?

Robert M. Münch robert.muench at robertmuench.de
Fri Jun 11 12:39:37 PDT 2010


Picking up this old topic & state.

What's the solution at the moment? I'm a bit lost. Is there now a way 
to use / make custom alloators with D2 or not?

If yes, how to do it?


On 2009-10-07 21:16:37 +0200, Andrei Alexandrescu said:

>> So for placement construction of a class, I guess it would look something like:
>> 
>> auto x = cast(MyClass) malloc(MyClass.classinfo.init.length);
>> x.__ctor( a, b, c ); // construct
>> ...
>> x.__dtor();
>> free( cast(void*) x );
>> 
>> Is that right?
> 
> Yes, I think so, but I haven't checked all the details. For example I'm 
> not sure whether __ctor copies .init over the memory before running the 
> user-defined constructor, or expects that to have been done already.
> 
> My understanding from Walter is that __ctor(x, y, z) are simply the 
> functions this(x, y, z) as written by the user, so you'd need to memcpy 
> the .init by hand before calling __ctor.
> 
> Aw hell I got curious so let me check.
> 
> class MyClass {
>      int x = 42;
>      this() {}
> }
> 
> void main() {
>      auto x = cast(MyClass) malloc(MyClass.classinfo.init.length);
>      x.__ctor();
>      writeln(x.x);
>      writeln(x.toString);
> }
> 
> That prints 0 and then crashes on my machine. Looks like you need to 
> memcpy the .init before calling __ctor.
> 
> I'm very glad we're starting to look into this. There are very nice 
> opportunities for adding custom allocation support in the stdlib.


-- 
Robert M. Münch
http://www.robertmuench.de



More information about the Digitalmars-d mailing list