8#ifndef HPP_ALIB_MONOMEM_MONOALLOCATOR
9#define HPP_ALIB_MONOMEM_MONOALLOCATOR 1
11#if !defined (HPP_ALIB_LANG_TMP) && !defined(ALIB_DOX)
15#if !defined (HPP_ALIB_MONOMEM_FWDS)
20# if !defined(HPP_ALIB_THREADS_THREADLOCK)
26# if !defined(HPP_ALIB_STRINGS_STRING)
32#if !defined(_GLIBCXX_CSTDLIB) && !defined(_CSTDLIB_)
36#if !defined (_GLIBCXX_MEMORY) && !defined(_MEMORY_)
40#if !defined (_GLIBCXX_CSTDDEF) && !defined (_CSTDDEF_)
46namespace alib {
namespace monomem {
78 #if !defined(ALIB_DOX)
79 template<
typename TContained>
145 act=
reinterpret_cast<char*
>(
this + 1 );
158 char*
alloc(
size_t requestedSize,
size_t alignment )
161 size_t freeSpace=
static_cast<size_t>(
end-
act );
163 if( !std::align( alignment, requestedSize, result, freeSpace) )
165 act=
reinterpret_cast<char*
>( result ) + requestedSize;
166 return reinterpret_cast<char*
>( result );
178 template<
size_t requestedSize,
size_t alignment>
183 size_t freeSpace=
static_cast<size_t>(
end-
act );
184 if( !std::align( alignment, requestedSize, result, freeSpace) )
186 act=
reinterpret_cast<char*
>( result ) + requestedSize;
188 return reinterpret_cast<char*
>( result );
205 +
sizeof(
Chunk) %
alignof(std::max_align_t);
238 #if ALIB_DEBUG_MONOMEM
311 #if !defined(ALIB_DOX)
348 return chunk !=
nullptr;
399 #if ALIB_THREADS && ALIB_CAMP && !defined(ALIB_DOX)
400 ALIB_API void dbgCheckGlobalAllocatorLock();
416 char*
Alloc(
size_t size,
size_t alignment)
418 #if ALIB_THREADS && ALIB_CAMP
419 dbgCheckGlobalAllocatorLock();
422 #if ALIB_DEBUG_MONOMEM
436 #if ALIB_DEBUG_MONOMEM
461 return reinterpret_cast<T*
>(
Alloc(
sizeof(T),
alignof(T) ) );
477 template<
typename T,
typename TSize>
481 return reinterpret_cast<T*
>(
Alloc(
sizeof(T[1]) *
static_cast<size_t>(length),
494 template<
typename T,
typename... TArgs>
498 return new (Alloc<T>()) T( std::forward<TArgs>( args )... );
519 template<
typename T,
typename TSize,
typename... TArgs>
523 T* mem= AllocArray<T, TSize>( length );
525 for( TSize i= 0 ; i < length ; ++i )
526 new (mem + i) T(std::forward<TArgs>( args )...);
582 template<
typename TChar>
588 TChar* mem= AllocArray<TChar>( src.
Length() );
593 #if ALIB_DEBUG_MONOMEM
Snapshot(MonoAllocator *allocator)
ALIB_FORCE_INLINE T * Alloc()
ALIB_FORCE_INLINE char * Alloc(size_t size, size_t alignment)
ALIB_FORCE_INLINE T * Emplace(TArgs &&... args)
T * EmplaceArray(TSize length, TArgs &&... args)
unsigned int chunkGrowthInPercent
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
size_t nextChunksUsableSize
ALIB_API NAString DbgDumpStats()
static ALIB_API MonoAllocator * Create(size_t initialChunkSize, unsigned int chunkGrowthInPercent=200)
ALIB_API void Reset(const Snapshot &snapshot=Snapshot())
ALIB_FORCE_INLINE T * AllocArray(TSize length)
ALIB_API char * getCreateChunk(size_t size, size_t alignment)
static constexpr size_t MaxUsableSpaceLoss()
ALIB_API ~MonoAllocator()
constexpr integer Length() const
integer CopyTo(TChar *dest) const
ALIB_API void Acquire(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
ALIB_API void Release()
defined(ALIB_DOX)
#define ALIB_WARNINGS_RESTORE
#define ALIB_FORCE_INLINE
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
ALIB_API ThreadLock GlobalAllocatorLock
void ReleaseGlobalAllocator()
MonoAllocator GlobalAllocator(8 *1024)
MonoAllocator & AcquireGlobalAllocator(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
@ Destruct
The main phase of termination that destructs everything.
void DbgCheckSingleThreaded()
monomem::MonoAllocator MonoAllocator
Type alias in namespace alib.
ALIB_FORCE_INLINE char * alloc()
ALIB_FORCE_INLINE char * alloc(size_t requestedSize, size_t alignment)
Chunk * previous
the previously allocated chunk.
char * end
Pointer to the first byte behind the chunk.
char * act
Pointer to the free space in the chunk.
static ALIB_FORCE_INLINE Chunk * create(size_t size)
size_t QtyChunkSizeExceeds
size_t QtyTrivialAllocations