Base Framework
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Static Protected Member Functions | List of all members
Allocator< TYPE > Class Template Reference

Allocator. More...

#include <base/mem/Allocator.h>

Inherited by ReferenceCountedAllocator< TYPE >.

Classes

class  NoThrow
 

Public Types

typedef SequenceIterator< IteratorTraits< TYPE > > Iterator
 
typedef SequenceIterator< ReadIteratorTraits< TYPE > > ReadIterator
 
typedef AllocatorEnumerator< EnumeratorTraits< TYPE > > Enumerator
 
typedef AllocatorEnumerator< ReadEnumeratorTraits< TYPE > > ReadEnumerator
 

Public Member Functions

 Allocator () noexcept
 
 Allocator (MemorySize _size)
 
 Allocator (MemorySize _size, MemorySize _capacity)
 
 Allocator (const Allocator &copy)
 
 Allocator (Allocator &&move) noexcept
 
Allocatoroperator= (const Allocator &copy)
 
Allocatoroperator= (Allocator &&move) noexcept
 
TYPE * getElements () noexcept
 
const TYPE * getElements () const noexcept
 
Span< TYPE > getSpan () noexcept
 
ConstSpan< TYPE > getSpan () const noexcept
 
Iterator getBeginIterator () noexcept
 
Iterator getEndIterator () noexcept
 
ReadIterator getBeginReadIterator () const noexcept
 
ReadIterator getEndReadIterator () const noexcept
 
Iterator begin () noexcept
 
Iterator end () noexcept
 
ReadIterator begin () const noexcept
 
ReadIterator end () const noexcept
 
Enumerator getEnumerator () noexcept
 
ReadEnumerator getReadEnumerator () const noexcept
 
MemorySize getSize () const noexcept
 
MemorySize getByteSize () const noexcept
 
bool isEmpty () const noexcept
 
void ensureCapacity (MemorySize _capacity) noexcept
 
void ensureCapacity () noexcept
 
MemorySize getCapacity () const noexcept
 
void setSizeImpl (MemorySize size, const TYPE *value)
 
void setSize (MemorySize size)
 
void setSize (MemorySize size, const TYPE &value)
 
void clear ()
 
MemoryDiff garbageCollect ()
 
MemorySize getGarbageByteSize () const noexcept
 

Static Public Member Functions

static void initialize (TYPE *dest, const TYPE *end)
 
static void initialize (TYPE *dest, const TYPE *end, const TYPE &value)
 
static void initializeByCopy (TYPE *restrict dest, const TYPE *restrict src, MemorySize count)
 
static void destroy (TYPE *begin, const TYPE *_end, MemorySize &destroyedCount)
 
static void destroy2 (TYPE *begin, const TYPE *end)
 
static void initializeByMove (TYPE *dest, TYPE *src, const TYPE *end)
 
static void initializeByMove (TYPE *dest, TYPE *src, const TYPE *end, bool &moved)
 
static MemorySize align (MemorySize offset) noexcept
 

Static Public Attributes

static constexpr bool useSafeDestroy = false
 

Protected Types

enum  MemoryFill { INIT_MEMORY = 0xaa, DESTROY_OBJECT = 0xbb, RELEASE_MEMORY = 0xdd }
 

Protected Member Functions

Span< TYPE > detach ()
 
void attach (TYPE *_buffer, MemorySize _size)
 
void attach (const Span< TYPE > &span)
 
MemorySize getAdjustedSize (MemorySize size) const noexcept
 
TYPE * allocate (const MemorySize size)
 
TYPE * resize (TYPE *buffer, const MemorySize newSize, const MemorySize originalSize)
 
bool canResizeInplace () const noexcept
 
TYPE * tryResize (TYPE *buffer, const MemorySize newSize, const MemorySize originalSize) noexcept
 
void release (TYPE *buffer, const MemorySize size)
 

Static Protected Member Functions

static void fill (TYPE *dest, MemorySize size, MemoryFill memoryFill)
 

Detailed Description

template<class TYPE>
class Allocator< TYPE >

Allocator.

Allocator of resizeable memory block. The implementation is not MT-safe.

Use capacity to avoid default construction.

See also
ReferenceCountedAllocator
Version
1.2

Constructor & Destructor Documentation

◆ Allocator() [1/3]

template<class TYPE >
Allocator< TYPE >::Allocator ( )
inlineexplicitnoexcept

Initializes an empty allocator.

◆ Allocator() [2/3]

template<class TYPE >
Allocator< TYPE >::Allocator ( MemorySize  _size)
inlineexplicit

Initializes an allocator of the specified size without initializing the elements. Raises MemoryException if unable to allocate enough memory to hold the requested number of elements.

Parameters
sizeSpecifies the initial size of the allocator.

◆ Allocator() [3/3]

template<class TYPE >
Allocator< TYPE >::Allocator ( const Allocator< TYPE > &  copy)
inline

Initializes allocator from other allocator.

Member Function Documentation

◆ allocate()

template<class TYPE >
TYPE* Allocator< TYPE >::allocate ( const MemorySize  size)
inlineprotected

Allocate new memory.

◆ attach() [1/2]

template<class TYPE >
void Allocator< TYPE >::attach ( const Span< TYPE > &  span)
inlineprotected

Attaches buffer.

◆ attach() [2/2]

template<class TYPE >
void Allocator< TYPE >::attach ( TYPE *  _buffer,
MemorySize  _size 
)
inlineprotected

Attaches buffer.

◆ begin() [1/2]

template<class TYPE >
ReadIterator Allocator< TYPE >::begin ( ) const
inlinenoexcept

Returns the first element of the allocator as a non-modifying iterator.

◆ begin() [2/2]

template<class TYPE >
Iterator Allocator< TYPE >::begin ( )
inlinenoexcept

Returns the first element of the allocator as a modifying iterator.

◆ canResizeInplace()

template<class TYPE >
bool Allocator< TYPE >::canResizeInplace ( ) const
inlineprotectednoexcept

Inplace memory resize supported.

◆ clear()

template<class TYPE >
void Allocator< TYPE >::clear ( )
inline

Clear buffer. Capacity is ignored.

◆ destroy()

template<class TYPE >
static void Allocator< TYPE >::destroy ( TYPE *  begin,
const TYPE *  _end,
MemorySize &  destroyedCount 
)
inlinestatic

Destroys the elements of the sequence. Does nothing for uninitializeable objects.

◆ detach()

template<class TYPE >
Span<TYPE> Allocator< TYPE >::detach ( )
inlineprotected

Detaches buffer.

◆ end() [1/2]

template<class TYPE >
ReadIterator Allocator< TYPE >::end ( ) const
inlinenoexcept

Returns the end of the allocator as a non-modifying iterator.

◆ end() [2/2]

template<class TYPE >
Iterator Allocator< TYPE >::end ( )
inlinenoexcept

Returns the end of the allocator as a modifying iterator.

◆ ensureCapacity() [1/2]

template<class TYPE >
void Allocator< TYPE >::ensureCapacity ( )
inlinenoexcept

Sets the capacity to the current size. Only heap will be allocated so objects won't be initialized. Required when object do not have default initialization.

◆ ensureCapacity() [2/2]

template<class TYPE >
void Allocator< TYPE >::ensureCapacity ( MemorySize  _capacity)
inlinenoexcept

Sets the capacity. Only heap will be allocated so objects won't be initialized. Required when object do not have default initialization.

◆ fill()

template<class TYPE >
static void Allocator< TYPE >::fill ( TYPE *  dest,
MemorySize  size,
MemoryFill  memoryFill 
)
inlinestaticprotected

Fills the memory with the given marker.

◆ garbageCollect()

template<class TYPE >
MemoryDiff Allocator< TYPE >::garbageCollect ( )
inline

Releases unused memory. Requested capacity is ignored. Any object must not have a cached pointer/iterator to the elements!

Returns
Returns the bytes released.

◆ getAdjustedSize()

template<class TYPE >
MemorySize Allocator< TYPE >::getAdjustedSize ( MemorySize  size) const
inlineprotectednoexcept

Returns the actual size to allocate. Ie. compensated for capacity.

◆ getBeginIterator()

template<class TYPE >
Iterator Allocator< TYPE >::getBeginIterator ( )
inlinenoexcept

Returns the first element of the allocator as a modifying iterator.

◆ getBeginReadIterator()

template<class TYPE >
ReadIterator Allocator< TYPE >::getBeginReadIterator ( ) const
inlinenoexcept

Returns the first element of the allocator as a non-modifying iterator.

◆ getByteSize()

template<class TYPE >
MemorySize Allocator< TYPE >::getByteSize ( ) const
inlinenoexcept

Returns the number of allocated bytes.

◆ getCapacity()

template<class TYPE >
MemorySize Allocator< TYPE >::getCapacity ( ) const
inlinenoexcept

Returns the capacity.

◆ getElements() [1/2]

template<class TYPE >
const TYPE* Allocator< TYPE >::getElements ( ) const
inlinenoexcept

Returns the elements of the allocator for non-modifying access.

◆ getElements() [2/2]

template<class TYPE >
TYPE* Allocator< TYPE >::getElements ( )
inlinenoexcept

Returns the elements of the allocator for modifying access.

◆ getEndIterator()

template<class TYPE >
Iterator Allocator< TYPE >::getEndIterator ( )
inlinenoexcept

Returns the end of the allocator as a modifying iterator.

◆ getEndReadIterator()

template<class TYPE >
ReadIterator Allocator< TYPE >::getEndReadIterator ( ) const
inlinenoexcept

Returns the end of the allocator as a non-modifying iterator.

◆ getEnumerator()

template<class TYPE >
Enumerator Allocator< TYPE >::getEnumerator ( )
inlinenoexcept

Returns a modifying enumerator of the allocator.

◆ getGarbageByteSize()

template<class TYPE >
MemorySize Allocator< TYPE >::getGarbageByteSize ( ) const
inlinenoexcept

Returns the number of bytes that could be garbage collected.

◆ getReadEnumerator()

template<class TYPE >
ReadEnumerator Allocator< TYPE >::getReadEnumerator ( ) const
inlinenoexcept

Returns a non-modifying enumerator of the allocator.

◆ getSize()

template<class TYPE >
MemorySize Allocator< TYPE >::getSize ( ) const
inlinenoexcept

Returns the number of elements of the allocator.

◆ initialize() [1/2]

template<class TYPE >
static void Allocator< TYPE >::initialize ( TYPE *  dest,
const TYPE *  end 
)
inlinestatic

Initializes the elements of the sequence using the default constructor. Uninitializeable objects are not initialized.

◆ initialize() [2/2]

template<class TYPE >
static void Allocator< TYPE >::initialize ( TYPE *  dest,
const TYPE *  end,
const TYPE &  value 
)
inlinestatic

Initializes the elements of the sequence using the given value. Uninitializeable objects are not initialized.

◆ initializeByCopy()

template<class TYPE >
static void Allocator< TYPE >::initializeByCopy ( TYPE *restrict  dest,
const TYPE *restrict  src,
MemorySize  count 
)
inlinestatic

Initializes the elements of the sequence by copying elements from other sequence. The memory image is copied directly for relocatable objects.

◆ initializeByMove() [1/2]

template<class TYPE >
static void Allocator< TYPE >::initializeByMove ( TYPE *  dest,
TYPE *  src,
const TYPE *  end 
)
inlinestatic

Initializes the elements of the sequence by moving elements from other sequence. Arrays must not overlap.

◆ initializeByMove() [2/2]

template<class TYPE >
static void Allocator< TYPE >::initializeByMove ( TYPE *  dest,
TYPE *  src,
const TYPE *  end,
bool &  moved 
)
inlinestatic

Initializes the elements of the sequence by moving elements from other sequence. Arrays must not overlap.

◆ isEmpty()

template<class TYPE >
bool Allocator< TYPE >::isEmpty ( ) const
inlinenoexcept

Returns true if no elements are allocated.

◆ operator=()

template<class TYPE >
Allocator& Allocator< TYPE >::operator= ( const Allocator< TYPE > &  copy)
inline

Assignment of allocator by allocator.

◆ release()

template<class TYPE >
void Allocator< TYPE >::release ( TYPE *  buffer,
const MemorySize  size 
)
inlineprotected

Release memory buffer.

◆ resize()

template<class TYPE >
TYPE* Allocator< TYPE >::resize ( TYPE *  buffer,
const MemorySize  newSize,
const MemorySize  originalSize 
)
inlineprotected

Resize memory buffer.

◆ setSize() [1/2]

template<class TYPE >
void Allocator< TYPE >::setSize ( MemorySize  size)
inline

Sets the number of elements of the allocator. If the size is increased the original elements are not modified and the newly allocated elements are not initialized. Raises MemoryException if unable to allocate additional memory (does not throw an exception when the size is decreased or unchanged). If the size is reduced the elements up to the new size are unchanged.

Parameters
sizeThe desired size.

◆ setSize() [2/2]

template<class TYPE >
void Allocator< TYPE >::setSize ( MemorySize  size,
const TYPE &  value 
)
inline

Same as setSize() but fills new elements with the given value instead of using default initialization.

◆ setSizeImpl()

template<class TYPE >
void Allocator< TYPE >::setSizeImpl ( MemorySize  size,
const TYPE *  value 
)
inline

Sets the number of elements of the allocator. If the size is increased the original elements are not modified and the newly allocated elements are not initialized. Raises MemoryException if unable to allocate additional memory (does not throw an exception when the size is decreased or unchanged). If the size is reduced the elements up to the new size are unchanged.

Parameters
sizeThe desired size.

◆ tryResize()

template<class TYPE >
TYPE* Allocator< TYPE >::tryResize ( TYPE *  buffer,
const MemorySize  newSize,
const MemorySize  originalSize 
)
inlineprotectednoexcept

Try to inplace resize buffer.