momo  3.12
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 public:
78  : mMemPool(std::allocate_shared<MemPool>(alloc, pvGetMemPoolParams(), MemManager(alloc))) //?
79  {
80  }
81 
83  : mMemPool(alloc.mMemPool)
84  {
85  }
86 
88 
90  {
91  mMemPool = alloc.mMemPool;
92  return *this;
93  }
94 
95  template<class Value>
97  noexcept
98  {
101  }
102 
104  {
105  return base_allocator_type(mMemPool->GetMemManager().GetByteAllocator());
106  }
107 
109  {
111  }
112 
114  {
115  if (count == 1)
116  {
117  MemPoolParams memPoolParams = pvGetMemPoolParams();
118  bool equal = pvIsEqual(memPoolParams, mMemPool->GetParams());
119  if (!equal && mMemPool->GetAllocateCount() == 0)
120  {
121  *mMemPool = MemPool(memPoolParams, MemManager(get_base_allocator()));
122  equal = true;
123  }
124  if (equal)
125  return mMemPool->template Allocate<value_type>();
126  }
127  return MemManagerProxy::template Allocate<value_type>(mMemPool->GetMemManager(),
128  count * sizeof(value_type));
129  }
130 
131  void deallocate(pointer ptr, size_type count) noexcept
132  {
133  if (count == 1 && pvIsEqual(pvGetMemPoolParams(), mMemPool->GetParams()))
134  return mMemPool->Deallocate(ptr);
135  MemManagerProxy::Deallocate(mMemPool->GetMemManager(), ptr, count * sizeof(value_type));
136  }
137 
138  template<typename Value, typename... ValueArgs>
139  void construct(Value* ptr, ValueArgs&&... valueArgs)
140  {
142  ::template Creator<ValueArgs...> ValueCreator;
143  ValueCreator(mMemPool->GetMemManager(), std::forward<ValueArgs>(valueArgs)...)(ptr);
144  }
145 
146  template<class Value>
147  void destroy(Value* ptr) noexcept
148  {
149  momo::internal::ObjectManager<Value, MemManager>::Destroy(mMemPool->GetMemManager(), *ptr);
150  }
151 
152  friend bool operator==(const unsynchronized_pool_allocator& left,
153  const unsynchronized_pool_allocator& right) noexcept
154  {
155  return left.mMemPool == right.mMemPool;
156  }
157 
158  friend bool operator!=(const unsynchronized_pool_allocator& left,
159  const unsynchronized_pool_allocator& right) noexcept
160  {
161  return !(left == right);
162  }
163 
164 protected:
165  explicit unsynchronized_pool_allocator(const std::shared_ptr<MemPool>& memPool) noexcept
166  : mMemPool(memPool)
167  {
168  }
169 
170 private:
171  static MemPoolParams pvGetMemPoolParams() noexcept
172  {
173  return MemPoolParams(sizeof(value_type),
175  }
176 
177  static bool pvIsEqual(const MemPoolParams& memPoolParams1,
178  const MemPoolParams& memPoolParams2) noexcept
179  {
180  return memPoolParams1.GetBlockSize() == memPoolParams2.GetBlockSize()
181  && memPoolParams1.GetBlockAlignment() == memPoolParams2.GetBlockAlignment();
182  }
183 
184 private:
185  std::shared_ptr<MemPool> mMemPool;
186 };
187 
188 } // namespace stdish
189 
190 } // namespace momo
191 
192 #endif // MOMO_INCLUDE_GUARD_STDISH_POOL_ALLOCATOR
momo::internal::ObjectManager
Definition: ObjectManager.h:394
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:82
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:108
momo::stdish::unsynchronized_pool_allocator::operator=
unsynchronized_pool_allocator & operator=(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:89
momo::stdish::unsynchronized_pool_allocator::operator==
friend bool operator==(const unsynchronized_pool_allocator &left, const unsynchronized_pool_allocator &right) noexcept
Definition: pool_allocator.h:152
momo::stdish::unsynchronized_pool_allocator::size_type
size_t size_type
Definition: pool_allocator.h:62
momo::internal::ProxyConstructor
Definition: Utility.h:238
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:179
momo::internal::ObjectManager::Destroy
static void Destroy(MemManager &memManager, Object &object) noexcept
Definition: ObjectManager.h:452
momo::stdish::unsynchronized_pool_allocator::destroy
void destroy(Value *ptr) noexcept
Definition: pool_allocator.h:147
momo::stdish::unsynchronized_pool_allocator::operator!=
friend bool operator!=(const unsynchronized_pool_allocator &left, const unsynchronized_pool_allocator &right) noexcept
Definition: pool_allocator.h:158
momo::stdish::unsynchronized_pool_allocator::allocate
MOMO_NODISCARD pointer allocate(size_type count)
Definition: pool_allocator.h:113
momo::stdish::unsynchronized_pool_allocator::deallocate
void deallocate(pointer ptr, size_type count) noexcept
Definition: pool_allocator.h:131
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:27
momo::stdish::unsynchronized_pool_allocator::base_allocator_type
TBaseAllocator base_allocator_type
Definition: pool_allocator.h:56
momo::internal::MemManagerProxy
Definition: MemManager.h:332
momo::stdish::unsynchronized_pool_allocator::construct
void construct(Value *ptr, ValueArgs &&... valueArgs)
Definition: pool_allocator.h:139
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:172
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:165
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_swap
std::true_type propagate_on_container_swap
Definition: pool_allocator.h:67
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:424
std
Definition: Array.h:1178
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const base_allocator_type &alloc=base_allocator_type())
Definition: pool_allocator.h:77
momo::MemPoolParams<>
momo::stdish::unsynchronized_pool_allocator::get_base_allocator
base_allocator_type get_base_allocator() const noexcept
Definition: pool_allocator.h:103
Utility.h
MOMO_NODISCARD
#define MOMO_NODISCARD
Definition: UserSettings.h:262
momo::stdish::unsynchronized_pool_allocator::~unsynchronized_pool_allocator
~unsynchronized_pool_allocator()=default