ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localallocator.inl
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-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace monomem {
9
10//==================================================================================================
11/// A mono allocator, that incorporates a member of templated size \p{TCapacityInKB}, which is used
12/// as the initial buffer for allocation. The class is intended to be instantiated by local
13/// variables, hence using stack memory.<br>
14/// When the initial buffer is exhausted, further buffers will be heap allocated.
15///
16/// Note that the \ref alib_manual_appendix_typealiases "usual type alias" given in namespace #alib,
17/// in this case misses the leading <em>"T"</em>, and is given with \alib{LocalAllocator}.
18/// Besides that, further aliases that denote specific stack sizes are provided with
19/// \alib{LocalAllocator1K},
20/// \alib{LocalAllocator2K},
21/// \alib{LocalAllocator4K},
22/// \alib{LocalAllocator8K},
23/// \alib{LocalAllocator16K},
24/// \alib{LocalAllocator32K}, and
25/// \alib{LocalAllocator64K}.
26///
27/// @see Chapter \ref alib_contmono_class_monoalloc_local of the Programmer's Manual of this
28/// \alibcamp_nl.
29/// @tparam TCapacityInKB The size of the internal buffer in kilobytes.
30/// @tparam TAllocator The allocator type that parent \alib{monomem;TMonoAllocator} should
31/// be \ref alib_contmono_chaining "chained" to.<br>
32/// Defaults to \alib{lang;HeapAllocator}.
33//==================================================================================================
34template<size_t TCapacityInKB, typename TAllocator= lang::HeapAllocator>
35class TLocalAllocator : public TMonoAllocator<TAllocator>
36{
37 protected:
38 /// Shortcut to our base type.
40
41 /// Internal memory passed as a first buffer to base class \b MonoAllocator
42 void* localMemory[TCapacityInKB*1024/sizeof(void*)];
43
44 public:
45 /// Default constructor.
46 /// @param pBufferGrowthInPercent Optional growth factor in percent, applied to each allocation
47 /// of a next buffer size in respect to its previous size.
48 /// Defaults to \c 200, which doubles buffer size with each
49 /// next internal buffer allocation.
50 TLocalAllocator(unsigned int pBufferGrowthInPercent= 200)
51 : base( ALIB_DBG("LocalAllocator",)
52 new (localMemory) detail::Buffer( TCapacityInKB*1024),
53 TCapacityInKB,
54 pBufferGrowthInPercent ) {}
55
56 /// Constructor that accepts a different underlying allocator
57 /// @param pAllocator The allocator to use for allocation of additional buffers.
58 /// @param pBufferGrowthInPercent Optional growth factor in percent, applied to each allocation
59 /// of a next buffer size in respect to its previous size.
60 /// Defaults to \c 200, which doubles buffer size with each
61 /// next internal buffer allocation.
62 TLocalAllocator(TAllocator& pAllocator, unsigned int pBufferGrowthInPercent= 200)
63 : base( ALIB_DBG("LocalAllocator",)
64 pAllocator,
65 new (localMemory) detail::Buffer( TCapacityInKB*1024),
66 TCapacityInKB,
67 pBufferGrowthInPercent) {}
68
69 /// Destructor. Calls \alib{monomem;MonoAllocator::destructWithExternalBuffer}.
74
75
76 /// Convenience method that returns <c>*this</c> statically cast to base type
77 /// \b %MonoAllocator. This is primarily needed in situations where overload resolution
78 /// of methods fails, if not exactly this base type is given. A prominent sample for this is
79 /// the constructor of type \alib{AStringMA}.
80 /// @return A reference to \p{*this} as \b its base type.
81 MonoAllocator& AsMonoAllocator() { return static_cast<MonoAllocator&>(*this); }
82}; // class TLocalAllocator
83
84} // namespace alib[::monomem]
85
86/// Type alias in namespace \b alib.
87template<size_t TCapacityInKB>
89
90using LocalAllocator1K = monomem::TLocalAllocator< 1>; ///< Type alias in namespace \b alib. Allocates 1kB of stack memory.
91using LocalAllocator2K = monomem::TLocalAllocator< 2>; ///< Type alias in namespace \b alib. Allocates 2kB of stack memory.
92using LocalAllocator4K = monomem::TLocalAllocator< 4>; ///< Type alias in namespace \b alib. Allocates 4kB of stack memory.
93using LocalAllocator8K = monomem::TLocalAllocator< 8>; ///< Type alias in namespace \b alib. Allocates 8kB of stack memory.
94using LocalAllocator16K = monomem::TLocalAllocator<16>; ///< Type alias in namespace \b alib. Allocates 16kB of stack memory.
95using LocalAllocator32K = monomem::TLocalAllocator<32>; ///< Type alias in namespace \b alib. Allocates 32kB of stack memory.
96using LocalAllocator64K = monomem::TLocalAllocator<64>; ///< Type alias in namespace \b alib. Allocates 64kB of stack memory.
97
98} // namespace [alib]
99
~TLocalAllocator()
Destructor. Calls MonoAllocator::destructWithExternalBuffer.
TMonoAllocator< TAllocator > base
Shortcut to our base type.
TLocalAllocator(TAllocator &pAllocator, unsigned int pBufferGrowthInPercent=200)
TLocalAllocator(unsigned int pBufferGrowthInPercent=200)
void * localMemory[TCapacityInKB *1024/sizeof(void *)]
TMonoAllocator(const char *dbgName, std::nullptr_t) noexcept
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_DBG(...)
Definition alib.inl:836
Details of namespace alib::monomem.
monomem::TLocalAllocator< 8 > LocalAllocator8K
Type alias in namespace alib. Allocates 8kB of stack memory.
monomem::TLocalAllocator< 32 > LocalAllocator32K
Type alias in namespace alib. Allocates 32kB of stack memory.
monomem::TLocalAllocator< 1 > LocalAllocator1K
Type alias in namespace alib. Allocates 1kB of stack memory.
monomem::TLocalAllocator< TCapacityInKB > LocalAllocator
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
monomem::TLocalAllocator< 64 > LocalAllocator64K
Type alias in namespace alib. Allocates 64kB of stack memory.
monomem::TLocalAllocator< 4 > LocalAllocator4K
Type alias in namespace alib. Allocates 4kB of stack memory.
monomem::TLocalAllocator< 2 > LocalAllocator2K
Type alias in namespace alib. Allocates 2kB of stack memory.
monomem::TLocalAllocator< 16 > LocalAllocator16K
Type alias in namespace alib. Allocates 16kB of stack memory.