ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
MonoAllocator Class Reference

Description:


Implements a monotonic allocator. Allocates a series of bigger memory chunks and offers sequential allocation of portions of those. The allocations can be reset to a certain state (see TakeSnapshot), which allows to reuse the allocated chunks for future sequential allocations.

The size of the chunks allocated is defined with constructor parameters initialChunkSize . This value is reduced by MaxUsableSpaceLoss and stored in field nextChunksUsableSize. With each allocation of a chunk this value can be increased. Constructor parameter chunkGrowthInPercent defaults to 200, which doubles the next chunk size.

If an invocation of one of the allocation methods requests memory bigger than the remaining space in the actual (last) chunk, then a new chunk is created and made the actual chunk. The remaining space of the former actual chunk will not be used for future allocations and is lost in this respect, except for the following exclamation.

If a requested allocation size exceeds nextChunksUsableSize, then a new chunk with the requested size is created, stored in the list of allocated chunks and the current chunk remains in use. With that, requested allocations are allowed to have a bigger size than the standard chunk size given in the constructor.

Depending on compiler symbol ALIB_DEBUG_MONOMEM some metrics on instances of this class become available. Those might for example be helpful to find a reasonable value for constructor parameter initialChunkSize .

Definition at line 76 of file monoallocator.hpp.

#include <monoallocator.hpp>

Collaboration diagram for MonoAllocator:
[legend]

Inner Type Index:

struct  Chunk
 
struct  DbgStatistics
 
class  Snapshot
 

Public Static Method Index:

static ALIB_API MonoAllocatorCreate (size_t initialChunkSize, unsigned int chunkGrowthInPercent=200)
 

Public Field Index:

DbgStatistics DbgStats
 
String LogDomain = A_CHAR("MA")
 

Public Method Index:

ALIB_API MonoAllocator (size_t initialChunkSize, unsigned int chunkGrowthInPercent=200)
 
ALIB_API ~MonoAllocator ()
 
template<typename T >
ALIB_FORCE_INLINE T * Alloc ()
 
ALIB_FORCE_INLINE char * Alloc (size_t size, size_t alignment)
 
template<typename T , typename TSize >
ALIB_FORCE_INLINE T * AllocArray (TSize length)
 
ALIB_API NAString DbgDumpStats ()
 
template<typename T , typename... TArgs>
ALIB_FORCE_INLINE T * Emplace (TArgs &&... args)
 
template<typename T , typename TSize , typename... TArgs>
T * EmplaceArray (TSize length, TArgs &&... args)
 
template<typename TChar >
strings::TString< TChar > EmplaceString (const strings::TString< TChar > &src)
 
ALIB_API void Reset (const Snapshot &snapshot=Snapshot())
 
Snapshot TakeSnapshot ()
 

Field Details:

◆ chunk

Chunk* chunk
protected

The actual chunk. Contains a link to previously allocated chunks.

Definition at line 214 of file monoallocator.hpp.

◆ chunkGrowthInPercent

unsigned int chunkGrowthInPercent
protected

Growth factor of subsequently allocated chunks. Given by a construction parameter, which defaults to 200, which doubles chunk size with each next chunk allocation.

Definition at line 235 of file monoallocator.hpp.

◆ DbgStats

DbgStatistics DbgStats

Debug statistics measured on the whole run-time of this object.
Availability depends on code selector symbol ALIB_DEBUG_MONOMEM.

Definition at line 275 of file monoallocator.hpp.

◆ LogDomain

String LogDomain = A_CHAR("MA")

Sub-domain for debug log output of this class. Could be changed to allow fine grained log-output selection. The following domains are set by ALib types:

Type Sub-Domain
- GlobalAllocator "MA/GLBL"
- InMemoryPlugin "MA/CFG/IMPLGN"
- Variable "MA/CFG/VAR"
- Lox "MA/ALOX/LOX"
- Compiler "MA/EXPR/CMPLR"
- Scope "MA/EXPR/SCP", respectively "MA/EXPR/CTSCP"
- Calculus "MA/EXPR/CLCLS"

Availability depends on code selector symbol ALIB_DEBUG_MONOMEM.

Definition at line 294 of file monoallocator.hpp.

◆ nextChunksUsableSize

size_t nextChunksUsableSize
protected

The initial allocation size given in the constructor minus the maximum overhead caused by storing a chunk's management data inside the chunks themselves. In other words, this field stores the value given in the constructor, minus the value returned by MaxUsableSpaceLoss .

Allocated chunks may be bigger in the case that a single allocation is larger than this value.

Definition at line 228 of file monoallocator.hpp.

◆ recyclables

Chunk* recyclables
protected

The list of chunks that are to be recycled.

Definition at line 217 of file monoallocator.hpp.

Constructor(s) / Destructor Details::

◆ MonoAllocator() [1/2]

MonoAllocator ( size_t initialChunkSize,
unsigned int chunkGrowthInPercent = 200 )

Constructor. MaxUsableSpaceLoss is subtracted from given initialChunkSize and stored in field nextChunksUsableSize.

Parameters
chunkGrowthInPercentOptional growth factor in percent (*100), applied to each allocation of a next chunk size. Values provided should be greater than 100.

Defaults to 200, which doubles chunk size with each next internal chunk allocation.

Parameters
initialChunkSizeThe standard size of memory chunks that are dynamically allocated.

Definition at line 118 of file monoallocator.cpp.

◆ ~MonoAllocator()


Destructor. Frees all allocated memory chunks.

Definition at line 145 of file monoallocator.cpp.

Here is the call graph for this function:

◆ MonoAllocator() [2/2]

MonoAllocator ( Chunk * firstChunk,
size_t initialChunkSize,
unsigned int chunkGrowthInPercent )
protected

Protected constructor taking a first chunk of memory that was allocated externally. This constructor is used by static method Create which uses placement new to create this allocate inside the chunk given.

Parameters
firstChunkThe first chunk already created outside.
initialChunkSizeThe size of the first chunk allocated.
chunkGrowthInPercentGrowth factor in percent (*100), applied to each allocation of a next chunk size.

Definition at line 99 of file monoallocator.cpp.

Method Details:

◆ Alloc() [1/2]

template<typename T >
ALIB_FORCE_INLINE T * Alloc ( )
inline

Allocates aligned memory suitable to emplace an instance of T .

See also
For a version that also constructs the type, see Emplace.
Template Parameters
TThe type of object to allocated memory for.
Returns
A pointer to the allocated memory.

Definition at line 459 of file monoallocator.hpp.

Here is the call graph for this function:

◆ Alloc() [2/2]

ALIB_FORCE_INLINE char * Alloc ( size_t size,
size_t alignment )
inline

Allocates memory of requested size and alignment. This method may be used if templated method Alloc is not applicable.

Parameters
sizeThe allocation size requested.
alignmentThe required alignment.
Returns
A pointer to the allocated memory.

Definition at line 416 of file monoallocator.hpp.

Here is the call graph for this function:

◆ AllocArray()

template<typename T , typename TSize >
ALIB_FORCE_INLINE T * AllocArray ( TSize length)
inline

Allocates aligned memory for an array of objects of type T of size length .

See also
Method EmplaceArray for an alternative that initializes the memory.
Template Parameters
TThe array element type.
TSizeThe type of the array length value. This template parameter is provided for convenience, to avoid casts from singed types. It is deduced by the compiler and not needed to be provided.
Parameters
lengthThe capacity of the requested array.
Returns
A pointer to the first element of the allocated array.

Definition at line 479 of file monoallocator.hpp.

Here is the call graph for this function:

◆ Create()

MonoAllocator * Create ( size_t initialChunkSize,
unsigned int chunkGrowthInPercent = 200 )
static

This static method creates an object of this type inside "itself", aka inside its first allocated chunk. Objects created with this method, have to be deleted by only invoking the destructor, which also deletes the object the returned pointer refers to.

The parameterless version of method Reset must not be called with objects created by this method. Instead, if reset operations are desired, a snapshot has to be taken (see method TakeSnapshot) right after the invocation of this method (aka before allocations are performed) which then has to be passed to the overloaded method Reset(const Snapshot&).

Parameters
initialChunkSizeThe size of memory chunks allocated.
chunkGrowthInPercentOptional growth factor in percent (*100), applied to each allocation of a next chunk size. Values provided should be greater than 100.

Defaults to 200, which doubles chunk size with each next internal chunk allocation.

Returns
A MonoAllocator object residing in its first created chunk.

Definition at line 131 of file monoallocator.cpp.

Here is the call graph for this function:

◆ DbgDumpStats()

NAString DbgDumpStats ( )

Provides allocation statistics for manual performance optimization. This method is available only if code selector symbol ALIB_DEBUG_MONOMEM is set.

Returns
Some textual information on the allocation statistics.

Definition at line 350 of file monoallocator.cpp.

Here is the call graph for this function:

◆ Emplace()

template<typename T , typename... TArgs>
ALIB_FORCE_INLINE T * Emplace ( TArgs &&... args)
inline

Allocates aligned memory of size and alignment suitable for type T and performs a C++ "placement new", passing the given arguments to the type's constructor.

Template Parameters
TType to emplace in this monotonic allocator.
TArgsTypes of variadic parameters given with parameter args .
Parameters
argsVariadic parameters to be forwarded to constructor of type T .
Returns
A pointer to the allocated memory.

Definition at line 496 of file monoallocator.hpp.

◆ EmplaceArray()

template<typename T , typename TSize , typename... TArgs>
T * EmplaceArray ( TSize length,
TArgs &&... args )
inline

Allocates aligned memory for an array of objects of type T of size* length . Array members are initialized using a "placement new" passing the given args to the type's constructor.

See also
Method AllocArray for an alternative that does not initialize the memory.
Template Parameters
TThe array element type.
TArgsTypes of variadic parameters given with parameter args .
TSizeThe type of the array length value. This template parameter is provided for convenience, to avoid casts from singed types. It is deduced by the compiler and not needed to be provided.
Parameters
lengthThe capacity of the requested array.
argsVariadic parameters to be forwarded to constructor of each array element of type T .
Returns
A pointer to the first element of the allocated array.

Definition at line 520 of file monoallocator.hpp.

◆ EmplaceString()

template<typename TChar >
strings::TString< TChar > EmplaceString ( const strings::TString< TChar > & src)
inline

Returns a copy of the given string.

Note
In case src is empty, the a copy of the original string is returned (aka a string that points to the same buffer that was given). This allows to have a sort of "valid" pointer, even though the buffer of src might be a temporary.
Template Parameters
TCharThe character type of the input and output string.
Parameters
srcThe source string to copy
Returns
A string object representing the copy.

Definition at line 583 of file monoallocator.hpp.

Here is the call graph for this function:

◆ getCreateChunk()

char * getCreateChunk ( size_t size,
size_t alignment )
protected

This internal allocation method is called by the allocation interface methods, in case the current request can not be trivially satisfied.

Implements the overall strategy of this class in respect to oversized blocks, recycling of blocks, etc.

Parameters
sizeThe size of the memory to allocate.
alignmentThe allocation alignment needed.
Returns
A pointer to the allocated memory.

Definition at line 258 of file monoallocator.cpp.

Here is the call graph for this function:

◆ MaxUsableSpaceLoss()

static constexpr size_t MaxUsableSpaceLoss ( )
inlinestaticconstexprprotected

The maximum amount of bytes that are lost by the fact that a chunk's data is stored at the start of the chunk. This is the sum of the chunk's size plus the waste caused by aligning an object of maximum possible alignment, if allocated as a first object behind a chunk's management data.

Returns
The maximum space loss in allocated chunks.

Definition at line 202 of file monoallocator.hpp.

◆ Reset()

void Reset ( const Snapshot & snapshot = Snapshot())

Resets this allocator to the given Snapshot . Parameter snapshot is defaulted with a default-constructed Snapshot, which completely resets the allocator.

With a reset, the memory chunks which had been allocated after taking the given snapshot , are not freed, but re-used with future monotonic allocations.

This method is useful in cases where some permanent objects which are allocated first have to be preserved with resets. It can also be used to preserve a self-contained monotonic allocator created with static method Create.

Note that snapshots taken after the given one, become invalid. This is because class Snapshot is only a simple lightweight class that marks the currently used chunk and its fill level.

Parameters
snapshotThe snapshot to reset to.

Definition at line 195 of file monoallocator.cpp.

Here is the call graph for this function:

◆ TakeSnapshot()

Snapshot TakeSnapshot ( )
inline

Saves the current state of the allocator and returns this information as a Snapshot value. Such snapshots may be passed to method Reset(const Snapshot&).

Note that the actual memory is not copied and restored. In this respect the word "Snapshot" is overstating. What is stored are the current use of memory, but not it's contents.

Returns
A (lightweight) snapshot value object.

Definition at line 542 of file monoallocator.hpp.


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