200
, resulting in doubling the size of each next buffer.If an invocation of one of the allocation methods requests memory bigger than the remaining space in the actual (last) buffer, then a new buffer is created and made the actual buffer. The remaining space of the former actual buffer will not be used for future allocations and is wasted in this respect.
TAllocator | The underlying allocator to use. With type definition MonoAllocator, this is bound to type HeapAllocator. For general information see chapter 8. Allocator Chaining of the Programmer's Manual. |
Definition at line 338 of file monoallocator.hpp.
#include <monoallocator.hpp>
Public Type Index: | |
using | ChainedAllocator = TAllocator |
Public Type Index: inherited from AllocatorMember< TAllocator > | |
using | AllocatorType = TAllocator |
Exposes the allocator type. | |
Public Static Field Index: | |
static constexpr size_t | MAX_ALIGNMENT = (std::numeric_limits<size_t>::max)() |
static constexpr size_t | MIN_ALIGNMENT = 1 |
Public Field Index: | |
lang::Placeholder< lang::DbgCriticalSections > | DbgCriticalSectionsPH |
const char * | DbgName |
Public Method Index: | |
TMonoAllocator (const char *dbgName, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200) | |
TMonoAllocator (const char *dbgName, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200) | |
TMonoAllocator (const char *dbgName, std::nullptr_t) noexcept | |
TMonoAllocator (const char *dbgName, TAllocator &pAllocator, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200) | |
ALIB_API | TMonoAllocator (const char *dbgName, TAllocator &pAllocator, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200) |
TMonoAllocator (const TMonoAllocator &)=delete | |
Not copyable. | |
TMonoAllocator (TMonoAllocator &&)=delete | |
Not movable. | |
ALIB_API | ~TMonoAllocator () |
Destructor. Disposes all memory allocated with ChainedAllocator. | |
lang::Allocator Implementation | |
void * | allocate (size_t size, size_t alignment) |
void * | reallocate (void *mem, size_t oldSize, size_t newSize, size_t alignment) |
void | free (void *mem, size_t size) const |
template<typename TSize > | |
void | dbgAcknowledgeIncreasedAllocSize (void *, TSize) const |
lang::AllocatorInterface< TMonoAllocator > | operator() () |
constexpr bool | allowsMemSplit () noexcept |
Snapshots and Reset | |
Snapshot | TakeSnapshot () |
ALIB_API void | Reset (Snapshot snapshot=Snapshot()) |
void | Reset (size_t firstObjectSize, size_t firstObjectAlignment) |
Statistics and Debug-Interface | |
ALIB_API void | GetStatistics (Statistics &result) |
ALIB_FORCE_INLINE void | DbgLock (bool onOff) noexcept |
template<typename TSize > | |
void | dbgCheckMemory (void *mem, TSize size) |
const DbgStatistics & | DbgGetStatistics () const |
ALIB_API NAString | DbgDumpStatistics () |
Public Method Index: inherited from AllocatorMember< TAllocator > | |
AllocatorMember ()=delete | |
Deleted default constructor. (The allocator has to be given with construction) | |
AllocatorMember (TAllocator &pAllocator) noexcept | |
AllocatorInterface< TAllocator > | AI () const noexcept |
TAllocator & | GetAllocator () const noexcept |
Protected Type Index: | |
using | allocMember = lang::AllocatorMember<TAllocator> |
The allocator type that TAllocator specifies. | |
Protected Field Index: | |
detail::Buffer * | buffer |
The actual buffer. Contains a link to previously allocated buffers. | |
unsigned int | bufferGrowthInPercent |
bool | dbgLock = false |
DbgStatistics | dbgStats |
size_t | nextBuffersUsableSize |
detail::Buffer * | recyclables |
The list of buffers that are to be recycled. | |
Protected Field Index: inherited from AllocatorMember< TAllocator > | |
TAllocator & | allocator |
A reference to the allocator. | |
Protected Method Index: | |
ALIB_API char * | nextBuffer (size_t size, size_t alignment) |
|
protected |
The allocator type that TAllocator specifies.
Definition at line 342 of file monoallocator.hpp.
using ChainedAllocator = TAllocator |
The type of the allocator that this allocator uses underneath to allocate the buffers, given with template parameter TAllocator.
The instance can be accessed with inherited methods GetAllocator and AI.
Definition at line 409 of file monoallocator.hpp.
|
protected |
The actual buffer. Contains a link to previously allocated buffers.
Definition at line 345 of file monoallocator.hpp.
|
protected |
Growth factor of subsequently allocated buffers. Given by a construction parameter, which in most cases defaults to 200
, doubling the buffer size with each next buffer allocation.
Definition at line 358 of file monoallocator.hpp.
lang::Placeholder<lang::DbgCriticalSections> DbgCriticalSectionsPH |
Due to the possibility of beeing self-contained, this type needs special treatment in respect to instance DbgCriticalSections. It must be neither derived nor being a member. Instead, destruction has to be controlled. Furthermore, the ALib Module Threads Macros cannot be used.
Definition at line 386 of file monoallocator.hpp.
|
protected |
This flag to mark this allocator to not accept allocations.
Definition at line 370 of file monoallocator.hpp.
const char* DbgName |
A name for this object. With debug-compilations, this name has to be given with construction.
Definition at line 364 of file monoallocator.hpp.
|
protected |
Debug statistics measured on the whole run-time of this object.
Availability depends on code selector symbol ALIB_DEBUG_MONOMEM.
Definition at line 376 of file monoallocator.hpp.
|
staticconstexpr |
Evaluates to std::numeric_limits<size_t>::max()
.
Definition at line 417 of file monoallocator.hpp.
|
staticconstexpr |
Evaluates to 1.
Definition at line 413 of file monoallocator.hpp.
|
protected |
The initial allocation size given in the constructor, multiplied with bufferGrowthInPercent with each new buffer created. Allocated buffers may be bigger in the case that a single allocation is larger than this value.
Definition at line 353 of file monoallocator.hpp.
|
protected |
The list of buffers that are to be recycled.
Definition at line 348 of file monoallocator.hpp.
|
noexcept |
Special constructor that is not initializing this type. This can be used if a value of this type is required, but only later it becomes known what the initial buffer size and growth factor is to be.
Method IsInitialized can be used to determine if an allocator was initialized on construction. If not, a placement-new calling a decent constructor is to be performed before construction.
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
TMonoAllocator | ( | const char * | dbgName, |
detail::Buffer * | pInitialBuffer, | ||
size_t | pInitialBufferSizeInKB, | ||
unsigned int | pBufferGrowthInPercent = 200 ) |
Constructor that accepts an external first buffer. If this constructor is used, and the given initialBuffer must not be freed to the heap, method destructWithExternalBuffer has to be called right before the allocator is destructed.
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
pInitialBuffer | The initial buffer to use. Has to be aligned with at least alignof(void*) . |
pInitialBufferSizeInKB | The size in kB (1024 bytes) of the given initialBuffer. |
pBufferGrowthInPercent | Optional growth factor in percent, applied to each allocation of a next buffer size in respect to its previous size. Defaults to 200 , which doubles buffer size with each next internal buffer allocation. |
TMonoAllocator | ( | const char * | dbgName, |
TAllocator & | pAllocator, | ||
detail::Buffer * | pInitialBuffer, | ||
size_t | pInitialBufferSizeInKB, | ||
unsigned int | pBufferGrowthInPercent = 200 ) |
Alternative constructor version that in addition expects an allocator.
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
pAllocator | The allocator to use for allocation of buffers. |
pInitialBuffer | The initial buffer to use. Has to be aligned with at least alignof(void*) . |
pInitialBufferSizeInKB | The size in kB (1024 bytes) of the given initialBuffer. |
pBufferGrowthInPercent | Optional growth factor in percent, applied to each allocation of a next buffer size in respect to its previous size. Defaults to 200 , which doubles buffer size with each next internal buffer allocation. |
TMonoAllocator | ( | const char * | dbgName, |
size_t | pInitialBufferSizeInKB, | ||
unsigned int | pBufferGrowthInPercent = 200 ) |
Constructor. A first memory buffer is allocated from ChainedAllocator.
Parameter bufferGrowthInPercent determines the growth of memory buffers. The size of a next buffer is calculated as: newSize= (previousSize * bufferGrowthInPercent) / 100
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
pInitialBufferSizeInKB | The size in kB (1024 bytes) of the first memory buffer used for the allocator itself as well as for the first allocations. |
pBufferGrowthInPercent | Optional growth factor in percent, applied to each allocation of a next buffer size in respect to its previous size. Defaults to 200 , which doubles the buffer size with each next internal buffer allocation. |
ALIB_API TMonoAllocator | ( | const char * | dbgName, |
TAllocator & | pAllocator, | ||
size_t | pInitialBufferSizeInKB, | ||
unsigned int | pBufferGrowthInPercent = 200 ) |
Alternative constructor accepting an allocator instance. To be used if template parameter TAllocator does not equal HeapAllocator or any (custom) default-constructible allocator type.
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
pAllocator | The allocator to use for allocation of buffers. |
pInitialBufferSizeInKB | The size in kB (1024 bytes) of the first memory buffer used for the allocator itself as well as for the first allocations. |
pBufferGrowthInPercent | Optional growth factor in percent, applied to each allocation of a next buffer size in respect to its previous size. Defaults to 200 , which doubles the buffer size with each next internal buffer allocation. |
~TMonoAllocator | ( | ) |
Destructor. Disposes all memory allocated with ChainedAllocator.
Definition at line 59 of file monoallocator.t.hpp.
|
inline |
Allocate memory from the internal buffer. If the buffer's size is exceeded, a next buffer is allocated and used.
size | The size of memory the block to allocate in bytes. With this allocator this is not an input/output parameter. |
alignment | The (minimum) alignment of the memory block to allocate in bytes. See Chapter 11.1 Alignment of the Programmer's Manual of this module. |
Definition at line 756 of file monoallocator.hpp.
|
constexprnoexcept |
See the description of this method with prototype Allocator::allowsMemSplit.
(Note: This method is static. For technical reasons this cannot be reflected in this documentation)
true
, except if compiler symbol ALIB_DEBUG_ALLOCATIONS is given.
|
inlinestatic |
Same as Create(const char*, TAllocator&, size_t, unsigned int), but misses the allocator parameter. Used with chained allocators that are default-constructible (e.g., HeapAllocator):
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
initialBufferSizeInKB | The size of memory the buffers allocated in kilobytes. |
bufferGrowthInPercent | Optional growth factor in percent, applied to the buffer size with each next buffer allocation. Values provided should be greater than 100. Defaults to 200 , which doubles buffer size with each next internal buffer allocation. |
Definition at line 695 of file monoallocator.hpp.
|
static |
This static method creates an object of this type inside "itself", aka inside its first allocated buffer. Instances created with this method have to be deleted by only invoking the destructor, e.g., using Destruct.
Method Reset must not be called using its default parameter when an instance of this type was 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 and maybe other initial members that should survive a reset, which then has to be passed to method Reset.
Alternatively, if only the monotonic allocator should survive the reset, overloaded method Reset(size_t, size_t) might be used, passing sizeof(TMonoAllocator<T>)
and alignof(TMonoAllocator<T>)
as parameters.
dbgName | Has to be specified with debug-compilations only. Use macro ALIB_DBG to pass a constant. |
pAllocator | The allocator used for creating the first buffer. |
initialBufferSizeInKB | The size of memory the buffers allocated in kilobytes. |
bufferGrowthInPercent | Optional growth factor in percent, applied to the buffer size with each next buffer allocation. Values provided should be greater than 100. Defaults to 200 , which doubles buffer size with each next internal buffer allocation. |
|
inline |
This is an empty implementation of the prototyped method. It is empty because this allocator never returns a higher allocation size than requested. Allocator::dbgAcknowledgeIncreasedAllocSize.
TSize | The type of parameter allocSize. (Deduced by the compiler.) |
Definition at line 863 of file monoallocator.hpp.
|
inline |
If the compiler symbol ALIB_DEBUG_ALLOCATIONS is not set, this method is empty and will be optimized out. Otherwise, this will raise an ALib assertion if the piece of allocated memory is corrupted or its allocation size is not rightfully given by the using code.
TSize | The type of parameter size. (Deduced by the compiler.) |
mem | The address of the allocated object. |
size | The requested allocation size of the object. |
Definition at line 979 of file monoallocator.hpp.
NAString DbgDumpStatistics | ( | ) |
Provides allocation statistics for manual performance optimization.
Definition at line 306 of file monoallocator.t.hpp.
|
inline |
Returns allocation statistics for manual performance optimization.
Definition at line 990 of file monoallocator.hpp.
|
inlinenoexcept |
Lock or unlock this allocator. If locked, an ALib assertion is raised if allocations are performed. This can be quite useful to detect allocations with an allocator that is shared between different code entities and to enforce certain allocation contracts.
With release-compilations, this method is empty and optimized out.
onOff | The state to set. |
Definition at line 967 of file monoallocator.hpp.
void destructWithExternalBuffer | ( | ) |
This method has to be called before destruction of an instance, in the case that instance was allocated using a special version of the constructors that accept an external Buffer. This will remove the initially given buffer from the list of buffers, and thus this buffer not be deleted. Only if the memory was simply heap-allocated using std::malloc
, C++ new operator
, or HeapAllocator, and if the memory is intended to be freed, this method may not be called. As a sample, class TLocalAllocator, which uses stack-memory for the first buffer, will call this method in its destructor.
Note that as an exception to the rule, this method's name starts with a lowercase letter as if it was protected, while it has to be public.
Definition at line 195 of file monoallocator.t.hpp.
|
inline |
This method is empty for this allocator and optimized out. Only if compiler symbol ALIB_DEBUG_ALLOCATIONS is set, the method will overwrite the freed memory with character 0xD2
. This is why method allowsMemSplit returns false
if that symbol is set and some few optimizations will not be performed in that mode, for example, the reuse of bucket arrays as recycled node objects when resizing hashtables.
mem | The memory to free. |
size | The allocated size. |
Definition at line 851 of file monoallocator.hpp.
void GetStatistics | ( | Statistics & | result | ) |
Fills the given result record with statistical information about this allocator.
result | The object to write the result into. (Will be reset before use.) |
Definition at line 271 of file monoallocator.t.hpp.
|
inlinenoexcept |
Tests if non-initializing constructor TMonoAllocator(const char*, std::nullptr_t) was used.
false
if this allocator needs to be initialized by performing a placement-new on this
. Definition at line 740 of file monoallocator.hpp.
|
protected |
This internal allocation method is called by the allocation interface methods, in case the current request cannot be trivially satisfied.
Implements the overall strategy of this class in respect to oversized blocks, recycling of blocks, etc.
size | The size of the first object to allocate in the buffer. |
alignment | The allocation alignment of the first object to allocate. |
Definition at line 213 of file monoallocator.t.hpp.
|
inline |
Returns a temporary object (which is usually optimized out together with a call to this operator) providing high-level convenience methods for allocation.
Definition at line 869 of file monoallocator.hpp.
|
inline |
Grows a piece of memory. If a new allocation had to be performed, the existing data is copied. Note that this allocator implementation never shrinks memory, thus if oldSize is greater than newSize, the original memory is returned.
mem | The memory to reallocate. |
oldSize | The current size of mem. |
newSize | The now required size of mem in bytes. With this allocator this is not an input/output parameter. |
alignment | The (minimum) alignment of the memory block to allocate in bytes. (Has to be the same as before, but this is not tested here). See Chapter 11.1 Alignment of the Programmer's Manual of this module. |
Definition at line 815 of file monoallocator.hpp.
|
inline |
Special version of Reset(Snapshot) which resets this allocator to the first buffer and within that, behind the first object of the given size.
This method is used by class TSharedMonoVal to avoid the need of storing a snapshot behind itself.
If the compiler symbol ALIB_DEBUG_ALLOCATIONS is set, then all freed memory is overwritten with 0xD2
. This helps to identify invalid reset operations.
firstObjectSize | The size of the first emplaced object. |
firstObjectAlignment | The alignment of the first emplaced object. |
Definition at line 940 of file monoallocator.hpp.
void Reset | ( | monomem::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 buffers which had been allocated after taking the given snapshot, are not released back to the operating system, 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.
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 buffer and its fill level.
snapshot | The snapshot to reset to. |
Definition at line 108 of file monoallocator.t.hpp.
|
inline |
Saves the current state of the allocator and returns this information as a Snapshot value. Such snapshots may be passed to method Reset(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 903 of file monoallocator.hpp.