Allocation

#include "LWF/Nothrow/Allocator.h"
#include "LWF/Nothrow/Array/Allocator.h"
#include "LWF/Placement/Allocator.h"
#include "LWF/Std/Allocator.h"
#include "LWF/Std/Array/Allocator.h"
#include "LWF/Std/Typed/Allocator.h"
#include "LWF/Custom/Allocator.h"
#include "LWF/Custom/Array/Allocator.h"
#include "LWF/Deleter.h"
#include "LWF/Destroying/Deleter.h"
#include "LWF/Placement/Deleter.h"
#include "LWF/Deallocation/Deleter.h"

Conception

An object life cycle

A life cycle of each object comprises:

Kind of classes

Kind Description
Allocator A class intended to provide the memory to accommodate an object
C++::new statement The statement which initializes an object by calling a specified Object’s::constructor(…) on memory allocated by the Allocator
Deallocator Frees the memory allocated by the Allocator
Deleter Combines invoking of the Object’s::~destructor() and the Deallocator

Interfaces

Deallocator
Void deallocate(Void *memory) const
Dellocates a block of specified memory
Allocator : Deallocator
Void * allocate(UInt size) const
Allocates a block of memory of specified size

Classes

namespace Std::
Classes of this namespace operate with the memory in a way trivial C++ new and delete statements do.
Deallocator
Void deallocate(Void *memory) const
Allocator
Void * allocate(UInt size) const
Void deallocate(Void *memory) const
Array::Deallocator
Void deallocate(Void *memory) const
Array::Allocator
Void * allocate(UInt size) const
Void deallocate(Void *memory) const
namespace Std::Typed::
The same as Std:: (extends and overloads it) but these classes automatically calculate memory size based on specified type of Object
template<typename T> Allocator : Std::Allocator
T * allocate() const
template<typename T> Array::Allocator<T[]> : Std::Array::Allocator
T * allocate(UInt count) const
namespace Nothrow::
Similar to Std:: but uses std::nothrow version of C++ new and delete statements and should be rarely used.
Deallocator
Void deallocate(Void *memory) const
Allocator
Void * allocate(UInt size) const
Void deallocate(Void *memory) const
namespace Placement::
Just provides an Allocator/Deallocator interface for already allocated memory.
Notice no array-specific implementation exists it’s the same as this one.
Deallocator
Void deallocate(Void *memory) const
Allocator
Allocator(Void *memory)
Void allocate(UInt) const
::
Aimed to delete objects allocated with C++ new statement
template<typename T> Deleter
Void Delete(T *object) const
template<typename T> Deleter<T[]>
Void Delete(T *objects) const
namespace Destroying::
Destructs an than deallocate objest(s) by specified deallocator
template<typename T, typename Deallocator> Deleter : Deallocator
Deleter(const Deallocator &deallocator)
Void Delete(T *object) const
template<typename T, typename Deallocator> Deleter<T[]> : Deallocator
Deleter(const Deallocator &deallocator)
Void Delete(T *objects, UInt count) const
namespace Placement::
Destructs objest(s) allocated by Placement::Allocator
template<typename T> Deleter : Destroying::Deleter<T, Placement::Deallocator>
Deleter(const Placement::Deallocator &deallocator)
namespace Deallocation::
Just represents a Deleter interface to the Deallocator object
template<typename T> Deleter : Destroying::Deleter<T, Placement::Deallocator>
Deleter(const Deallocator &deallocator)
Void Delete(Void *objects) const

Implementation details

Allocators/Deallocators

Class Implementation
::Allocator::allocate(UInt size) const ::Deallocator::deallocate(Void *memory) const
Std::Allocator/Deallocator ::operator new(size) ::operator delete(memory)
Std::Array::Allocator/Deallocator ::operator new[](size) ::operator delete[](memory)
Std::Typed::Allocator/Deallocator<T> Std::Allocator::allocate(sizeof(T))) Std::Deallocator::deallocate(memory)
Std::Typed::Allocator/Deallocator<T[]> Std::Array::Allocator::allocate(count * sizeof(T)) Std::Array::Deallocator::deallocate(memory)
Nothrow::Allocator/Deallocator ::operator new(size, std::nothrow) ::operator delete(memory, std::nothrow)
Nothrow::Array::Allocator/Deallocator ::operator new[](size, std::nothrow) ::operator delete[](memory, std::nothrow)
Placement::Allocator/Deallocator Just returns already allocated memory passed through a constructor Does nothing

Deleters

Class Delete(object) implementation
Destruction Deallocation
Deleter<Type> delete object
Deleter<Type[]> delete [] object
Destroying::Deleter<Type, Deallocator> object.~Type() deallocator.deallocate(object)
Destroying::Deleter<Type[], Array::Deallocator> object[count].~Type() deallocator.deallocate(object)
Placement::Deleter same as Destroying::Deleter<Type> Placemenet::Deallocator::deallocate(object)
Placement::Deleter<Type[]> same as Destroying::Deleter<Type[]> Placemenet::Deallocator::deallocate(object)
Deallocation::Deleter<Deallocator> Does nothing deallocator.deallocate(object)

Use cases examples

Usecase:: Object
Creation Destroying
Standard:: Plain
Type *ptr = new Type; Deleter<Type >().Delete(ptr);
Array
Type *ptr = new Type[N]; Deleter<Type[]>().Delete(ptr);
Nothrow:: Plain
Type *ptr = new (nothrow) Type; Deleter<Type >().Delete(ptr);
Array
Type *ptr = new (nothrow) Type[N]; Deleter<Type[]>().Delete(ptr);
Placement:: Plain
Type *ptr = new (Placement::Allocator(memory)) Type; Placement::Deleter().Delete(ptr);
Array
Type *ptr = new (Placement::Allocator(memory)) Type[N]; Placement::Deleter<Type[]>().Delete(ptr, N);
Custom:: Plain
typedef Std::Allocator Allocator;
Allocator allocator;
Type *ptr = new (allocator) Type;
Destroying::Deleter<Type, Allocator>(allocator).Delete(ptr);
Array
typedef Std::Array::Allocator Allocator;
Allocator allocator;
Type *ptr = new (allocator) Type[N];
Destroying::Deleter<Type[], Allocator>(allocator).Delete(ptr, N);
Preallocated:: Plain
typedef Std::Typed::Allocator<Type> Allocator;
Allocator allocator;
Type *ptr = allocator.allocate( ); Usecase::Placement::Plain(ptr);
Deallocation::Deleter<Allocator>(allocator).Delete(ptr);
Array
typedef Std::Typed::Allocator<Type[]> Allocator;
Allocator allocator;
Type *ptr = allocator.allocate(N); Usecase::Placement::Array(ptr);
Deallocation::Deleter<Allocator>(allocator).Delete(ptr);