Global replacement for alloc operators

  • Anonymous

    Anonymous - 2010-07-23

    I want to use SmallObjAllocator from Loki to replace new and delete operators globally. So I've done such thing:

    #include <loki/SmallObj.h>
    #include <loki/Singleton.h>
    class MySmallObjAllocator;
    typedef Loki::SingletonHolder<MySmallObjAllocator, Loki::CreateUsingMalloc> MyAlloc;
    class MySmallObjAllocator : public Loki::SmallObjAllocator {
      friend struct Loki::CreateStatic<MySmallObjAllocator>;
      : Loki::SmallObjAllocator(10485760, 1024, 4)
    void* operator new(std::size_t size) {
      return MyAlloc::Instance().Allocate(size, true);
    void operator delete(void *p) {

    But I faced problem, that some methods (for example FixedAllocator::MakeNewChunk) call global new and delete, which calls those methods. So due to such infinite recursion application crashes.

    Is it possible to use allocator from Loki to replace operators new and delete globally?

  • Richard Sposato

    Richard Sposato - 2010-07-29

    I suggest using caution before deciding to write your own versions of global new and delete.  There are very few situations where you may want to write your own, such as embedded firmware.  Other than a tiny minority of situations, writing your own global new and delete operators causes more problems than it solves.

    One problem is that if you write your own, and you link your software with libraries from your compiler vendor or third parties, you may get linker errors if you are lucky.  If you are not lucky, you may end up with no linker errors but strange run time bugs because those other libraries depended on the common implementations of global new and delete.

    Having said all that, if you still want to write your own global new and delete operators, here is my advice.  If you already know all I am about to advise, you may disregard the next three paragraphs.

    First read Item 8 in Scott Meyer's book, "Effective C++ Second Edition".  That will explain some of the pitfalls of writing new and delete operators.

    My second piece of advice is to make sure you provide all 6 forms each of the global new and delete operators.  (There are 3 for new single item, and 3 for new array, and counterparts for the delete operators.)  If you make any one of those new or delete operators, you should make them all because without them you will get compiler errors in your code, and linker errors when linking other libraries to your code.

    My third piece of advice is to check if the global new and delete operators provided by your compiler are just hidden calls to the standard C functions, malloc and free.  Some compilers do that, and if they do, you can modify Loki's SmallObj.cpp file to call malloc and free instead of new and delete.  If your compilers versions of global new and delete operators don't call malloc and free, then that option is not available to you.

    So, from what I mentioned above, I strongly advise against using Loki's SmallObject allocator to replace global new and delete operators.  If you still want to, then I suggest changing your local copy of Loki's SmallObj.cpp file to use malloc and free instead.  You will need to make several other changes.  You will need to provide an allocator to the Chunks vector since the Chunks uses the STL allocator which calls global new and delete.  It's trivial to write an allocator that calls malloc and delete instead.  You will also need see if your version of Loki's SmallObject allocator #defines USE_NEW_TO_ALLOCATE.  Look through the source code for uses of USE_NEW_TO_ALLOCATE because that will tell you where most calls to new and delete occur.

    Hope that helps,




Log in to post a comment.