mn_basic_allocator.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINILIB_BASIC_ALLOCATOR_H__
19 #define __MINILIB_BASIC_ALLOCATOR_H__
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_algorithm.hpp"
24 #include "../mn_functional.hpp"
25 #include "../mn_typetraits.hpp"
26 #include "../utils/mn_alignment.hpp"
27 
29 
30 namespace mn {
31  namespace memory {
32 
37  public:
38  bool on_pre_alloc(size_t size, size_t alignment) { return true; }
39  void on_alloc(size_t size, size_t alignment) { }
40 
41  bool on_pre_dealloc(size_t size, size_t alignment) { return true; }
42  void on_dealloc(size_t size, size_t alignment) { }
43  };
44 
48  template <class TAllocator, class TFilter = basic_allocator_filter>
50  public:
51  using allocator_category = typename TAllocator::allocator_category ;
52  using is_thread_safe = typename TAllocator::is_thread_safe ;
53  using filter_type = TFilter;
54 
55  using value_type = void;
56  using pointer = void*;
57  using const_pointer = const void*;
59  using size_type = size_t;
60 
61  basic_allocator() noexcept { TAllocator::first(); }
62 
70  pointer allocate(size_t size, size_t alignment) {
71  pointer _mem = nullptr;
72 
73  if(m_fFilter.on_pre_alloc(size)) {
74  _mem = TAllocator::allocate(size, alignment);
75  m_fFilter.on_alloc(size);
76  }
77  return _mem;
78  }
79 
86  pointer allocate(size_t size) {
87  return allocate(size, mn::alignment_for(size));
88  }
89 
98  pointer allocate(size_t count, size_t size, size_t alignment = 0) {
99  return allocate(count * size, (alignment == 0) ? mn::alignment_for(size) : alignment);
100  }
101 
102 
109  void deallocate(pointer address, size_t size, size_t alignment) noexcept {
110  if(m_fFilter.on_pre_dealloc(size)) {
111  TAllocator::deallocate(address, size, alignment);
112  m_fFilter.on_dealloc(size);
113  }
114  }
115 
121  void deallocate(pointer address, size_t size) noexcept {
122  deallocate(address, size, mn::alignment_for(size));
123  }
124 
132  void deallocate(pointer address, size_t count, size_t size, size_t alignment) noexcept {
133  size = size * count;
134  if(m_fFilter.on_pre_dealloc(size)) {
135  TAllocator::deallocate(address, size, (alignment == 0) ? mn::alignment_for(size) : alignment);
136  m_fFilter.on_dealloc(size);
137  }
138  }
139 
145  template <class Type, typename... Args>
146  Type* construct(Args&&... args) {
147  auto _size = sizeof(Type);
148 
149  void* _mem = allocate(_size, mn::alignment_for(_size) );
150 
151  return ::new (_mem) Type(mn::forward<Args>(args)...);
152  }
153 
159  template <class Type>
160  void destroy(Type* address) noexcept {
161  if(address == nullptr) return;
162 
163  auto _size = sizeof(Type);
164 
165  mn::destruct<Type>(address);
166  deallocate(address, _size, mn::alignment_for(_size));
167  }
168 
173  size_t get_max_alocator_size() const noexcept {
174  return TAllocator::get_max_alocator_size();
175  }
176 
177  private:
179  };
180 
181 
182  }
183 }
184 
185 #endif // __MINILIB_BASIC_ALLOCATOR_H__
Definition: mn_basic_allocator.hpp:36
void on_dealloc(size_t size, size_t alignment)
Definition: mn_basic_allocator.hpp:42
bool on_pre_dealloc(size_t size, size_t alignment)
Definition: mn_basic_allocator.hpp:41
bool on_pre_alloc(size_t size, size_t alignment)
Definition: mn_basic_allocator.hpp:38
void on_alloc(size_t size, size_t alignment)
Definition: mn_basic_allocator.hpp:39
Definition: mn_basic_allocator.hpp:49
const void * const_pointer
Definition: mn_basic_allocator.hpp:57
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_basic_allocator.hpp:86
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_basic_allocator.hpp:98
size_t get_max_alocator_size() const noexcept
Get the maximal size to allocate.
Definition: mn_basic_allocator.hpp:173
typename TAllocator::allocator_category allocator_category
Definition: mn_basic_allocator.hpp:51
mn::ptrdiff_t difference_type
Definition: mn_basic_allocator.hpp:58
void value_type
Definition: mn_basic_allocator.hpp:55
basic_allocator() noexcept
Definition: mn_basic_allocator.hpp:61
void deallocate(pointer address, size_t size) noexcept
free() a buffer in a given heap.
Definition: mn_basic_allocator.hpp:121
void destroy(Type *address) noexcept
Deconstruct a object (call deconstructor) and free the memory.
Definition: mn_basic_allocator.hpp:160
void deallocate(pointer address, size_t count, size_t size, size_t alignment) noexcept
free() a buffer in a given heap.
Definition: mn_basic_allocator.hpp:132
filter_type m_fFilter
Definition: mn_basic_allocator.hpp:178
void deallocate(pointer address, size_t size, size_t alignment) noexcept
free() a buffer in a given heap.
Definition: mn_basic_allocator.hpp:109
void * pointer
Definition: mn_basic_allocator.hpp:56
Type * construct(Args &&... args)
Construct a object from allocated impl.
Definition: mn_basic_allocator.hpp:146
TFilter filter_type
Definition: mn_basic_allocator.hpp:53
typename TAllocator::is_thread_safe is_thread_safe
Definition: mn_basic_allocator.hpp:52
size_t size_type
Definition: mn_basic_allocator.hpp:59
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_basic_allocator.hpp:70
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
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