Writing a replacement STL allocator

I’ve recently had the task of replacing the STL allocator and thought I would record some of the more useful resources while doing this:

Allocators (STL) @ Codeguru. A good article about writing a pool allocator replacement.

This article provides a good introduction to the subject with a sample replacement allocator. Things get a little more interesting when you have some state that is maintained per allocator. The implementation of the libstdc++ multithreaded allocator

This is a very interesting policy based allocator that is the default STL allocator on GNU/linux distributions. I assume it is very fast. I’m not actually trying to write a faster allocator.

Rogue Wave’s open source stdc++ implementation

I just came across this and it looks interesting. It’s supposed to be pretty fast and stable.

STLport cross platform STL implementation

At work we use STLport. If you have to use STL most people will refer you to STLport. Most STL implemenations are pretty solid but some such as the old vc6 dinkumware library are pretty bad.

Doug Lea’s malloc replacement

dlmalloc is a great replacement for the system malloc. In many cases it is the system implementation of malloc. The feature that I am using is the ‘mspaces’ private heap implementation which in combination with a custom STL allocator allows systems written using STL to use indepedent heaps. Using a private heap you can collect better statistics at runtime and also thread isolate heap access to reduce lock contention.

The C++ Programming Language Of course the best source of all details for the C++ language. This is the only place that I found that the rebind semantics were clearly explained. When state is maintained on the allocator it’s important to use some kind of reference maintainence for the allocator. The allocators may be copied, rebound to a different type and assigned to.

Someone may create a container with an allocator on their heap and you may store a reference to the container. If the caller’s allocator goes out of scope the heap needs to stay valid because you now have a container with a lot of objects allocated on it.

In my system there is a Heap class which acts as a wrapper around the mspace (private heap). The current design has the heaps reference linked together.

This entry was posted in c++. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>