I want to replace the standard allocator with a more robust allocator (the C++ standard only requires an overflow check on vector::resize). The various C++ allocators supplied with many libraries fall flat on their face when fed negative self tests.
I have access to a more robust allocator. ESAPI’s allocator not only checks for overflow, it also has debug instrumentation to help find mistakes. http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h.
Is there a standard way to replace the C++ allocator used in a program without too much effort? I also want to ensure its replaced in library code, which I may not have access to source code.
Unlike
mallocwhich is a library function that can be replaced by another function with the same signature,std::allocatoris a class template and template code is instantiated as needed and inlined into code that uses it. Some standard library code will have already been compiled into the library’s object files and will contain instantiatedstd::allocatorcode which can’t be replaced. So the only way is if the standard library provides some non-standard way to replace itsstd::allocator. Luckily, GCC’s libstdc++ allows you to do just that, allowing you to select the implementation used forstd::allocatorwhen GCC is configured and built, with a few different choicesIt wouldn’t be too much work to add the ESAPI allocator to the GCC sources as one of the options, then rebuild GCC to use that allocator as the base class of
std::allocatorproviding its implementation. You might need to tweak the ESAPI allocator code a bit, and maybe alter the libstdc++configurescript to allow you to say--enable-libstdcxx-allocator=esapi