ALib C++ Framework
by
Library Version: 2511 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 #"mainpage_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 #"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 #"LocalAllocator".
18/// Besides that, further aliases that denote specific stack sizes are provided with
19/// #"LocalAllocator1K",
20/// #"LocalAllocator2K",
21/// #"LocalAllocator4K",
22/// #"LocalAllocator8K",
23/// #"LocalAllocator16K",
24/// #"LocalAllocator32K", and
25/// #"LocalAllocator64K".
26///
27/// @see Chapter #"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 #"TMonoAllocator" should
31/// be #"alib_contmono_chaining;chained" to.<br>
32/// Defaults to #"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 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 pBufferGrowthInPercent= 200)
63 : base( ALIB_DBG("LocalAllocator",)
64 pAllocator,
65 new (localMemory) detail::Buffer( TCapacityInKB*1024),
66 TCapacityInKB,
67 pBufferGrowthInPercent) {}
68
69 /// Destructor. Calls #"destructWithExternalBuffer".
71
72 /// Convenience method that returns <c>*this</c> statically cast to base type
73 /// \b %MonoAllocator. This is primarily needed in situations where overload resolution
74 /// of methods fails, if not exactly this base type is given. A prominent sample for this is
75 /// the constructor of type #"AStringMA".
76 /// @return A reference to \p{*this} as \b its base type.
77 MonoAllocator& AsMonoAllocator() { return static_cast<MonoAllocator&>(*this); }
78
79}; // class TLocalAllocator
80
81} // namespace alib[::monomem]
82
83/// Type alias in namespace \b alib.
84template<size_t TCapacityInKB>
86
87using LocalAllocator1K = monomem::TLocalAllocator< 1>; ///< Type alias in namespace \b alib. Allocates 1kB of stack memory.
88using LocalAllocator2K = monomem::TLocalAllocator< 2>; ///< Type alias in namespace \b alib. Allocates 2kB of stack memory.
89using LocalAllocator4K = monomem::TLocalAllocator< 4>; ///< Type alias in namespace \b alib. Allocates 4kB of stack memory.
90using LocalAllocator8K = monomem::TLocalAllocator< 8>; ///< Type alias in namespace \b alib. Allocates 8kB of stack memory.
91using LocalAllocator16K = monomem::TLocalAllocator<16>; ///< Type alias in namespace \b alib. Allocates 16kB of stack memory.
92using LocalAllocator32K = monomem::TLocalAllocator<32>; ///< Type alias in namespace \b alib. Allocates 32kB of stack memory.
93using LocalAllocator64K = monomem::TLocalAllocator<64>; ///< Type alias in namespace \b alib. Allocates 64kB of stack memory.
94
95} // namespace [alib]
#define ALIB_EXPORT
Definition alib.inl:562
#define ALIB_DBG(...)
Definition alib.inl:931
~TLocalAllocator()
Destructor. Calls #"destructWithExternalBuffer".
TMonoAllocator< TAllocator > base
Shortcut to our base type.
TLocalAllocator(TAllocator &pAllocator, unsigned pBufferGrowthInPercent=200)
TLocalAllocator(unsigned pBufferGrowthInPercent=200)
void * localMemory[TCapacityInKB *1024/sizeof(void *)]
TMonoAllocator(const char *dbgName, std::nullptr_t) noexcept
Details of namespace #"alib::monomem;2".
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
monomem::TLocalAllocator< 1 > LocalAllocator1K
Type alias in namespace alib. Allocates 1kB of stack memory.
monomem::TLocalAllocator< TCapacityInKB > LocalAllocator
Type alias in namespace alib.
monomem::TLocalAllocator< 32 > LocalAllocator32K
Type alias in namespace alib. Allocates 32kB of stack memory.
monomem::TLocalAllocator< 8 > LocalAllocator8K
Type alias in namespace alib. Allocates 8kB of stack memory.
monomem::TLocalAllocator< 2 > LocalAllocator2K
Type alias in namespace alib. Allocates 2kB of stack memory.
monomem::TLocalAllocator< 64 > LocalAllocator64K
Type alias in namespace alib. Allocates 64kB of stack memory.
monomem::TLocalAllocator< 16 > LocalAllocator16K
Type alias in namespace alib. Allocates 16kB of stack memory.
monomem::TLocalAllocator< 4 > LocalAllocator4K
Type alias in namespace alib. Allocates 4kB of stack memory.