Samchon Framework for CPP  1.0.0
CriticalAllocator.hpp
1 #pragma once
2 
3 #include <memory>
4 #include <samchon/library/RWMutex.hpp>
5 
6 namespace samchon
7 {
8 namespace library
9 {
49  template <class T>
51  : public std::allocator<T>
52  {
53  private:
54  typedef std::allocator<T> super;
55 
60 
61  public:
65  using super::super;
66 
70  auto getMutex() -> RWMutex&
71  {
72  return rw_mutex;
73  };
74  auto getMutex() const -> const RWMutex&
75  {
76  return rw_mutex;
77  };
78 
79  /* ---------------------------------------------------------------
80  SINGLE ELEMENT'S CONSTRUCTION AND DESTRUCTION
81  --------------------------------------------------------------- */
103  template<class _U, class... _Args>
104  void construct(_U *ptr, _Args&&... args)
105  {
106  UniqueWriteLock uk(rw_mutex);
107 
108  super::construct(ptr, args);
109  };
110 
128  template<class _U>
129  void destroy(_U *ptr)
130  {
131  UniqueWriteLock uk(rw_mutex);
132 
133  super::destroy(ptr);
134  };
135 
136  /* ---------------------------------------------------------------
137  MULTIPLE ELEMENT'S ALLOCATION AND DEALLOCATION
138  --------------------------------------------------------------- */
168  auto allocate(size_type n, std::allocator<void>::const_pointer hint = NULL) -> pointer
169  {
170  UniqueWriteLock uk(rw_mutex);
171 
172  super::allocate(n, hint);
173  };
174 
196  void deallocate(pointer ptr, size_type size)
197  {
198  UniqueWriteLock uk(rw_mutex);
199 
200  super::deallocate(ptr, size);
201  };
202  };
203 };
204 };
An allocator ensuring concurrency.
void deallocate(pointer ptr, size_type size)
Lock when deallocated [Inherited] Release block of storage.
auto getMutex() -> RWMutex &
Get rw_mutex.
void destroy(_U *ptr)
Lock when destroyed [Inherited] Destory an object.
Unique lock for writing.
RWMutex rw_mutex
A rw_mutex for handling concurrency.
void construct(_U *ptr, _Args &&...args)
Lock when constructed. [Inherited] Construct an object. .
auto allocate(size_type n, std::allocator< void >::const_pointer hint=NULL) -> pointer
Lock when allocated [Inherited] Allocate block of storage.