PageAllocator Class Reference

#include <Pt/PageAllocator.h>

Page based allocator. More...

Inherits Allocator, and NonCopyable.

Public Member Functions

 PageAllocator (std::size_t pageSize=MinChunkSize)
 Construct with minimum page size.
 
 ~PageAllocator ()
 Destructor.
 
void * allocate (std::size_t size)
 Allocates size bytes of memory.
 
void clear ()
 Releases all memory.
 
void deallocate (void *p, std::size_t size)
 Deallocates memory of size bytes.
 

Detailed Description

The PageAllocator is useful, when chunks of memory have to be allocated, that can be released simultaneously. This allows the PageAllocator to allocate memory consecutively on pages and simply release all pages together at the end. Therefore, deallocate() will not do anything, but memory will only eventually be released, when clear() is called or the PageAllocator is destructed. The next example illustrates this:

void useAllocator(Pt::Allocator& a)
{
for(std::size_t n = 1; n < 16; ++n)
{
void* p = a.allocate(n);
...
// won't do anything if it's a PoolAllocator
a.deallocate(p, n);
}
}
useAllocator(allocator);
// release all allocated memory
allocator.clear();