D's New GC and Object Allocation Pools
Damian via Digitalmars-d
digitalmars-d at puremagic.com
Sun Oct 26 04:16:37 PDT 2014
On Sunday, 26 October 2014 at 03:37:47 UTC, Maxime
Chevalier-Boisvert wrote:
> Hello,
>
> I was wondering if there have been updates regarding Andrei's
> announcement that he would rewrite the D garbage collector. Is
> there any kind of timeline for when a new version of the GC can
> be expected?
>
> I also wanted to ask if there was an implementation of an
> object pool in the standard library. If not, I'm wondering what
> the best way to implement this is. Is there any way to overload
> new and destroy?
>
> I was thinking of using the templated emplace operator from
> std.conv to allocate class objects into a large flat array, and
> to derive pool-allocated classes from a PoolObject base class.
> This base class would contain linked list pointers to implement
> a free list, as well as templated static methods to allocate
> and free the objects. Any advice welcome.
For your object pool, why not just create functions, alloc and
free, instead of trying to override them - something like below
version (USE_GC)
{
import core.memory;
}
else
{
import core.stdc.stdlib: malloc, free;
}
struct MemPool
{
void* alloc(size_t size, bool usePool = true) nothrow
{
version (USE_GC) return GC.malloc(size);
else malloc(size);
}
void Free(void* p) nothrow
{
version (USE_GC) GC.free(p);
else free(p);
}
}
More information about the Digitalmars-d
mailing list