template<typename TAllocator>
struct alib::lang::AllocatorInterface< TAllocator >
This type offers high-level convenience methods for ALib allocators.
An ALib allocator (a type that offers an interface as specified with prototype Allocator) has to provide only three low-level (!) allocation methods. Those are:
Besides these, the C++ function call operator operator()() has to be implemented to return a temporary instance of this type. Note that the compiler will optimize the function call and the temporary instance out. With this setup, each allocator offers the high-level allocation methods provided with this class.
The naming scheme of the convenience methods found with this type, is along the standards:
- Methods containing the words
"Alloc"
or "Free"
allocate, respectively free memory without construction, respectively destruction of type instances.
- Methods containing the word
"New"
and "Delete"
allocate, respectively free memory and in addition invoke constructors, respectively destructors of type instances.
In contrast to the low-level interface methods Allocator::allocate and Allocator::reallocate, information about a piece of memory returned that is larger than requested is not retrievable with the methods provided here. In case this can be of value, the low-level interface of an allocator has to be used.
The following quick sample illustrates the use of this class in combination with a TMonoAllocator:
struct MyType
{
: member(pMember)
{}
};
MyType* myObject= allocator().New<MyType>(
A_CHAR(
"Hello"));
allocator().Delete(myObject);
- Note
- In this sample, the destructor is of class
MyType
is emtpy, and furthermore method free of class MonoAllocator is empty. Thus, the whole call to Delete will be optimized out. With other allocators this is different. It is recommended to still include such calls to avoid memory leaks at the moment either the allocator is changed, an allocated type gets equipped with a destructor, etc.
- See also
-
Definition at line 322 of file allocation.hpp.
|
| AllocatorInterface (TAllocator &pAllocator) |
|
template<typename T > |
T * | Alloc () |
|
template<typename TSize , typename TAlignment > |
void * | Alloc (TSize size, TAlignment alignment) |
|
template<typename T , typename TLength > |
T * | AllocArray (TLength length) |
|
template<typename T > |
void | Delete (T *object) |
|
template<typename T , typename TIntegral > |
void | DeleteArray (T *array, TIntegral length) |
|
template<typename T > |
void | Free (T *mem) |
|
template<typename T , typename TIntegral > |
void | Free (T *mem, TIntegral size) |
|
template<typename T , typename TIntegral > |
void | FreeArray (T *array, TIntegral length) |
|
template<typename T , typename... TArgs> |
ALIB_FORCE_INLINE T * | New (TArgs &&... args) |
|
template<typename T , typename TSize , typename... TArgs> |
T * | NewArray (TSize length, TArgs &&... args) |
|
template<typename TAllocator >
template<typename T , typename TLength >
T * AllocArray |
( |
TLength | length | ) |
|
|
inline |
Allocates aligned memory for an array of objects of type T of size length leaving the memory uninitialized.
- Template Parameters
-
T | The array element type. |
TLength | The type of parameter length. Has to be integral. Deduced by the compiler. |
- Parameters
-
length | The capacity of the requested array. |
- Returns
- A pointer to the first element of the allocated array.
Definition at line 365 of file allocation.hpp.
template<typename TAllocator >
template<typename T , typename TIntegral >
void Free |
( |
T * | mem, |
|
|
TIntegral | size ) |
|
inline |
Frees memory of given size pointed to by mem. This method does not call a destructor. To dispose custom types with due destruction, use method Delete<T>(T*).
- Template Parameters
-
T | The pointer type of the given mem to free. |
TIntegral | The type that parameter size is provided with. Deduced by the compiler. (Has to be integral.) |
- Parameters
-
mem | The memory to free. |
size | The size of the given mem. |
Definition at line 473 of file allocation.hpp.
template<typename TAllocator >
template<typename T , typename TIntegral >
void FreeArray |
( |
T * | array, |
|
|
TIntegral | length ) |
|
inline |
Frees memory of an array of objects. This method does not call a destructor. To also destruct the elements of an array, use method DeleteArray<T>(T*, TIntegral).
- Template Parameters
-
T | The pointer type of parameter array. |
TIntegral | Integral type of parameter size. Deduced by the compiler. |
- Parameters
-
array | Pointer to the first array element. |
length | The size of the given mem. |
Definition at line 487 of file allocation.hpp.
template<typename TAllocator >
template<typename T , typename... TArgs>
Allocates memory of size- and alignment-suitable for type T and performs a C++ "placement-new", passing the given variadic arguments to the type's constructor.
- Template Parameters
-
T | Type of the object to allocate and construct. |
TArgs | Types of variadic parameters given with parameter args. |
- Parameters
-
args | Variadic parameters to be forwarded to the constructor of type T. |
- Returns
- A pointer to the initialized array.
Definition at line 379 of file allocation.hpp.
template<typename TAllocator >
template<typename T , typename TSize , typename... TArgs>
T * NewArray |
( |
TSize | length, |
|
|
TArgs &&... | args ) |
|
inline |
Allocates aligned memory for an array of objects of type T of size length. All array elements are initialized using a "placement-new", passing the given args to each element's constructor.
- Template Parameters
-
T | Element type of the array to allocate and construct. |
TArgs | Types of variadic parameters given with parameter args. |
TSize | The type of parameter length. Deduced by the compiler. (Must be integral.) |
- Parameters
-
length | The capacity of the requested array. |
args | Variadic parameters to be forwarded to the constructor of each array element of type T. |
- Returns
- A pointer to the first element of the allocated array.
Definition at line 394 of file allocation.hpp.