momo  3.9
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 #include "../MemPool.h"
20 
21 namespace momo
22 {
23 
24 namespace stdish
25 {
26 
39 template<typename TValue,
40  typename TBaseAllocator = std::allocator<unsigned char>,
41  typename TMemPoolParams = MemPoolParams<>>
43 {
44 public:
45  typedef TValue value_type;
46 
47  typedef TBaseAllocator base_allocator_type;
48  typedef TMemPoolParams mem_pool_params;
49 
50  typedef value_type* pointer;
51  typedef const value_type* const_pointer;
52 
53  typedef size_t size_type;
54  typedef ptrdiff_t difference_type;
55 
56  typedef std::false_type propagate_on_container_copy_assignment;
58  typedef std::true_type propagate_on_container_swap;
59 
60 private:
63 
64  typedef mem_pool_params MemPoolParams;
66 
67  template<typename Value>
68  struct PoolAllocatorProxy
69  : public unsynchronized_pool_allocator<Value, base_allocator_type, mem_pool_params>
70  {
72  MOMO_DECLARE_PROXY_CONSTRUCTOR(PoolAllocator)
73  };
74 
75 public:
77  : mMemPool(std::allocate_shared<MemPool>(alloc, pvGetMemPoolParams(), MemManager(alloc))) //?
78  {
79  }
80 
82  : mMemPool(alloc.mMemPool)
83  {
84  }
85 
87 
89  {
90  mMemPool = alloc.mMemPool;
91  return *this;
92  }
93 
94  template<class Value>
96  noexcept
97  {
98  return PoolAllocatorProxy<Value>(mMemPool);
99  }
100 
102  {
103  return base_allocator_type(mMemPool->GetMemManager().GetByteAllocator());
104  }
105 
107  {
109  }
110 
112  {
113  if (count == 1)
114  {
115  MemPoolParams memPoolParams = pvGetMemPoolParams();
116  bool equal = pvIsEqual(memPoolParams, mMemPool->GetParams());
117  if (!equal && mMemPool->GetAllocateCount() == 0)
118  {
119  *mMemPool = MemPool(memPoolParams, MemManager(get_base_allocator()));
120  equal = true;
121  }
122  if (equal)
123  return mMemPool->template Allocate<value_type>();
124  }
125  return MemManagerProxy::template Allocate<value_type>(mMemPool->GetMemManager(),
126  count * sizeof(value_type));
127  }
128 
129  void deallocate(pointer ptr, size_type count) noexcept
130  {
131  if (count == 1 && pvIsEqual(pvGetMemPoolParams(), mMemPool->GetParams()))
132  return mMemPool->Deallocate(ptr);
133  MemManagerProxy::Deallocate(mMemPool->GetMemManager(), ptr, count * sizeof(value_type));
134  }
135 
136  template<typename Value, typename... ValueArgs>
137  void construct(Value* ptr, ValueArgs&&... valueArgs)
138  {
140  ::template Creator<ValueArgs...> ValueCreator;
141  ValueCreator(mMemPool->GetMemManager(), std::forward<ValueArgs>(valueArgs)...)(ptr);
142  }
143 
144  template<class Value>
145  void destroy(Value* ptr) noexcept
146  {
147  momo::internal::ObjectManager<Value, MemManager>::Destroy(mMemPool->GetMemManager(), *ptr);
148  }
149 
150  bool operator==(const unsynchronized_pool_allocator& alloc) const noexcept
151  {
152  return mMemPool == alloc.mMemPool;
153  }
154 
155  bool operator!=(const unsynchronized_pool_allocator& alloc) const noexcept
156  {
157  return !(*this == alloc);
158  }
159 
160 protected:
161  explicit unsynchronized_pool_allocator(const std::shared_ptr<MemPool>& memPool) noexcept
162  : mMemPool(memPool)
163  {
164  }
165 
166 private:
167  static MemPoolParams pvGetMemPoolParams() noexcept
168  {
169  return MemPoolParams(sizeof(value_type),
171  }
172 
173  static bool pvIsEqual(const MemPoolParams& memPoolParams1,
174  const MemPoolParams& memPoolParams2) noexcept
175  {
176  return memPoolParams1.GetBlockSize() == memPoolParams2.GetBlockSize()
177  && memPoolParams1.GetBlockAlignment() == memPoolParams2.GetBlockAlignment();
178  }
179 
180 private:
181  std::shared_ptr<MemPool> mMemPool;
182 };
183 
184 } // namespace stdish
185 
186 } // namespace momo
187 
188 #endif // MOMO_INCLUDE_GUARD_STDISH_POOL_ALLOCATOR
momo::internal::ObjectManager
Definition: ObjectManager.h:196
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:81
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:57
momo::stdish::unsynchronized_pool_allocator::select_on_container_copy_construction
unsynchronized_pool_allocator select_on_container_copy_construction() const noexcept
Definition: pool_allocator.h:106
momo::stdish::unsynchronized_pool_allocator::operator=
unsynchronized_pool_allocator & operator=(const unsynchronized_pool_allocator &alloc) noexcept
Definition: pool_allocator.h:88
momo::stdish::unsynchronized_pool_allocator::size_type
size_t size_type
Definition: pool_allocator.h:53
momo::stdish::unsynchronized_pool_allocator::operator!=
bool operator!=(const unsynchronized_pool_allocator &alloc) const noexcept
Definition: pool_allocator.h:155
momo::stdish::unsynchronized_pool_allocator::const_pointer
const value_type * const_pointer
Definition: pool_allocator.h:51
momo::stdish::unsynchronized_pool_allocator::value_type
TValue value_type
Definition: pool_allocator.h:45
momo::MemManagerStd
MemManagerStd uses allocator<unsigned char>::allocate and deallocate
Definition: MemManager.h:176
momo::internal::ObjectManager::Destroy
static void Destroy(MemManager &memManager, Object &object) noexcept
Definition: ObjectManager.h:307
momo::stdish::unsynchronized_pool_allocator::destroy
void destroy(Value *ptr) noexcept
Definition: pool_allocator.h:145
momo::stdish::unsynchronized_pool_allocator::allocate
MOMO_NODISCARD pointer allocate(size_type count)
Definition: pool_allocator.h:111
momo::stdish::unsynchronized_pool_allocator::deallocate
void deallocate(pointer ptr, size_type count) noexcept
Definition: pool_allocator.h:129
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_copy_assignment
std::false_type propagate_on_container_copy_assignment
Definition: pool_allocator.h:56
momo
Definition: Array.h:26
momo::stdish::unsynchronized_pool_allocator::base_allocator_type
TBaseAllocator base_allocator_type
Definition: pool_allocator.h:47
momo::internal::MemManagerProxy
Definition: MemManager.h:328
momo::stdish::unsynchronized_pool_allocator::construct
void construct(Value *ptr, ValueArgs &&... valueArgs)
Definition: pool_allocator.h:137
momo::stdish::unsynchronized_pool_allocator::mem_pool_params
TMemPoolParams mem_pool_params
Definition: pool_allocator.h:48
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:43
momo::internal::ObjectAlignmenter
Definition: ObjectManager.h:143
momo::stdish::unsynchronized_pool_allocator::pointer
value_type * pointer
Definition: pool_allocator.h:50
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const std::shared_ptr< MemPool > &memPool) noexcept
Definition: pool_allocator.h:161
momo::stdish::unsynchronized_pool_allocator::propagate_on_container_swap
std::true_type propagate_on_container_swap
Definition: pool_allocator.h:58
MOMO_DECLARE_PROXY_CONSTRUCTOR
#define MOMO_DECLARE_PROXY_CONSTRUCTOR(Object)
Definition: Utility.h:85
momo::stdish::unsynchronized_pool_allocator::difference_type
ptrdiff_t difference_type
Definition: pool_allocator.h:54
std
Definition: ArrayUtility.h:299
momo::stdish::unsynchronized_pool_allocator::unsynchronized_pool_allocator
unsynchronized_pool_allocator(const base_allocator_type &alloc=base_allocator_type())
Definition: pool_allocator.h:76
momo::stdish::unsynchronized_pool_allocator::operator==
bool operator==(const unsynchronized_pool_allocator &alloc) const noexcept
Definition: pool_allocator.h:150
momo::MemPoolParams<>
momo::internal::MemManagerProxy::Deallocate
static void Deallocate(MemManager &memManager, void *ptr, size_t size) noexcept
Definition: MemManager.h:423
momo::stdish::unsynchronized_pool_allocator::get_base_allocator
base_allocator_type get_base_allocator() const noexcept
Definition: pool_allocator.h:101
MOMO_NODISCARD
#define MOMO_NODISCARD
Definition: UserSettings.h:192
momo::stdish::unsynchronized_pool_allocator::~unsynchronized_pool_allocator
~unsynchronized_pool_allocator()=default