momo  3.11
pool_allocator.h
Go to the documentation of this file.
1 /**********************************************************\
2 
3  This file is part of the
4  https://github.com/morzhovets/momo
5  project, distributed under the MIT License. See
6  https://github.com/morzhovets/momo/blob/branch_cpp11/LICENSE
7  for details.
8 
9  momo/stdish/pool_allocator.h
10 
11  namespace momo::stdish:
12  class unsynchronized_pool_allocator
13 
14 \**********************************************************/
15 
16 #ifndef MOMO_INCLUDE_GUARD_STDISH_POOL_ALLOCATOR
17 #define MOMO_INCLUDE_GUARD_STDISH_POOL_ALLOCATOR
18 
19 #ifdef __has_include
20 # if __has_include(<momo/Utility.h>)
21 # include <momo/Utility.h>
22 # endif
23 #endif
24 #ifndef MOMO_PARENT_HEADER
25 # include "../Utility.h"
26 #endif
27 
28 #include MOMO_PARENT_HEADER(MemPool)
29 
30 namespace momo
31 {
32 
33 namespace stdish
34 {
35 
48 template<typename TValue,
49  typename TBaseAllocator = std::allocator<unsigned char>,
50  typename TMemPoolParams = MemPoolParams<>>
52 {
53 public:
54  typedef TValue value_type;
55 
56  typedef TBaseAllocator base_allocator_type;
57  typedef TMemPoolParams mem_pool_params;
58 
59  typedef value_type* pointer;
60  typedef const value_type* const_pointer;
61 
62  typedef size_t size_type;
63  typedef ptrdiff_t difference_type;
64 
65  typedef std::false_type propagate_on_container_copy_assignment;
67  typedef std::true_type propagate_on_container_swap;
68 
69 private:
72 
73  typedef mem_pool_params MemPoolParams;
75 
76  template<typename Value>
77  struct PoolAllocatorProxy
78  : public unsynchronized_pool_allocator<Value, base_allocator_type, mem_pool_params>
79  {
81  MOMO_DECLARE_PROXY_CONSTRUCTOR(PoolAllocator)
82  };
83 
84 public:
86  : mMemPool(std::allocate_shared<MemPool>(alloc, pvGetMemPoolParams(), MemManager(alloc))) //?
87  {
88  }
89 
91  : mMemPool(alloc.mMemPool)
92  {
93  }
94 
96 
98  {
99  mMemPool = alloc.mMemPool;
100  return *this;
101  }
102 
103  template<class Value>
105  noexcept
106  {
107  return PoolAllocatorProxy<Value>(mMemPool);
108  }
109 
111  {
112  return base_allocator_type(mMemPool->GetMemManager().GetByteAllocator());
113  }
114 
116  {
118  }
119 
121  {
122  if (count == 1)
123  {
124  MemPoolParams memPoolParams = pvGetMemPoolParams();
125  bool equal = pvIsEqual(memPoolParams, mMemPool->GetParams());
126  if (!equal && mMemPool->GetAllocateCount() == 0)
127  {
128  *mMemPool = MemPool(memPoolParams, MemManager(get_base_allocator()));
129  equal = true;
130  }
131  if (equal)
132  return mMemPool->template Allocate<value_type>();
133  }
134  return MemManagerProxy::template Allocate<value_type>(mMemPool->GetMemManager(),
135  count * sizeof(value_type));
136  }
137 
138  void deallocate(pointer ptr, size_type count) noexcept
139  {
140  if (count == 1 && pvIsEqual(pvGetMemPoolParams(), mMemPool->GetParams()))
141  return mMemPool->Deallocate(ptr);
142  MemManagerProxy::Deallocate(mMemPool->GetMemManager(), ptr, count * sizeof(value_type));
143  }
144 
145  template<typename Value, typename... ValueArgs>
146  void construct(Value* ptr, ValueArgs&&... valueArgs)
147  {
149  ::template Creator<ValueArgs...> ValueCreator;
150  ValueCreator(mMemPool->GetMemManager(), std::forward<ValueArgs>(valueArgs)...)(ptr);
151  }
152 
153  template<class Value>
154  void destroy(Value* ptr) noexcept
155  {
156  momo::internal::ObjectManager<Value, MemManager>::Destroy(mMemPool->GetMemManager(), *ptr);
157  }
158 
159  friend bool operator==(const unsynchronized_pool_allocator& left,
160  const unsynchronized_pool_allocator& right) noexcept
161  {
162  return left.mMemPool == right.mMemPool;
163  }
164 
165  friend bool operator!=(const unsynchronized_pool_allocator& left,
166  const unsynchronized_pool_allocator& right) noexcept
167  {
168  return !(left == right);
169  }
170 
171 protected:
172  explicit unsynchronized_pool_allocator(const std::shared_ptr<MemPool>& memPool) noexcept
173  : mMemPool(memPool)
174  {
175  }
176 
177 private:
178  static MemPoolParams pvGetMemPoolParams() noexcept
179  {
180  return MemPoolParams(sizeof(value_type),
182  }
183 
184  static bool pvIsEqual(const MemPoolParams& memPoolParams1,
185  const MemPoolParams& memPoolParams2) noexcept
186  {
187  return memPoolParams1.GetBlockSize() == memPoolParams2.GetBlockSize()
188  && memPoolParams1.GetBlockAlignment() == memPoolParams2.GetBlockAlignment();
189  }
190 
191 private:
192  std::shared_ptr<MemPool> mMemPool;
193 };
194 
195 } // namespace stdish
196 
197 } // namespace momo
198 
199 #endif // MOMO_INCLUDE_GUARD_STDISH_POOL_ALLOCATOR
momo::internal::ObjectManager
Definition: ObjectManager.h:240
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:90
momo::MemPool
Definition: MemPool.h:135
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_move_assignment
std::true_type propagate_on_container_move_assignment
Definition: pool_allocator.h:66
momo::stdish::unsynchronized_pool_allocator::select_on_container_copy_construction
unsynchronized_pool_allocator select_on_container_copy_construction() const noexcept
Definition: pool_allocator.h:115
momo::stdish::unsynchronized_pool_allocator::operator=
unsynchronized_pool_allocator & operator=(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:97
momo::stdish::unsynchronized_pool_allocator::operator==
friend bool operator==(const unsynchronized_pool_allocator &left, const unsynchronized_pool_allocator &right) noexcept
Definition: pool_allocator.h:159
momo::stdish::unsynchronized_pool_allocator::size_type
size_t size_type
Definition: pool_allocator.h:62
momo::stdish::unsynchronized_pool_allocator::const_pointer
const value_type * const_pointer
Definition: pool_allocator.h:60
momo::stdish::unsynchronized_pool_allocator::value_type
TValue value_type
Definition: pool_allocator.h:54
momo::MemManagerStd
MemManagerStd uses allocator<unsigned char>::allocate and deallocate
Definition: MemManager.h:177
momo::internal::ObjectManager::Destroy
static void Destroy(MemManager &memManager, Object &object) noexcept
Definition: ObjectManager.h:351
momo::stdish::unsynchronized_pool_allocator::destroy
void destroy(Value *ptr) noexcept
Definition: pool_allocator.h:154
momo::stdish::unsynchronized_pool_allocator::operator!=
friend bool operator!=(const unsynchronized_pool_allocator &left, const unsynchronized_pool_allocator &right) noexcept
Definition: pool_allocator.h:165
momo::stdish::unsynchronized_pool_allocator::allocate
MOMO_NODISCARD pointer allocate(size_type count)
Definition: pool_allocator.h:120
momo::stdish::unsynchronized_pool_allocator::deallocate
void deallocate(pointer ptr, size_type count) noexcept
Definition: pool_allocator.h:138
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_copy_assignment
std::false_type propagate_on_container_copy_assignment
Definition: pool_allocator.h:65
momo
Definition: Array.h:26
momo::stdish::unsynchronized_pool_allocator::base_allocator_type
TBaseAllocator base_allocator_type
Definition: pool_allocator.h:56
momo::internal::MemManagerProxy
Definition: MemManager.h:329
momo::stdish::unsynchronized_pool_allocator::construct
void construct(Value *ptr, ValueArgs &&... valueArgs)
Definition: pool_allocator.h:146
momo::stdish::unsynchronized_pool_allocator::mem_pool_params
TMemPoolParams mem_pool_params
Definition: pool_allocator.h:57
momo::stdish::unsynchronized_pool_allocator
Allocator with a pool of memory for containers like std::list, std::forward_list, std::map,...
Definition: pool_allocator.h:52
momo::internal::ObjectAlignmenter
Definition: ObjectManager.h:171
momo::stdish::unsynchronized_pool_allocator::pointer
value_type * pointer
Definition: pool_allocator.h:59
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const std::shared_ptr< MemPool > &memPool) noexcept
Definition: pool_allocator.h:172
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_swap
std::true_type propagate_on_container_swap
Definition: pool_allocator.h:67
MOMO_DECLARE_PROXY_CONSTRUCTOR
#define MOMO_DECLARE_PROXY_CONSTRUCTOR(Object)
Definition: Utility.h:118
momo::stdish::unsynchronized_pool_allocator::difference_type
ptrdiff_t difference_type
Definition: pool_allocator.h:63
momo::internal::MemManagerProxy::Deallocate
static void Deallocate(MemManager &memManager, Object *ptr, size_t size) noexcept
Definition: MemManager.h:425
std
Definition: Array.h:1173
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const base_allocator_type &alloc=base_allocator_type())
Definition: pool_allocator.h:85
momo::MemPoolParams<>
momo::stdish::unsynchronized_pool_allocator::get_base_allocator
base_allocator_type get_base_allocator() const noexcept
Definition: pool_allocator.h:110
Utility.h
MOMO_NODISCARD
#define MOMO_NODISCARD
Definition: UserSettings.h:217
momo::stdish::unsynchronized_pool_allocator::~unsynchronized_pool_allocator
~unsynchronized_pool_allocator()=default