I’m working on the runtime library for a dynamic language, and more specifically on its memory allocation.
I will use garbage collection, but I thought it would be nice to allow the user to use its own memory allocator if it needs to. However, after looking at the standard c++ allocator interface, I didn’t seen any way to have a generic allocator (except by templating on char, but it seems hacky).
- Is it a good idea to use the standard allocator interface ?
- If it is, what would be a possible design for allocating multiple types ?
Note : My library use C++11, so I’m talking about the ‘new’ allocator interface.
As @Cubbi said, look at
allocator::rebind. The actual type argument to an allocator is pretty-much irrelevant. Standard containers useallocator::rebindto change to the type that they actually need. In particular, associative containers need to allocate nodes that hold their own data as well as the data objects that the container nominally holds. Soallocator::rebindis used to get anallocator<Node<T>>from anallocator<T>.