On Sunday, 26 October 2014 at 03:37:47 UTC, Maxime
Chevalier-Boisvert wrote:
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'm not an authority on the subject, but I believe all you need
to do is provide alternate implementations for _d_newclass[1] and
destroy[2]. I experimented with this a little bit about a year
ago, and it worked well for me. I modified druntime for my
experiments, but there may be a way to link in your
implementations instead of the defaults at link time, thus giving
you an "override" effect.
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.
If you haven't already, check out the Memory Management article
on the D Wiki [3]. It's a nice article that was written some
time ago by an unknown author and migrated to the wiki last year
to help keep it maintained. I particularly like Explicit Class
Allocation[4], Mark/Release[5], and Free Lists[6] as alternatives.
Mike
[1] -
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/lifetime.d#L60
[2] -
https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L2379
[3] - http://wiki.dlang.org/Memory_Management
[4] -
http://wiki.dlang.org/Memory_Management#Explicit_Class_Instance_Allocation
[5] - http://wiki.dlang.org/Memory_Management#Mark.2FRelease
[6] - http://wiki.dlang.org/Memory_Management#Free_Lists