cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
cppcore::TPoolAllocator< T > Class Template Reference

This class implements a simple pool-based allocation scheme. More...

#include <TPoolAllocator.h>

Public Member Functions

 TPoolAllocator ()=default
 The default class constructor.
 TPoolAllocator (size_t numItems)
 The class constructor with the pool size.
 ~TPoolAllocator ()
 The class destructor.
T * alloc ()
 WIll alloc one item from the pool.
void release ()
 Will release all allocated items.
void reserve (size_t size)
 Will reserve the pool.
void clear ()
 Will clear the pool, memory will be deallocated.
size_t capacity () const
 Returns the capacity of items in the pool allocator.
size_t reservedMem () const
 WIll return the reserved memory in bytes.
size_t freeMem () const
 Will return the free memory in the pool in bytes.
void dumpAllocations (std::string &allocs)
 Will dump a statistic overview into the given string.
void resize (size_t growSize)
 Will perform a resize for the pool.
void reset ()
 Will reset the allocator.

Detailed Description

template<class T>
class cppcore::TPoolAllocator< T >

This class implements a simple pool-based allocation scheme.

Initially you have to define its size. Each allocation will be done from this initially created pool. You have to release all pooled instances after the usage. This allocation scheme is fast and does no call any new-calls during the lifetime of the allocator.

struct foo { int payload[10]; };
TPoolAllocator<foo> allocator(10000);
foo *instance = allocator.alloc();
...
allocator.release(instance);
This class implements a simple pool-based allocation scheme.

Definition at line 49 of file TPoolAllocator.h.

Constructor & Destructor Documentation

◆ TPoolAllocator() [1/2]

template<class T>
cppcore::TPoolAllocator< T >::TPoolAllocator ( )
default

The default class constructor.

References TPoolAllocator().

Referenced by TPoolAllocator().

◆ TPoolAllocator() [2/2]

template<class T>
cppcore::TPoolAllocator< T >::TPoolAllocator ( size_t numItems)
inline

The class constructor with the pool size.

Parameters
numItems[in] The pool size.

Definition at line 139 of file TPoolAllocator.h.

◆ ~TPoolAllocator()

template<class T>
cppcore::TPoolAllocator< T >::~TPoolAllocator ( )
inline

The class destructor.

Definition at line 146 of file TPoolAllocator.h.

References clear().

Member Function Documentation

◆ alloc()

template<class T>
T * cppcore::TPoolAllocator< T >::alloc ( )
inline

WIll alloc one item from the pool.

Returns
The pointer to the allocated item.

Definition at line 151 of file TPoolAllocator.h.

References resize().

◆ capacity()

template<class T>
size_t cppcore::TPoolAllocator< T >::capacity ( ) const
inline

Returns the capacity of items in the pool allocator.

Returns
The capacity.

Definition at line 211 of file TPoolAllocator.h.

◆ clear()

template<class T>
void cppcore::TPoolAllocator< T >::clear ( )
inline

Will clear the pool, memory will be deallocated.

Note
All instances which are currently is use will get invalid. PLease use with care.

Definition at line 195 of file TPoolAllocator.h.

Referenced by reserve(), and ~TPoolAllocator().

◆ dumpAllocations()

template<class T>
void cppcore::TPoolAllocator< T >::dumpAllocations ( std::string & allocs)
inline

Will dump a statistic overview into the given string.

Parameters
allocs[inout] The string to hold the allocation statistic.

Definition at line 230 of file TPoolAllocator.h.

◆ freeMem()

template<class T>
size_t cppcore::TPoolAllocator< T >::freeMem ( ) const
inline

Will return the free memory in the pool in bytes.

Returns
The free memory in bytes.

Definition at line 221 of file TPoolAllocator.h.

◆ release()

template<class T>
void cppcore::TPoolAllocator< T >::release ( )
inline

Will release all allocated items.

Definition at line 167 of file TPoolAllocator.h.

◆ reserve()

template<class T>
void cppcore::TPoolAllocator< T >::reserve ( size_t size)
inline

Will reserve the pool.

@apram size [in] The pool size to reserve.

Definition at line 182 of file TPoolAllocator.h.

References clear().

◆ reservedMem()

template<class T>
size_t cppcore::TPoolAllocator< T >::reservedMem ( ) const
inline

WIll return the reserved memory in bytes.

Returns
The reserved memory in bytes.

Definition at line 216 of file TPoolAllocator.h.

◆ reset()

template<class T>
void cppcore::TPoolAllocator< T >::reset ( )

Will reset the allocator.

Definition at line 261 of file TPoolAllocator.h.

◆ resize()

template<class T>
void cppcore::TPoolAllocator< T >::resize ( size_t growSize)
inline

Will perform a resize for the pool.

Parameters
growSize[in] The soze to grow.

Definition at line 238 of file TPoolAllocator.h.

Referenced by alloc().


The documentation for this class was generated from the following file: