Allocator Class Reference

#include <Pt/Allocator.h>

Allocator interface. More...

Inherited by PageAllocator, and PoolAllocator.

Public Member Functions

 Allocator ()
 Default constructor.
virtual ~Allocator ()
virtual void * allocate (std::size_t size)
 Allocates size bytes of memory.
virtual void deallocate (void *p, std::size_t)
 Deallocates memory of size bytes.

Detailed Description

Allocators allow a program to use different methods of allocating and deallocating raw memory. The default implementation will simply use new and delete. Custom allocators are implemented by overriding the two methods allocate() and deallocate() of the Pt::Allocator base class. The following example tracks the amount of allocated memory:

class CheckedAllocator : public Pt::Allocator
: _allocated(0)
virtual void* allocate(std::size_t size)
void* p = Pt::Allocator::allocate(size);
_allocated += size;
return p;
virtual void deallocate(void* p, std::size_t size)
_allocated -= size;
std:size_t allocated() const
{ return _allocated; }
std::size_t _allocated;

This interface differs from std::allocator used for STL containers, because it allows to allocate memory of different sizes through the same interface. The std::allocator is meant to allocate and also construct objects of the same size. It is however possible, to implement a std::allocator using the raw memory allocators described here.