mn_lock_storage.hpp
Go to the documentation of this file.
1 
21 #ifndef __MN_BASIC_LOCK_STORAGE_H__
22 #define __MN_BASIC_LOCK_STORAGE_H__
23 
24 #include "../mn_config.hpp"
25 
27 
28 #include "../mn_autolock.hpp"
29 #include "../mn_null_lock.hpp"
30 
31 #include "mn_basic_allocator.hpp"
32 
33 namespace mn {
34  namespace memory {
35 
39  template <class TMutex, class TAllocator, class TFilter = basic_allocator_filter>
41  public:
42  using allocator_category = typename allocator::allocator_category ;
43 
44  using value_type = void;
45  using pointer = void*;
46  using const_pointer = const void*;
48  using size_type = size_t;
49 
51 
52  using lock_type = TMutex;
53  using allocator_impl = TAllocator;
54  using filter_type = TFilter;
57 
58 
60  : m_lockObjct(), m_fFilter(), m_xTicksToWait(portMAX_DELAY) { allocator::first(); }
61 
62  basic_threadsafed_allocator(const lock_type& lckObject) noexcept
63  : m_lockObjct(lckObject), m_fFilter(), m_xTicksToWait(portMAX_DELAY) { allocator::first(); }
64 
65  basic_threadsafed_allocator(const lock_type& lckObject, const filter_type& _fFilter ) noexcept
66  : m_lockObjct(lckObject), m_fFilter(_fFilter), m_xTicksToWait(portMAX_DELAY) { allocator::first(); }
67 
75  pointer allocate(size_t size, size_t alignment) {
77 
78  pointer _mem = nullptr;
79 
80  if(m_fFilter.on_pre_alloc(size)) {
81  _mem = allocator_impl::allocate(size, alignment);
82  m_fFilter.on_alloc(size);
83  }
84  return _mem;
85  }
92  pointer allocate(size_t size) {
93  return allocate(size, mn::alignment_for(size));
94  }
95 
104  pointer allocate(size_t count, size_t size, size_t alignment = 0) {
105  return allocate(count * size, (alignment == 0) ? mn::alignment_for(size) : alignment);
106  }
107 
114  void deallocate(pointer address, size_t size, size_t alignment) noexcept {
116 
117  if(m_fFilter.on_pre_dealloc(size)) {
118  allocator_impl::deallocate(address, size, alignment);
119  m_fFilter.on_dealloc(size);
120  }
121  }
122 
128  void deallocate(pointer address, size_t size) noexcept {
129  deallocate(address, size, mn::alignment_for(size));
130  }
131 
139  void deallocate(pointer address, size_t count, size_t size, size_t alignment) noexcept {
141 
142  size = size * count;
143  if(m_fFilter.on_pre_dealloc(size)) {
144  allocator_impl::deallocate(address, size, (alignment == 0) ? mn::alignment_for(size) : alignment);
145  m_fFilter.on_dealloc(size);
146  }
147  }
148 
154  template <class Type, typename... Args>
155  Type* construct(Args&&... args) {
156  auto _size = sizeof(Type);
157 
158  void* _mem = allocate(_size, mn::alignment_for(_size) );
159 
160  return ::new (_mem) Type(mn::forward<Args>(args)...);
161  }
162 
168  template <class Type>
169  void destroy(Type* address) noexcept {
170  if(address == nullptr) return;
171 
172  auto _size = sizeof(Type);
173 
174  mn::destruct<Type>(address);
175  deallocate(address, _size, mn::alignment_for(_size));
176  }
177 
182  size_t get_max_alocator_size() const noexcept {
184 
185  return allocator_impl::get_max_alocator_size();
186  }
187 
188  void lock(unsigned long xTicksToWait = 0) const {
189 
190  m_lockObjct.lock(xTicksToWait == 0 ? m_xTicksToWait : xTicksToWait);
191  }
192 
193  void unlock() const noexcept {
194  m_lockObjct.unlock();
195  }
196 
197  void set_ticksToWait(unsigned long xTicksToWait) {
199  m_xTicksToWait = xTicksToWait;
200  m_lockObjct.unlock();
201  }
202  bool is_locked() {
203  return m_lockObjct.is_locked();
204  }
205 
206  basic_threadsafed_allocator(const self_type& other) noexcept = delete;
207  self_type& operator = (const basic_threadsafed_allocator& other) noexcept = delete;
208  private:
211  unsigned long m_xTicksToWait;
212  };
213 
214  }
215 }
216 
217 
218 #endif // __MN_BASIC_LOCK_STORAGE_H__
Definition: mn_lock.hpp:122
Definition: mn_lock_storage.hpp:40
void destroy(Type *address) noexcept
Deconstruct a object (call deconstructor) and free the memory.
Definition: mn_lock_storage.hpp:169
TFilter filter_type
Definition: mn_lock_storage.hpp:54
size_t size_type
Definition: mn_lock_storage.hpp:48
basic_threadsafed_allocator(const self_type &other) noexcept=delete
void value_type
Definition: mn_lock_storage.hpp:44
pointer allocate(size_t size, size_t alignment)
malloc() a buffer in a given TAllocator and cheak with the given TFilter is this okay to alloc
Definition: mn_lock_storage.hpp:75
pointer allocate(size_t size)
malloc() a buffer in a given TAllocator and cheak with the given TFilter is this okay to alloc
Definition: mn_lock_storage.hpp:92
void set_ticksToWait(unsigned long xTicksToWait)
Definition: mn_lock_storage.hpp:197
mn::ptrdiff_t difference_type
Definition: mn_lock_storage.hpp:47
void lock(unsigned long xTicksToWait=0) const
Definition: mn_lock_storage.hpp:188
basic_threadsafed_allocator() noexcept
Definition: mn_lock_storage.hpp:59
void deallocate(pointer address, size_t size, size_t alignment) noexcept
free() a buffer in a given heap.
Definition: mn_lock_storage.hpp:114
TMutex lock_type
Definition: mn_lock_storage.hpp:52
lock_type m_lockObjct
Definition: mn_lock_storage.hpp:209
void deallocate(pointer address, size_t count, size_t size, size_t alignment) noexcept
free() a buffer in a given heap.
Definition: mn_lock_storage.hpp:139
bool is_locked()
Definition: mn_lock_storage.hpp:202
unsigned long m_xTicksToWait
Definition: mn_lock_storage.hpp:211
TAllocator allocator_impl
Definition: mn_lock_storage.hpp:53
pointer allocate(size_t count, size_t size, size_t alignment=0)
malloc() a buffer in a given TAllocator and cheak with the given TFilter is this okay to alloc
Definition: mn_lock_storage.hpp:104
filter_type m_fFilter
Definition: mn_lock_storage.hpp:210
void deallocate(pointer address, size_t size) noexcept
free() a buffer in a given heap.
Definition: mn_lock_storage.hpp:128
void unlock() const noexcept
Definition: mn_lock_storage.hpp:193
size_t get_max_alocator_size() const noexcept
Get the maximal size to allocate.
Definition: mn_lock_storage.hpp:182
Type * construct(Args &&... args)
Construct a object from allocated impl.
Definition: mn_lock_storage.hpp:155
void * pointer
Definition: mn_lock_storage.hpp:45
self_type & operator=(const basic_threadsafed_allocator &other) noexcept=delete
basic_threadsafed_allocator(const lock_type &lckObject) noexcept
Definition: mn_lock_storage.hpp:62
const void * const_pointer
Definition: mn_lock_storage.hpp:46
typename allocator::allocator_category allocator_category
Definition: mn_lock_storage.hpp:42
basic_threadsafed_allocator(const lock_type &lckObject, const filter_type &_fFilter) noexcept
Definition: mn_lock_storage.hpp:65
#define portMAX_DELAY
Definition: mn_autolock.hpp:34
void * allocate(const TAlloC &alloc, size_t size, size_t alignment, mn::memory::std_allocator_tag)
Definition: mn_allocator_typetraits.hpp:52
void * deallocate(const TAlloC &alloc, void *address, size_t size, size_t alignment, mn::memory::std_allocator_tag)
Definition: mn_allocator_typetraits.hpp:63
Definition: mn_allocator_typetraits.hpp:25
integral_constant< bool, true > true_type
Definition: mn_typetraits.hpp:129
size_t alignment_for(const size_t size) noexcept
Definition: mn_alignment.hpp:178
long ptrdiff_t
Definition: mn_def.hpp:49
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48