Kiwano Engine  v1.2.x
Allocator.h
1 // Copyright (c) 2016-2018 Kiwano - Nomango
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #pragma once
22 #include <utility> // std::forward
23 #include <limits> // std::numeric_limits
24 #include <memory> // std::addressof
25 #include <kiwano/macros.h>
26 
27 namespace kiwano
28 {
29 namespace memory
30 {
31 
34 class KGE_API MemoryAllocator
35 {
36 public:
39  virtual void* Alloc(size_t size) = 0;
40 
43  virtual void Free(void* ptr) = 0;
44 };
45 
48 MemoryAllocator* GetAllocator();
49 
52 void SetAllocator(MemoryAllocator* allocator);
53 
56 inline void* Alloc(size_t size)
57 {
58  return memory::GetAllocator()->Alloc(size);
59 }
60 
63 inline void Free(void* ptr)
64 {
65  memory::GetAllocator()->Free(ptr);
66 }
67 
68 } // namespace memory
69 
72 template <typename _Ty>
73 class Allocator
74 {
75 public:
76  typedef _Ty value_type;
77  typedef _Ty* pointer;
78  typedef const _Ty* const_pointer;
79  typedef _Ty& reference;
80  typedef const _Ty& const_reference;
81 
82  using size_type = size_t;
83  using difference_type = ptrdiff_t;
84 
85  template <class _Other>
86  struct rebind
87  {
88  using other = Allocator<_Other>;
89  };
90 
91  Allocator() noexcept {}
92 
93  Allocator(const Allocator&) noexcept = default;
94 
95  template <class _Other>
96  Allocator(const Allocator<_Other>&) noexcept
97  {
98  }
99 
100  inline _Ty* allocate(size_t count)
101  {
102  if (count > 0)
103  {
104  return static_cast<_Ty*>(memory::Alloc(sizeof(_Ty) * count));
105  }
106  return nullptr;
107  }
108 
109  inline void* allocate(size_t count, const void*)
110  {
111  return allocate(count);
112  }
113 
114  inline void deallocate(void* ptr, size_t count)
115  {
116  memory::Free(ptr /*, sizeof(_Ty) * count */);
117  }
118 
119  template <typename _UTy, typename... _Args>
120  inline void construct(_UTy* const ptr, _Args&&... args)
121  {
122  ::new (const_cast<void*>(static_cast<const volatile void*>(ptr))) _Ty(std::forward<_Args>(args)...);
123  }
124 
125  template <typename _UTy>
126  inline void destroy(_UTy* ptr)
127  {
128  ptr->~_UTy();
129  }
130 
131  size_t max_size() const noexcept
132  {
133  return std::numeric_limits<size_t>::max() / sizeof(_Ty);
134  }
135 
136  _Ty* address(_Ty& val) const noexcept
137  {
138  return std::addressof(val);
139  }
140 
141  const _Ty* address(const _Ty& val) const noexcept
142  {
143  return std::addressof(val);
144  }
145 };
146 
147 // Allocator<void>
148 template <>
149 class Allocator<void>
150 {
151 public:
152  using value_type = void;
153  typedef void* pointer;
154  typedef const void* const_pointer;
155 
156  using size_type = size_t;
157  using difference_type = ptrdiff_t;
158 
159  template <class _Other>
160  struct rebind
161  {
162  using other = Allocator<_Other>;
163  };
164 };
165 
166 template <class _Ty, class _Other>
167 bool operator==(const Allocator<_Ty>&, const Allocator<_Other>&) noexcept
168 {
169  return true;
170 }
171 
172 template <class _Ty, class _Other>
173 bool operator!=(const Allocator<_Ty>&, const Allocator<_Other>&) noexcept
174 {
175  return false;
176 }
177 
178 } // namespace kiwano
内存分配器
Definition: Allocator.h:34
Definition: Actor.cpp:26
分配器
Definition: Allocator.h:73
Definition: Allocator.h:86