ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localallocator.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/// Copyright 2013-2026 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 protected:
37 /// Shortcut to our base type.
39
40 /// Internal memory passed as a first buffer to base class #"%MonoAllocator"
41 void* localMemory[TCapacityInKB*1024/sizeof(void*)];
42
43 public:
44 /// Default constructor.
45 /// @param pBufferGrowthInPercent Optional growth factor in percent, applied to each allocation
46 /// of a next buffer size in respect to its previous size.
47 /// Defaults to \c 200, which doubles buffer size with each
48 /// next internal buffer allocation.
49 TLocalAllocator(unsigned pBufferGrowthInPercent= 200)
50 : base( ALIB_DBG("LocalAllocator",)
51 new (localMemory) detail::Buffer( TCapacityInKB*1024),
52 TCapacityInKB,
53 pBufferGrowthInPercent ) {}
54
55 /// Constructor that accepts a different underlying allocator
56 /// @param pAllocator The allocator to use for allocation of additional buffers.
57 /// @param pBufferGrowthInPercent Optional growth factor in percent, applied to each allocation
58 /// of a next buffer size in respect to its previous size.
59 /// Defaults to \c 200, which doubles buffer size with each
60 /// next internal buffer allocation.
61 TLocalAllocator(TAllocator& pAllocator, unsigned pBufferGrowthInPercent= 200)
62 : base( ALIB_DBG("LocalAllocator",)
63 pAllocator,
64 new (localMemory) detail::Buffer( TCapacityInKB*1024),
65 TCapacityInKB,
66 pBufferGrowthInPercent) {}
67
68 /// Destructor. Calls #"destructWithExternalBuffer".
70
71 /// Convenience method that returns <c>*this</c> statically cast to base type
72 /// #"%MonoAllocator". This is primarily needed in situations where overload resolution
73 /// of methods fails, if not exactly this base type is given.
74 /// A prominent sample for this is the constructor of type #"AStringMA".
75 /// @return A reference to \p{*this}, cast to the base type.
76 MonoAllocator& AsMonoAllocator() { return static_cast<MonoAllocator&>(*this); }
77
78}; // class TLocalAllocator
79
80} // namespace alib[::monomem]
81
82/// Type alias in namespace #"%alib".
83template<size_t TCapacityInKB>
85
86using LocalAllocator1K = monomem::TLocalAllocator< 1>; ///< Type alias in namespace #"%alib". Allocates 1kB of stack memory.
87using LocalAllocator2K = monomem::TLocalAllocator< 2>; ///< Type alias in namespace #"%alib". Allocates 2kB of stack memory.
88using LocalAllocator4K = monomem::TLocalAllocator< 4>; ///< Type alias in namespace #"%alib". Allocates 4kB of stack memory.
89using LocalAllocator8K = monomem::TLocalAllocator< 8>; ///< Type alias in namespace #"%alib". Allocates 8kB of stack memory.
90using LocalAllocator16K = monomem::TLocalAllocator<16>; ///< Type alias in namespace #"%alib". Allocates 16kB of stack memory.
91using LocalAllocator32K = monomem::TLocalAllocator<32>; ///< Type alias in namespace #"%alib". Allocates 32kB of stack memory.
92using LocalAllocator64K = monomem::TLocalAllocator<64>; ///< Type alias in namespace #"%alib". Allocates 64kB of stack memory.
93
94} // namespace [alib]
#define ALIB_EXPORT
#define ALIB_DBG(...)
~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".
Definition alox.cpp:14
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.