The only class you need. More...
#include <type_pool.h>
Public Types | |
typedef std::unique_ptr< T, std::function< void(T *)> > | unique_ptr |
Re-mapping of std::unqiue_ptr<T> to hide away the signature of the deleter. | |
Public Member Functions | |
type_pool (size_t preallocate=0, size_t cap=chunk_pool< sizeof(T), Allocator >::max_size()) | |
Constructor. | |
template<typename U > | |
std::enable_if< std::is_same < T, U >::value, T * >::type | get_ptr (U &&t) |
Returns a pointer to an instance of T. | |
template<typename U , typename... Args> | |
std::enable_if< std::is_same < T, U >::value, T * >::type | get_ptr (Args &&...args) |
Returns a pointer to an instance of T. | |
template<typename U > | |
std::enable_if< std::is_same < T, U >::value, std::shared_ptr< T > >::type | get_shared (U &&t) |
Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T> . | |
template<typename U , typename... Args> | |
std::enable_if< std::is_same < T, U >::value, std::shared_ptr< T > >::type | get_shared (Args &&...args) |
Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T> . | |
template<typename U > | |
std::enable_if< std::is_same < T, U >::value, typename type_pool< T, Allocator > ::unique_ptr >::type | get_unique (U &&t) |
Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T> . | |
template<typename U , typename... Args> | |
std::enable_if< std::is_same < T, U >::value, typename type_pool< T, Allocator > ::unique_ptr >::type | get_unique (Args &&...args) |
Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T> . |
The only class you need.
Use this class to pool memory dynamically allocated for objects of type T (and only T) in a free store instead of releasing it to the heap. The pool's free store starts empty. When allocation is requested:
Allocator<T>::allocate()
.Use get_ptr to get a pointer that will not automatically be returned to the pool's free store. Use get_shared or get_unique to get a pointer that will automatically be returned to the pool's store. When pointers are returned, the objects they point to are destroyed using their destructor and their memory space is pushed on the free store. Note that the pool itself must be kept alive for as long as their exists shared_ptr<T>'s
and unique_ptr<T>'s
that have not been returned to it.
It is possible, when it is instantiated, to request the pool to preallocate a certain amount of memory. Preallocation will occur on a seperate thread. The first allocation requested will wait until preallocation is over.
T | The type of objects to pool. |
Allocator | The allocation policy class to use when allocating and deallocating memory. |
type_pool< T, Allocator >::type_pool | ( | size_t | preallocate = 0 , |
size_t | cap = chunk_pool<sizeof(T), Allocator>::max_size() |
||
) | [inline] |
Constructor.
preallocate | The number of Ts for which to preallocate memory. |
cap | The maximum number of Ts to keep in the store. |
std::enable_if<std::is_same<T, U>::value, T*>::type type_pool< T, Allocator >::get_ptr | ( | U && | t | ) | [inline] |
Returns a pointer to an instance of T.
The returned pointer will not be automatically be returned to the pool and must be manually destroyed and deallocated with Allocator::destroy and Allocator::deallocate.
t | The object to move into the allocated memory. |
std::enable_if<std::is_same<T, U>::value, T*>::type type_pool< T, Allocator >::get_ptr | ( | Args &&... | args | ) | [inline] |
Returns a pointer to an instance of T.
The returned pointer will not be automatically be returned to the pool and must be manually destroyed and deallocated with Allocator::destroy and Allocator::deallocate.
args | The arguments to forward to T's constructor. |
std::enable_if<std::is_same<T, U>::value, std::shared_ptr<T> >::type type_pool< T, Allocator >::get_shared | ( | U && | t | ) | [inline] |
Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>
.
t | The object to move into the allocated memory. |
std::enable_if<std::is_same<T, U>::value, std::shared_ptr<T> >::type type_pool< T, Allocator >::get_shared | ( | Args &&... | args | ) | [inline] |
Wraps a raw pointer returned from get_ptr in a std::shared_ptr<T>
.
args | The arguments to forward to T's constructor. |
std::enable_if<std::is_same<T, U>::value, typename type_pool<T, Allocator>::unique_ptr>::type type_pool< T, Allocator >::get_unique | ( | U && | t | ) | [inline] |
Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>
.
t | The object to move into the allocated memory. |
std::enable_if<std::is_same<T, U>::value, typename type_pool<T, Allocator>::unique_ptr>::type type_pool< T, Allocator >::get_unique | ( | Args &&... | args | ) | [inline] |
Wraps a raw pointer returned from get_ptr in a type_pool<T>::unique_ptr. Use the return value as you would a std::unique_ptr<T>
.
args | The arguments to forward to T's constructor. |