Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Allocation Module

Classes

class  elm::BadAlloc
 
class  elm::DefaultAllocator
 
class  elm::StackAllocator
 

Detailed Description

This module binds together classes providing different ways to perform memory allocation. This kind of class is usually passed to any container class performing automatic memory allocation.

The chosen allocator is usually passed as a template parameter to the collection type and as a parameter to the collection constructor.

An allocator class must implement the concept elm::concept::Allocator.

A collection supporting allocator matches the concept below:

template <class A>
class CollectionWithAllocation {
public:
CollectionWithAllocation(A& allocator = A::DEFAULT);
};

An easy use of custom allocator is the overload of new and delete operators of a class to let passing the allocator as a parameter. The code below gives an examples:

class MyClass {
public:
// usual constructors, methods and attributes
inline void *operator new(std::size_t size, Allocator& alloc) { return alloc.allocate(size); }
inline void operator delete(void *p, std::size_t size) { alloc.free(p); }

Then, you can create and delete the object with the following code:

Allocator custom_alloc;
MyClass *object = new(custom_alloc) MyClass(arguments here);
// do the work with object
delete(custom_alloc) object;