Reference Counting Template

bearophile bearophileHUGS at lycos.com
Sun Dec 13 11:35:02 PST 2009


dsimcha: 
> Reference counting is a pretty useful general technique for managing resources
> that can't have cycles and I think it makes sense to have a generic template
> for it instead of having people reimplement the same code over and over for
> file handles, large arrays of primitives that they allocate on the C heap and
> want freed deterministically, database connections, etc.  Does this sound
> worthwhile?

In real-world D programs the built-in GC is not enough, and forcing the programmer to re-invent everything is bad in a modern language. So putting alternative allocators (or resource managers as in your case) in Phobos will be necessary. For example I have D1 programs that become two times faster when I use a pool allocator.

Your reference counter can even contain a cycle detection&break routine.

One memory allocator (that can be used in D with memory from the C heap too) that I'd like to see in Phobos is a Hierarchical allocator, this is the simplest, really easy to use, you can allocate memory blocks with a tree topology and then free a whole subtree. It's easy to remember and use this API:
http://www.swapped.cc/halloc/
This is a more complex implementation, that's more complex to use too:
http://talloc.samba.org/

Bye,
bearophile



More information about the Digitalmars-d mailing list