D's New GC and Object Allocation Pools

Mike via Digitalmars-d digitalmars-d at puremagic.com
Sun Oct 26 16:20:20 PDT 2014


On Sunday, 26 October 2014 at 14:13:25 UTC, Maxime 
Chevalier-Boisvert wrote:
> What I'm trying to do is have some specific classes be 
> pool-allocated though, not completely circumvent the GC.
>

The method I proposed just overrides `new` (i.e. _d_newclass).  
What goes inside of `new` is up to you, including allocating from 
GC memory by calling the default via `__real__d_newclass` (The 
linker takes care of translating the symbol names). And since you 
have a `ClassInfo` object you can selectively choose which types 
are allocated from a pool, and which are allocated by the GC.

extern (C) Object __real__d_newclass(const ClassInfo ci)

extern (C) Object __wrap__d_newclass(const ClassInfo ci)
{
     if (ci.ClassName == "MyPoolAllocatedClass")
     {
         return allocateFromPool(ci);
     }
     else
     {
         retun __real__d_newclass(ci);
     }
}

This method is somewhat hackish, but I think it's the only way to 
override `new`.  Language support for this kind of thing would be 
nice. `@weak` attribute[1] perhaps, or `virtual`/`final` module 
methods anyone? `final` by default please. :)

Mike

[1] - 
https://gcc.gnu.org/onlinedocs/gcc-4.9.1/gcc/Function-Attributes.html#Function-Attributes


More information about the Digitalmars-d mailing list