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.
operator new
for this library only, so alloperator new
calls in the library went to your limited heap. What behavior would you want when the limited heap is full and the overloadedoperator new
is called? Would you really want theoperator new
to fail under those circumstances? I don't believe you would, unless that library can still function whenoperator new
is failing. Maybe if the library is a database cache, or something like that. What does the library do? – Miguelmiguela