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