Replace default STL allocator

You could benefit from using EASTL (Enterprise Arts STL (partial) implementation)

EASTL -- Electronic Arts Standard Template Library

This was intended for embedded/game development, in environments where global heap is really scarce, non-existent or problematic in general.

The allocator model of EASTL was inspired on (or resembles?) the ideas in the famous Towards a Better Allocator Model publication (PDF).

The EASTL lends itself well for custom allocators. In fact, it doesn't ship with an allocator, so providing (a minimal) one is required to even get you application to link.

Here is the github repo for EASTL: https://github.com/electronicarts/EASTL


So I thought "is there a compiler or pragma switch to specify the allocator<> class at compile time"? But I'm open for anything.

No there isn't.

Take a look here.

Allocators are a template argument in every stl container. You will need to change them. I have done the same thing in the past, when working on embedded. I could give you some pointers if you like :

Basic template allocator :

namespace PFM_MEM {
    template <class T>
    class CTestInstAllocator {
    public:
        // type definitions
        typedef size_t    size_type;
        typedef ptrdiff_t difference_type;
        typedef T*        pointer;
        typedef const T*  const_pointer;
        typedef T&        reference;
        typedef const T&  const_reference;
        typedef T         value_type;

        // rebind CTestInstAllocator to type U
        template <class U>
        struct rebind {
            typedef CTestInstAllocator<U> other;
        };

        // return address of values
        pointer address (reference value) const {
            return &value;
        }
        const_pointer address (const_reference value) const {
            return &value;
        }

        /* constructors and destructor
        * - nothing to do because the CTestInstAllocator has no state
        */
        CTestInstAllocator() {
        }
        CTestInstAllocator(const CTestInstAllocator&) {
        }
        template <class U>
        CTestInstAllocator (const CTestInstAllocator<U>&) {
        }
        ~CTestInstAllocator() {
        }

        // return maximum number of elements that can be allocated
        size_type max_size () const {
            return std::numeric_limits<size_t>::max() / sizeof(T);
        }

        // pvAllocate but don't initialize num elements of type T by using our own memory manager
        pointer allocate (size_type num) {
            /**
            * pvAllocate memory custom memory allocation scheme
            */
            return(pointer)(CPfmTestInstMemManager::pvAllocate(num*sizeof(T)));
        }
        // initialize elements of allocated storage p with value value
        void construct (pointer p, const T& value) {
            // initialize memory with placement new
            new((void*)p)T(value);
        }

        // destroy elements of initialized storage p
        void destroy (pointer p) {
            // destroy objects by calling their destructor
            p->~T();
        }
        // vDeallocate storage p of deleted elements
        void deallocate (pointer p, size_type num) {
            /**
            *Deallocate memory with custom memory deallocation scheme
            */
            CPfmTestInstMemManager::vDeallocate((void*)p);
        }
    };

    // return that all specializations of this CTestInstAllocator are interchangeable
    template <class T1, class T2>
    bool operator== (const CTestInstAllocator<T1>&,
        const CTestInstAllocator<T2>&) {
            return true;
    }
    template <class T1, class T2>
    bool operator!= (const CTestInstAllocator<T1>&,
        const CTestInstAllocator<T2>&) {
            return false;
    }
}

Take particular note at these lines :

/**
* pvAllocate memory custom memory allocation scheme
*/
return(pointer)(CPfmTestInstMemManager::pvAllocate(num*sizeof(T)));

// vDeallocate storage p of deleted elements
void deallocate (pointer p, size_type num) {
/**
*Deallocate memory with custom memory deallocation scheme
*/
CPfmTestInstMemManager::vDeallocate((void*)p);

Here is the place where you call your new and delete which work on your heap.

I could provide you with an example of how to construct some basic memory manager to help you further.


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 file c++allocator.h in 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.

Tags:

C++

Stl