ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
monoallocator.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_monomem of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_MONOMEM_MONOALLOCATOR
9#define HPP_ALIB_MONOMEM_MONOALLOCATOR 1
10
11#if !defined (HPP_ALIB_LANG_TMP) && !defined(ALIB_DOX)
12# include "alib/lang/tmp.hpp"
13#endif
14
15#if !defined (HPP_ALIB_MONOMEM_FWDS)
16# include "alib/monomem/fwds.hpp"
17#endif
18
19#if ALIB_THREADS
20# if !defined(HPP_ALIB_THREADS_THREADLOCK)
22# endif
23#endif
24
25#if ALIB_STRINGS
26# if !defined(HPP_ALIB_STRINGS_STRING)
28# endif
29#endif
30
31
32#if !defined(_GLIBCXX_CSTDLIB) && !defined(_CSTDLIB_)
33# include <cstdlib>
34#endif
35
36#if !defined (_GLIBCXX_MEMORY) && !defined(_MEMORY_)
37# include <memory>
38#endif
39
40#if !defined (_GLIBCXX_CSTDDEF) && !defined (_CSTDDEF_)
41# include <cstddef>
42#endif
43
44
45
46namespace alib { namespace monomem {
47
48
49/** ************************************************************************************************
50 * Implements a monotonic allocator. Allocates a series of bigger memory chunks and offers
51 * sequential allocation of portions of those.
52 * The allocations can be reset to a certain state (see #TakeSnapshot), which allows to reuse
53 * the allocated chunks for future sequential allocations.
54 *
55 * The size of the chunks allocated is defined with constructor parameters
56 * \p{initialChunkSize}. This value is reduced by #MaxUsableSpaceLoss and stored in field
57 * #nextChunksUsableSize. With each allocation of a chunk this value can be increased.
58 * Constructor parameter \p{chunkGrowthInPercent} defaults to 200, which doubles the next
59 * chunk size.
60 *
61 * If an invocation of one of the allocation methods requests memory bigger than the
62 * remaining space in the actual (last) chunk, then a new chunk is created and made the actual
63 * chunk.
64 * The remaining space of the former actual chunk will not be used for future allocations and is
65 * lost in this respect, except for the following exclamation.
66 *
67 * If a requested allocation size exceeds #nextChunksUsableSize, then a new chunk with the requested size
68 * is created, stored in the list of allocated chunks and the current chunk remains in use.
69 * With that, requested allocations are allowed to have a bigger size than the standard
70 * chunk size given in the constructor.<br>
71 *
72 * Depending on compiler symbol \ref ALIB_DEBUG_MONOMEM some metrics on instances of this class
73 * become available. Those might for example be helpful to find a reasonable value for constructor
74 * parameter \p{initialChunkSize}.
75 **************************************************************************************************/
77{
78 #if !defined(ALIB_DOX)
79 template<typename TContained>
80 friend
81 class SelfContained;
82 #endif
83
84 // #############################################################################################
85 // inner types
86 // #############################################################################################
87 protected:
88 /** ****************************************************************************************
89 * Internal type defining an allocated chunk of memory.
90 * The allocation space is found behind this object itself as it is placed at the start
91 * of each allocated chunk.
92 ******************************************************************************************/
93 struct Chunk
94 {
95 Chunk* previous; ///< the previously allocated chunk.
96 char* act; ///< Pointer to the free space in the chunk.
97 char* end; ///< Pointer to the first byte behind the chunk.
98
99 /** ************************************************************************************
100 * Allocates a chunk of memory of a size so that an instance of this class as well as
101 * the desired minimum size \p{minSize} plus alignment bytes are all fitting in.
102 *
103 * This memory is not freed with a destructor. Instead, the allocated chunk that
104 * objects of this type reside in has to be deleted by invoking method #destruct.
105 *
106 * @param size The minimum size (of the usable part) of the chunk of memory
107 * allocate, taking the maximum possible alignment space
108 * into account.
109 * @return An new allocated and internally linked chunk of memory.
110 **************************************************************************************/
112 static
113 Chunk* create( size_t size )
114 {
117 Chunk* chunk= new (malloc(size)) Chunk();
118 chunk->act = reinterpret_cast<char*>( chunk + 1 );
119 chunk->end = reinterpret_cast<char*>( chunk ) + size;
121
122 return chunk;
123 }
124
125 /** ************************************************************************************
126 * Defaulted default constructor
127 **************************************************************************************/
128 Chunk() = default;
129
130 /** ************************************************************************************
131 * Deletes the allocated chunk and with it, this object itself.
132 **************************************************************************************/
133 void destruct()
134 {
135 free( this );
136 }
137
138 /** ************************************************************************************
139 * "Frees" all allocated memory, by simply resetting the fill marker of the
140 * this chunk to the first usable byte of the allocated chunk.
141 **************************************************************************************/
142 void reset()
143 {
145 act= reinterpret_cast<char*>( this + 1 );
147 }
148
149 /** ************************************************************************************
150 * Returns a pointer to an aligned piece of memory of the requested size inside this
151 * chunk. If there is not enough space left, \c nullptr is returned.
152 *
153 * @param requestedSize The size to allocate.
154 * @param alignment The necessary alignment.
155 * @return \c nullptr on failure, otherwise pointer to the requested memory.
156 **************************************************************************************/
158 char* alloc( size_t requestedSize, size_t alignment )
159 {
160 void* result= act;
161 size_t freeSpace= static_cast<size_t>( end-act );
163 if( !std::align( alignment, requestedSize, result, freeSpace) )
164 return nullptr;
165 act= reinterpret_cast<char*>( result ) + requestedSize;
166 return reinterpret_cast<char*>( result );
168 }
169
170 /** ************************************************************************************
171 * Templated version of #alloc(size_t, size_t). Used with sizes and alignment
172 * known at compile-time.
173 *
174 * @tparam requestedSize The size to allocate.
175 * @tparam alignment The necessary alignment.
176 * @return \c nullptr on failure, otherwise pointer to the requested memory.
177 **************************************************************************************/
178 template<size_t requestedSize, size_t alignment>
180 char* alloc()
181 {
182 void* result= act;
183 size_t freeSpace= static_cast<size_t>( end-act );
184 if( !std::align( alignment, requestedSize, result, freeSpace) )
185 return nullptr;
186 act= reinterpret_cast<char*>( result ) + requestedSize;
187
188 return reinterpret_cast<char*>( result );
189 }
190
191 }; // struct Chunk
192
193 /**
194 * The maximum amount of bytes that are lost by the fact that a chunk's data is
195 * stored at the start of the chunk.
196 * This is the sum of the chunk's size plus the waste caused by aligning an object of
197 * maximum possible alignment, if allocated as a first object behind a chunk's management
198 * data.
199 *
200 * @return The maximum space loss in allocated chunks.
201 */
202 static constexpr size_t MaxUsableSpaceLoss()
203 {
204 return sizeof(Chunk)
205 + sizeof(Chunk) % alignof(std::max_align_t);
206 }
207
208
209 // #############################################################################################
210 // protected fields
211 // #############################################################################################
212 protected:
213 /** The actual chunk. Contains a link to previously allocated chunks.*/
215
216 /** The list of chunks that are to be recycled. */
218
219 /**
220 * The initial allocation size given in the constructor minus the maximum overhead
221 * caused by storing a chunk's management data inside the chunks themselves.
222 * In other words, this field stores the value given in the constructor, minus the value
223 * returned by \alib{monomem::MonoAllocator;MaxUsableSpaceLoss}.
224 *
225 * Allocated chunks may be bigger in the case that a single allocation is larger than this
226 * value.
227 */
229
230 /**
231 * Growth factor of subsequently allocated chunks.
232 * Given by a construction parameter, which defaults to 200, which doubles chunk size
233 * with each next chunk allocation.
234 */
236
237 public:
238 #if ALIB_DEBUG_MONOMEM
239 /**
240 * Debug statistics.<br>
241 * Availability depends on code selector symbol \ref ALIB_DEBUG_MONOMEM.
242 */
244 {
245 /** The number of allocations performed. */
246 size_t QtyAllocations = 0;
247
248 /** The number of allocations that did not create a new chunk . */
250
251 /** The number of created chunks. */
252 size_t QtyChunks = 0;
253
254 /** The number of bytes allocated at the heap. */
255 size_t HeapSize = 0;
256
257 /** The number of allocated space. */
258 size_t AllocSize = 0;
259
260 /** The number of bytes lost for alignment. */
261 size_t AlignmentWaste = 0;
262
263 /** The number of bytes remaining in chunks. */
264 size_t ChunkWaste = 0;
265
266 /** The number of allocations that have been larger than the chunk size. */
268
269 /** The number of resets performed. */
270 size_t QtyResets = 0;
271 };
272
273 /** Debug statistics measured on the whole run-time of this object.<br>
274 * Availability depends on code selector symbol \ref ALIB_DEBUG_MONOMEM. */
276
277 /**
278 * Sub-domain for debug log output of this class. Could be changed to allow
279 * fine grained log-output selection. The following domains are set by \alib
280 * types:
281 *
282 * Type | Sub-Domain
283 * ------------------------------ |------------------------------------------------------
284 * - \alib{monomem;GlobalAllocator}| <c>"MA/GLBL"</c>
285 * - \alib{config;InMemoryPlugin} | <c>"MA/CFG/IMPLGN"</c>
286 * - \alib{config;Variable} | <c>"MA/CFG/VAR"</c>
287 * - \alib{lox;Lox} | <c>"MA/ALOX/LOX"</c>
288 * - \alib{expressions;Compiler} | <c>"MA/EXPR/CMPLR"</c>
289 * - \alib{expressions;Scope} | <c>"MA/EXPR/SCP"</c>, respectively <c>"MA/EXPR/CTSCP"</c>
290 * - \alib{expressions::plugins;Calculus} | <c>"MA/EXPR/CLCLS"</c>
291 *
292 * Availability depends on code selector symbol \ref ALIB_DEBUG_MONOMEM.
293 */
295
296
297 #endif
298
299 // #############################################################################################
300 // Snapshot
301 // #############################################################################################
302 public:
303 /**
304 * Stores the actual state of outer class \b %MonoAllocator.
305 * Retrieved method \alib{monomem;MonoAllocator::TakeSnapshot} and
306 * \alib{monomem;MonoAllocator::Reset(const Snapshot&)}.
307 */
309 {
310 private:
311 #if !defined(ALIB_DOX)
312 friend class MonoAllocator;
313 #endif
314
315 /** The current chunk. */
317
318 /** Pointer to the first free byte in the current chunk */
319 char* actFill;
320
321 public:
322 /**
323 * Internal constructor which creates a snapshot from the current allocator.
324 * @param allocator The current chunk.
325 */
327 : chunk ( allocator->chunk )
328 , actFill( chunk ? allocator->chunk->act : nullptr)
329 {}
330
331 /**
332 * Default constructor.
333 * \note
334 * Default-constructed snapshots passed to method #Reset(const Snapshot&) reset
335 * the whole of the monotonic allocator.
336 */
338 : chunk( nullptr)
339 {}
340
341 /**
342 * Returns \c false if this snapshot was never initialized properly (default
343 * constructed and not copied over.
344 * @return \c true if this is not a valid snapshot, \c false otherwise.
345 */
346 bool IsValid()
347 {
348 return chunk != nullptr;
349 }
350 };
351
352 // #############################################################################################
353 // Constructor / Destructor
354 // #############################################################################################
355 public:
356 /** ****************************************************************************************
357 * Constructor.
358 * #MaxUsableSpaceLoss is subtracted from given \p{initialChunkSize} and stored in field
359 * #nextChunksUsableSize.
360 * @param chunkGrowthInPercent Optional growth factor in percent (*100), applied to each
361 * allocation of a next chunk size.
362 * Values provided should be greater than 100.<p>
363 * Defaults to 200, which doubles chunk size with each
364 * next internal chunk allocation.
365 * @param initialChunkSize The standard size of memory chunks that are dynamically allocated.
366 ******************************************************************************************/
367 ALIB_API MonoAllocator( size_t initialChunkSize, unsigned int chunkGrowthInPercent= 200 );
368
369
370 /** ****************************************************************************************
371 * Destructor. Frees all allocated memory chunks.
372 ******************************************************************************************/
374
375 /** ****************************************************************************************
376 * This static method creates an object of this type inside "itself", aka inside its first
377 * allocated chunk.
378 * Objects created with this method, have to be deleted by only invoking the
379 * destructor, which also deletes the object the returned pointer refers to.
380 *
381 * The parameterless version of method #Reset must not be called with objects created
382 * by this method. Instead, if reset operations are desired, a snapshot has to be
383 * taken (see method #TakeSnapshot) right after the invocation of this method (aka before
384 * allocations are performed) which then has to be passed to the overloaded method
385 * \b Reset(const Snapshot&).
386 *
387 * @param initialChunkSize The size of memory chunks allocated.
388 * @param chunkGrowthInPercent Optional growth factor in percent (*100), applied to each
389 * allocation of a next chunk size.
390 * Values provided should be greater than 100.<p>
391 * Defaults to 200, which doubles chunk size with each
392 * next internal chunk allocation.
393 * @return A \b MonoAllocator object residing in its first created chunk.
394 ******************************************************************************************/
395 ALIB_API static MonoAllocator* Create( size_t initialChunkSize,
396 unsigned int chunkGrowthInPercent= 200 );
397
398
399 #if ALIB_THREADS && ALIB_CAMP && !defined(ALIB_DOX)
400 ALIB_API void dbgCheckGlobalAllocatorLock();
401 #endif
402
403 // #############################################################################################
404 // Interface
405 // #############################################################################################
406 public:
407 /** ****************************************************************************************
408 * Allocates memory of requested size and alignment.
409 * This method may be used if templated method #Alloc is not applicable.
410 *
411 * @param size The allocation size requested.
412 * @param alignment The required alignment.
413 * @return A pointer to the allocated memory.
414 ******************************************************************************************/
416 char* Alloc( size_t size, size_t alignment)
417 {
418 #if ALIB_THREADS && ALIB_CAMP
419 dbgCheckGlobalAllocatorLock();
420 #endif
421
422 #if ALIB_DEBUG_MONOMEM
424 DbgStats.AllocSize += size;
425 size_t qtyLeftBeforeAlloc= chunk ? static_cast<size_t>(chunk->end - chunk->act)
426 : 0;
427 #endif
428
429 char* mem;
430
431 if( chunk )
432 {
433 mem = chunk->alloc(size, alignment);
434 if ( mem )
435 {
436 #if ALIB_DEBUG_MONOMEM
437 DbgStats.AlignmentWaste += qtyLeftBeforeAlloc
438 - static_cast<size_t>(chunk->end - chunk->act)
439 - size;
441 #endif
442 return mem;
443 }
444 }
445
446 return getCreateChunk( size, alignment );
447 }
448
449 /** ****************************************************************************************
450 * Allocates aligned memory suitable to emplace an instance of \p{T}.
451 *
452 * \see For a version that also constructs the type, see #Emplace.
453 *
454 * @tparam T The type of object to allocated memory for.
455 * @return A pointer to the allocated memory.
456 ******************************************************************************************/
457 template<typename T>
459 T* Alloc()
460 {
461 return reinterpret_cast<T*>( Alloc( sizeof(T), alignof(T) ) );
462 }
463
464 /** ****************************************************************************************
465 * Allocates aligned memory for an array of objects of type \p{T} of size \p{length}.
466 *
467 * \see
468 * Method #EmplaceArray for an alternative that initializes the memory.
469 *
470 * @tparam T The array element type.
471 * @tparam TSize The type of the array length value. This template parameter is provided
472 * for convenience, to avoid casts from singed types. It is deduced by the
473 * compiler and not needed to be provided.
474 * @param length The capacity of the requested array.
475 * @return A pointer to the first element of the allocated array.
476 ******************************************************************************************/
477 template<typename T, typename TSize>
479 T* AllocArray( TSize length )
480 {
481 return reinterpret_cast<T*>( Alloc( sizeof(T[1]) * static_cast<size_t>(length),
482 alignof(T[]) ) );
483 }
484
485 /** ****************************************************************************************
486 * Allocates aligned memory of size and alignment suitable for type \p{T} and performs a
487 * C++ "placement new", passing the given arguments to the type's constructor.
488 *
489 * @tparam T Type to emplace in this monotonic allocator.
490 * @tparam TArgs Types of variadic parameters given with parameter \p{args}.
491 * @param args Variadic parameters to be forwarded to constructor of type \p{T}.
492 * @return A pointer to the allocated memory.
493 ******************************************************************************************/
494 template<typename T, typename... TArgs>
496 T* Emplace( TArgs&& ... args )
497 {
498 return new (Alloc<T>()) T( std::forward<TArgs>( args )... );
499 }
500
501 /** ****************************************************************************************
502 * Allocates aligned memory for an array of objects of type \p{T} of size* \p{length}.
503 * Array members are initialized using a "placement new" passing the given \p{args} to
504 * the type's constructor.
505 *
506 * \see
507 * Method #AllocArray for an alternative that does not initialize the memory.
508 *
509 * @tparam T The array element type.
510 * @tparam TArgs Types of variadic parameters given with parameter \p{args}.
511 * @tparam TSize The type of the array length value. This template parameter is provided
512 * for convenience, to avoid casts from singed types. It is deduced by the
513 * compiler and not needed to be provided.
514 * @param length The capacity of the requested array.
515 * @param args Variadic parameters to be forwarded to constructor of each array element
516 * of type \p{T}.
517 * @return A pointer to the first element of the allocated array.
518 ******************************************************************************************/
519 template<typename T, typename TSize, typename... TArgs>
520 T* EmplaceArray( TSize length, TArgs&& ... args )
521 {
523 T* mem= AllocArray<T, TSize>( length );
524
525 for( TSize i= 0 ; i < length ; ++i )
526 new (mem + i) T(std::forward<TArgs>( args )...);
528
529 return mem;
530 }
531
532 /** ****************************************************************************************
533 * Saves the current state of the allocator and returns this information as a \b Snapshot
534 * value. Such snapshots may be passed to method #Reset(const Snapshot&).
535 *
536 * Note that the actual memory is \b not copied and restored. In this respect the word
537 * "Snapshot" is overstating. What is stored are the current use of memory, but not it's
538 * contents.
539 *
540 * @return A (lightweight) snapshot value object.
541 ******************************************************************************************/
543 {
544 return Snapshot(this);
545 }
546
547 /** ****************************************************************************************
548 * Resets this allocator to the given \alib{monomem::MonoAllocator;Snapshot}.
549 * Parameter \p{snapshot} is defaulted with a default-constructed \b Snapshot, which
550 * completely resets the allocator.
551 *
552 * With a reset, the memory chunks which had been allocated after taking the given
553 * \p{snapshot}, are not freed, but re-used with future monotonic allocations.
554 *
555 * This method is useful in cases where some permanent objects which are allocated first
556 * have to be preserved with resets.
557 * It can also be used to preserve a self-contained monotonic allocator created with
558 * static method #Create.
559 *
560 * Note that snapshots taken after the given one, become invalid. This is because
561 * class \b Snapshot is only a simple lightweight class that marks the currently
562 * used chunk and its fill level.
563 *
564 * @param snapshot The snapshot to reset to.
565 ******************************************************************************************/
566 ALIB_API void Reset( const Snapshot& snapshot= Snapshot() );
567
568 #if ALIB_STRINGS
569 /** ************************************************************************************
570 * Returns a copy of the given string.
571 *
572 * \note
573 * In case \p{src} is empty, the a copy of the original string is
574 * returned (aka a string that points to the same buffer that was given).
575 * This allows to have a sort of "valid" pointer, even though the buffer of \p{src}
576 * might be a temporary.
577 *
578 * @tparam TChar The character type of the input and output string.
579 * @param src The source string to copy
580 * @return A string object representing the copy.
581 **************************************************************************************/
582 template<typename TChar>
584 {
585 if( !src.Length() )
586 return src;
587
588 TChar* mem= AllocArray<TChar>( src.Length() );
589 src.CopyTo( mem );
590 return strings::TString<TChar>(mem, src.Length() );
591 }
592
593 #if ALIB_DEBUG_MONOMEM
594 /** ********************************************************************************
595 * Provides allocation statistics for manual performance optimization.
596 * This method is available only if code selector symbol \ref ALIB_DEBUG_MONOMEM is
597 * set.
598 *
599 * @return Some textual information on the allocation statistics.
600 **********************************************************************************/
602 #endif
603
604 #endif
605
606 // #############################################################################################
607 // Internals
608 // #############################################################################################
609 protected:
610 /** ****************************************************************************************
611 * Protected constructor taking a first chunk of memory that was allocated externally.
612 * This constructor is used by static method #Create which uses <em>placement new</em> to
613 * create this allocate inside the chunk given.
614 *
615 * @param firstChunk The first chunk already created outside.
616 * @param initialChunkSize The size of the first chunk allocated.
617 * @param chunkGrowthInPercent Growth factor in percent (*100), applied to each
618 * allocation of a next chunk size.
619 ******************************************************************************************/
620 ALIB_API MonoAllocator( Chunk* firstChunk,
621 size_t initialChunkSize, unsigned int chunkGrowthInPercent );
622
623 /** ****************************************************************************************
624 * This internal allocation method is called by the allocation interface methods, in case
625 * the current request can not be trivially satisfied.
626 *
627 * Implements the overall strategy of this class in respect to oversized blocks,
628 * recycling of blocks, etc.
629 *
630 * @param size The size of the memory to allocate.
631 * @param alignment The allocation alignment needed.
632 * @return A pointer to the allocated memory.
633 ******************************************************************************************/
634 ALIB_API char* getCreateChunk(size_t size, size_t alignment);
635}; // class MonoAllocator
636
637
638// #################################################################################################
639// Namespace Functions
640// #################################################################################################
641/** ************************************************************************************************
642 * This static inline namespace function calls the destructor <b>~T()</b>of given \p{object}.
643 * The use of this method is recommended instead of calling the destructor directly,
644 * to increase readability of the code.
645 *
646 * @tparam T The object type. Deduced by the compiler and not needed to be given.
647 * @param object The object to destruct.
648 **************************************************************************************************/
649template<typename T>
651void Destruct(T* object)
652{
653 object->~T();
654}
655
656
657
658#if defined(ALIB_DOX)
659/** ************************************************************************************************
660 * Simple inline function that acquires \alib{monomem;GlobalAllocatorLock} and returns the object
661 * singleton \alib{monomem;GlobalAllocator}.
662 *
663 * \see
664 * - Chapter \ref alib_monomem_globalinstance "7. The Global MonoAllocator" of the Programmer's
665 * Manual of this \alibmod_nl.
666 * - Macro \ref ALIB_CALLER_PRUNED that is recommended to be used to pass the parameters to
667 * this method.
668 *
669 * \see Namespace function #ReleaseGlobalAllocator.
670 *
671 * @param dbgFile Caller information. Available only with debug builds and if
672 * \alib_threads is included in the \alibdist.
673 * @param dbgLine Caller information. Available only with debug builds and if
674 * \alib_threads is included in the \alibdist.
675 * @param dbgFunc Caller information. Available only with debug builds and if
676 * \alib_threads is included in the \alibdist.
677 * @return A reference to the global monotonic allocator instance
678 * \alib{monomem;AcquireGlobalAllocator}.
679 **************************************************************************************************/
680inline
682 int dbgLine,
683 const NCString& dbgFunc );
684
685/** ************************************************************************************************
686 * Simple inline function that releases \alib{monomem;GlobalAllocatorLock}, previously acquired
687 * with \alib{monomem;AcquireGlobalAllocator}.
688 **************************************************************************************************/
689inline
691
692/**
693 * This is the global monotonic allocator singleton instance.
694 *
695 * \see
696 * - Chapter \ref alib_monomem_globalinstance "7. The Global MonoAllocator" of the Programmer's
697 * Manual of this \alibmod_nl.
698 * - \b ThreadLock instance \alib{monomem::GlobalAllocatorLock}
699 * - Functions \alib{monomem;AcquireGlobalAllocator} and \alib{monomem;ReleaseGlobalAllocator}.
700 */
701extern ALIB_API
703
704/**
705 * This <em>mutex</em> is used by methods \alib{monomem;AcquireGlobalAllocator} and
706 * \alib{monomem;ReleaseGlobalAllocator} to control access over the global allocator instance
707 * \alib{monomem;GlobalAllocator}.
708 *
709 * This object may be controlled directly as an alternative to using the functions named above, for
710 * example in combination with macro \ref ALIB_LOCK_WITH, with attaches the locking/unlocking
711 * mechanisms "automatically" to the current call stack.
712 */
713extern ALIB_API
715
716#else // !defined(ALIB_DOX)
717
718extern ALIB_API
720
721#if ALIB_THREADS
722 extern ALIB_API
724
725 inline
726 MonoAllocator& AcquireGlobalAllocator( ALIB_DBG(const NCString& file, int line, const NCString& func) )
727 {
728 GlobalAllocatorLock.Acquire( ALIB_DBG(file,line,func) );
729 return GlobalAllocator;
730 }
731
732 inline void ReleaseGlobalAllocator()
733 {
735 }
736
737#else
738 inline
739 MonoAllocator& AcquireGlobalAllocator(ALIB_DBG(const char* , int , const char* ) )
740 {
742 return GlobalAllocator;
743 }
744 inline
746 {}
747#endif // ALIB_THREADS
748
749#endif // !defined(ALIB_DOX)
750
751} // namespace alib[::monomem]
752
753/// Type alias in namespace \b alib.
755
756} // namespace [alib]
757
758#endif // HPP_ALIB_MONOMEM_MONOALLOCATOR
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)
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
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()
constexpr integer Length() const
Definition string.hpp:357
integer CopyTo(TChar *dest) const
Definition string.hpp:1974
ALIB_API void Acquire(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
ALIB_API void Release()
defined(ALIB_DOX)
#define A_CHAR(STR)
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_FORCE_INLINE
Definition alib.hpp:549
#define ALIB_API
Definition alib.hpp:538
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
#define ALIB_DBG(...)
Definition alib.hpp:457
ALIB_API ThreadLock GlobalAllocatorLock
void ReleaseGlobalAllocator()
MonoAllocator GlobalAllocator(8 *1024)
MonoAllocator & AcquireGlobalAllocator(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
Definition alib.cpp:57
@ Destruct
The main phase of termination that destructs everything.
void DbgCheckSingleThreaded()
Definition alib.cpp:226
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)