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>
Inner Type Index: | |
struct | Chunk |
struct | DbgStatistics |
class | Snapshot |
Public Static Method Index: | |
static ALIB_API MonoAllocator * | Create (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 () |
|
protected |
The actual chunk. Contains a link to previously allocated chunks.
Definition at line 214 of file monoallocator.hpp.
|
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.
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.
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.
|
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.
|
protected |
The list of chunks that are to be recycled.
Definition at line 217 of file monoallocator.hpp.
MonoAllocator | ( | size_t | initialChunkSize, |
unsigned int | chunkGrowthInPercent = 200 ) |
Constructor. MaxUsableSpaceLoss is subtracted from given initialChunkSize and stored in field nextChunksUsableSize.
chunkGrowthInPercent | Optional 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.
initialChunkSize | The 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.
|
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.
firstChunk | The first chunk already created outside. |
initialChunkSize | The size of the first chunk allocated. |
chunkGrowthInPercent | Growth factor in percent (*100), applied to each allocation of a next chunk size. |
Definition at line 99 of file monoallocator.cpp.
|
inline |
Allocates aligned memory suitable to emplace an instance of T .
T | The type of object to allocated memory for. |
Definition at line 459 of file monoallocator.hpp.
|
inline |
Allocates memory of requested size and alignment. This method may be used if templated method Alloc is not applicable.
size | The allocation size requested. |
alignment | The required alignment. |
Definition at line 416 of file monoallocator.hpp.
|
inline |
Allocates aligned memory for an array of objects of type T of size length .
T | The array element type. |
TSize | The 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. |
length | The capacity of the requested array. |
Definition at line 479 of file monoallocator.hpp.
|
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&).
initialChunkSize | The size of memory chunks allocated. |
chunkGrowthInPercent | Optional 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.
Definition at line 131 of file monoallocator.cpp.
NAString DbgDumpStats | ( | ) |
Provides allocation statistics for manual performance optimization. This method is available only if code selector symbol ALIB_DEBUG_MONOMEM is set.
Definition at line 350 of file monoallocator.cpp.
|
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.
T | Type to emplace in this monotonic allocator. |
TArgs | Types of variadic parameters given with parameter args . |
args | Variadic parameters to be forwarded to constructor of type T . |
Definition at line 496 of file monoallocator.hpp.
|
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.
T | The array element type. |
TArgs | Types of variadic parameters given with parameter args . |
TSize | The 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. |
length | The capacity of the requested array. |
args | Variadic parameters to be forwarded to constructor of each array element of type T . |
Definition at line 520 of file monoallocator.hpp.
|
inline |
Returns a copy of the given string.
TChar | The character type of the input and output string. |
src | The source string to copy |
Definition at line 583 of file monoallocator.hpp.
|
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.
size | The size of the memory to allocate. |
alignment | The allocation alignment needed. |
Definition at line 258 of file monoallocator.cpp.
|
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.
Definition at line 202 of file monoallocator.hpp.
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.
snapshot | The snapshot to reset to. |
Definition at line 195 of file monoallocator.cpp.
|
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.
Definition at line 542 of file monoallocator.hpp.