I have the source for a large (>250 files) library that makes heavy use of STL containers and strings. I need to run it in an embedded environment with limited heap, so I want to ensure that this library itself is limited in its heap usage.
The obvious solution is to create an allocator, but modifying the entire code base to include the allocator template parameter is a big job of last resort, and undesirable in case I ever want to take a new version of the source. Globally replacing new and delete is not feasible since that affects the entire image, not just this library.
My next thought was a stupid C macro trick, but that doesn’t seem like it would be possible, although I admit to not being a clever macro author.
So I thought “is there a compiler or pragma switch to specify the allocator<> class at compile time”? But I’m open for anything.
The next question I’ll ask, if anyone can come up with a solution, is how to do the same for new/delete within the set of files comprising this library.
I’m using the gcc 3.4.4 toolchain to run this under Cygwin, with a target of VxWorks, if that sparks any ideas.
I resorted to the preprocessor to get a possible solution, although it currently relies upon the GCC 3.4.4 implementation to work.
The GCC
<memory>implementation includes the file<bits/allocator.h>, which in turn includes another file<bits/c++allocator.h>, which defines a macro that defines the class implementing the default allocator base class.Since is found in a platform-dependent path (
/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/i686-pc-cygwin/bits), I don’t feel (very) dirty in supplanting it with my own “platform-dependent” implementation.So I just create a folder
bits/in the root of my source’s include path, and then create the filec++allocator.hin that folder. I define the required macro to be the name of my allocator class and it works like a charm, since gcc searches my include paths prior to searching the system includes.Thanks for all your responses. I think I can go with this “solution”, which will only work as long as I’m using 3.4.4 probably.