I'll do that if I have to, but it's cleaner with an override of
new as it makes it impossible to mistakenly allocate the object
outside of the pool. It's also nicer if you can pass arguments to
your constructor.
On Sunday, 26 October 2014 at 11:16:39 UTC, Damian wrote:
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);
}
}