rev |
line source |
nuclear@0
|
1 /************************************************************************************
|
nuclear@0
|
2
|
nuclear@0
|
3 PublicHeader: OVR_Kernel.h
|
nuclear@0
|
4 Filename : OVR_ContainerAllocator.h
|
nuclear@0
|
5 Content : Template allocators and constructors for containers.
|
nuclear@0
|
6 Created : September 19, 2012
|
nuclear@0
|
7 Notes :
|
nuclear@0
|
8
|
nuclear@0
|
9 Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
|
nuclear@0
|
10
|
nuclear@0
|
11 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
|
nuclear@0
|
12 you may not use the Oculus VR Rift SDK except in compliance with the License,
|
nuclear@0
|
13 which is provided at the time of installation or download, or which
|
nuclear@0
|
14 otherwise accompanies this software in either electronic or hard copy form.
|
nuclear@0
|
15
|
nuclear@0
|
16 You may obtain a copy of the License at
|
nuclear@0
|
17
|
nuclear@0
|
18 http://www.oculusvr.com/licenses/LICENSE-3.2
|
nuclear@0
|
19
|
nuclear@0
|
20 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
|
nuclear@0
|
21 distributed under the License is distributed on an "AS IS" BASIS,
|
nuclear@0
|
22 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
nuclear@0
|
23 See the License for the specific language governing permissions and
|
nuclear@0
|
24 limitations under the License.
|
nuclear@0
|
25
|
nuclear@0
|
26 ************************************************************************************/
|
nuclear@0
|
27
|
nuclear@0
|
28 #ifndef OVR_ContainerAllocator_h
|
nuclear@0
|
29 #define OVR_ContainerAllocator_h
|
nuclear@0
|
30
|
nuclear@0
|
31 #include "OVR_Allocator.h"
|
nuclear@0
|
32 #include <string.h>
|
nuclear@0
|
33
|
nuclear@0
|
34
|
nuclear@0
|
35 namespace OVR {
|
nuclear@0
|
36
|
nuclear@0
|
37
|
nuclear@0
|
38 //-----------------------------------------------------------------------------------
|
nuclear@0
|
39 // ***** Container Allocator
|
nuclear@0
|
40
|
nuclear@0
|
41 // ContainerAllocator serves as a template argument for allocations done by
|
nuclear@0
|
42 // containers, such as Array and Hash; replacing it could allow allocator
|
nuclear@0
|
43 // substitution in containers.
|
nuclear@0
|
44
|
nuclear@0
|
45 class ContainerAllocatorBase
|
nuclear@0
|
46 {
|
nuclear@0
|
47 public:
|
nuclear@0
|
48 static void* Alloc(size_t size) { return OVR_ALLOC(size); }
|
nuclear@0
|
49 static void* Realloc(void* p, size_t newSize) { return OVR_REALLOC(p, newSize); }
|
nuclear@0
|
50 static void Free(void *p) { OVR_FREE(p); }
|
nuclear@0
|
51 };
|
nuclear@0
|
52
|
nuclear@0
|
53
|
nuclear@0
|
54
|
nuclear@0
|
55 //-----------------------------------------------------------------------------------
|
nuclear@0
|
56 // ***** Constructors, Destructors, Copiers
|
nuclear@0
|
57
|
nuclear@0
|
58 // Plain Old Data - movable, no special constructors/destructor.
|
nuclear@0
|
59 template<class T>
|
nuclear@0
|
60 class ConstructorPOD
|
nuclear@0
|
61 {
|
nuclear@0
|
62 public:
|
nuclear@0
|
63 static void Construct(void *) {}
|
nuclear@0
|
64 static void Construct(void *p, const T& source)
|
nuclear@0
|
65 {
|
nuclear@0
|
66 *(T*)p = source;
|
nuclear@0
|
67 }
|
nuclear@0
|
68
|
nuclear@0
|
69 // Same as above, but allows for a different type of constructor.
|
nuclear@0
|
70 template <class S>
|
nuclear@0
|
71 static void ConstructAlt(void *p, const S& source)
|
nuclear@0
|
72 {
|
nuclear@0
|
73 *(T*)p = source;
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@0
|
76 static void ConstructArray(void*, size_t) {}
|
nuclear@0
|
77
|
nuclear@0
|
78 static void ConstructArray(void* p, size_t count, const T& source)
|
nuclear@0
|
79 {
|
nuclear@0
|
80 uint8_t *pdata = (uint8_t*)p;
|
nuclear@0
|
81 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
82 *(T*)pdata = source;
|
nuclear@0
|
83 }
|
nuclear@0
|
84
|
nuclear@0
|
85 static void ConstructArray(void* p, size_t count, const T* psource)
|
nuclear@0
|
86 {
|
nuclear@0
|
87 memcpy(p, psource, sizeof(T) * count);
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 static void Destruct(T*) {}
|
nuclear@0
|
91 static void DestructArray(T*, size_t) {}
|
nuclear@0
|
92
|
nuclear@0
|
93 static void CopyArrayForward(T* dst, const T* src, size_t count)
|
nuclear@0
|
94 {
|
nuclear@0
|
95 memmove(dst, src, count * sizeof(T));
|
nuclear@0
|
96 }
|
nuclear@0
|
97
|
nuclear@0
|
98 static void CopyArrayBackward(T* dst, const T* src, size_t count)
|
nuclear@0
|
99 {
|
nuclear@0
|
100 memmove(dst, src, count * sizeof(T));
|
nuclear@0
|
101 }
|
nuclear@0
|
102
|
nuclear@0
|
103 static bool IsMovable() { return true; }
|
nuclear@0
|
104 };
|
nuclear@0
|
105
|
nuclear@0
|
106
|
nuclear@0
|
107 //-----------------------------------------------------------------------------------
|
nuclear@0
|
108 // ***** ConstructorMov
|
nuclear@0
|
109 //
|
nuclear@0
|
110 // Correct C++ construction and destruction for movable objects
|
nuclear@0
|
111 template<class T>
|
nuclear@0
|
112 class ConstructorMov
|
nuclear@0
|
113 {
|
nuclear@0
|
114 public:
|
nuclear@0
|
115 static void Construct(void* p)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 OVR::Construct<T>(p);
|
nuclear@0
|
118 }
|
nuclear@0
|
119
|
nuclear@0
|
120 static void Construct(void* p, const T& source)
|
nuclear@0
|
121 {
|
nuclear@0
|
122 OVR::Construct<T>(p, source);
|
nuclear@0
|
123 }
|
nuclear@0
|
124
|
nuclear@0
|
125 // Same as above, but allows for a different type of constructor.
|
nuclear@0
|
126 template <class S>
|
nuclear@0
|
127 static void ConstructAlt(void* p, const S& source)
|
nuclear@0
|
128 {
|
nuclear@0
|
129 OVR::ConstructAlt<T,S>(p, source);
|
nuclear@0
|
130 }
|
nuclear@0
|
131
|
nuclear@0
|
132 static void ConstructArray(void* p, size_t count)
|
nuclear@0
|
133 {
|
nuclear@0
|
134 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
135 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
136 Construct(pdata);
|
nuclear@0
|
137 }
|
nuclear@0
|
138
|
nuclear@0
|
139 static void ConstructArray(void* p, size_t count, const T& source)
|
nuclear@0
|
140 {
|
nuclear@0
|
141 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
142 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
143 Construct(pdata, source);
|
nuclear@0
|
144 }
|
nuclear@0
|
145
|
nuclear@0
|
146 static void ConstructArray(void* p, size_t count, const T* psource)
|
nuclear@0
|
147 {
|
nuclear@0
|
148 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
149 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
150 Construct(pdata, *psource++);
|
nuclear@0
|
151 }
|
nuclear@0
|
152
|
nuclear@0
|
153 static void Destruct(T* p)
|
nuclear@0
|
154 {
|
nuclear@0
|
155 p->~T();
|
nuclear@0
|
156 OVR_UNUSED(p); // Suppress silly MSVC warning
|
nuclear@0
|
157 }
|
nuclear@0
|
158
|
nuclear@0
|
159 static void DestructArray(T* p, size_t count)
|
nuclear@0
|
160 {
|
nuclear@0
|
161 p += count - 1;
|
nuclear@0
|
162 for (size_t i=0; i<count; ++i, --p)
|
nuclear@0
|
163 p->~T();
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@0
|
166 static void CopyArrayForward(T* dst, const T* src, size_t count)
|
nuclear@0
|
167 {
|
nuclear@0
|
168 memmove(dst, src, count * sizeof(T));
|
nuclear@0
|
169 }
|
nuclear@0
|
170
|
nuclear@0
|
171 static void CopyArrayBackward(T* dst, const T* src, size_t count)
|
nuclear@0
|
172 {
|
nuclear@0
|
173 memmove(dst, src, count * sizeof(T));
|
nuclear@0
|
174 }
|
nuclear@0
|
175
|
nuclear@0
|
176 static bool IsMovable() { return true; }
|
nuclear@0
|
177 };
|
nuclear@0
|
178
|
nuclear@0
|
179
|
nuclear@0
|
180 //-----------------------------------------------------------------------------------
|
nuclear@0
|
181 // ***** ConstructorCPP
|
nuclear@0
|
182 //
|
nuclear@0
|
183 // Correct C++ construction and destruction for movable objects
|
nuclear@0
|
184 template<class T>
|
nuclear@0
|
185 class ConstructorCPP
|
nuclear@0
|
186 {
|
nuclear@0
|
187 public:
|
nuclear@0
|
188 static void Construct(void* p)
|
nuclear@0
|
189 {
|
nuclear@0
|
190 OVR::Construct<T>(p);
|
nuclear@0
|
191 }
|
nuclear@0
|
192
|
nuclear@0
|
193 static void Construct(void* p, const T& source)
|
nuclear@0
|
194 {
|
nuclear@0
|
195 OVR::Construct<T>(p, source);
|
nuclear@0
|
196 }
|
nuclear@0
|
197
|
nuclear@0
|
198 // Same as above, but allows for a different type of constructor.
|
nuclear@0
|
199 template <class S>
|
nuclear@0
|
200 static void ConstructAlt(void* p, const S& source)
|
nuclear@0
|
201 {
|
nuclear@0
|
202 OVR::ConstructAlt<T,S>(p, source);
|
nuclear@0
|
203 }
|
nuclear@0
|
204
|
nuclear@0
|
205 static void ConstructArray(void* p, size_t count)
|
nuclear@0
|
206 {
|
nuclear@0
|
207 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
208 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
209 Construct(pdata);
|
nuclear@0
|
210 }
|
nuclear@0
|
211
|
nuclear@0
|
212 static void ConstructArray(void* p, size_t count, const T& source)
|
nuclear@0
|
213 {
|
nuclear@0
|
214 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
215 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
216 Construct(pdata, source);
|
nuclear@0
|
217 }
|
nuclear@0
|
218
|
nuclear@0
|
219 static void ConstructArray(void* p, size_t count, const T* psource)
|
nuclear@0
|
220 {
|
nuclear@0
|
221 uint8_t* pdata = (uint8_t*)p;
|
nuclear@0
|
222 for (size_t i=0; i< count; ++i, pdata += sizeof(T))
|
nuclear@0
|
223 Construct(pdata, *psource++);
|
nuclear@0
|
224 }
|
nuclear@0
|
225
|
nuclear@0
|
226 static void Destruct(T* p)
|
nuclear@0
|
227 {
|
nuclear@0
|
228 p->~T();
|
nuclear@0
|
229 OVR_UNUSED(p); // Suppress silly MSVC warning
|
nuclear@0
|
230 }
|
nuclear@0
|
231
|
nuclear@0
|
232 static void DestructArray(T* p, size_t count)
|
nuclear@0
|
233 {
|
nuclear@0
|
234 p += count - 1;
|
nuclear@0
|
235 for (size_t i=0; i<count; ++i, --p)
|
nuclear@0
|
236 p->~T();
|
nuclear@0
|
237 }
|
nuclear@0
|
238
|
nuclear@0
|
239 static void CopyArrayForward(T* dst, const T* src, size_t count)
|
nuclear@0
|
240 {
|
nuclear@0
|
241 for(size_t i = 0; i < count; ++i)
|
nuclear@0
|
242 dst[i] = src[i];
|
nuclear@0
|
243 }
|
nuclear@0
|
244
|
nuclear@0
|
245 static void CopyArrayBackward(T* dst, const T* src, size_t count)
|
nuclear@0
|
246 {
|
nuclear@0
|
247 for(size_t i = count; i; --i)
|
nuclear@0
|
248 dst[i-1] = src[i-1];
|
nuclear@0
|
249 }
|
nuclear@0
|
250
|
nuclear@0
|
251 static bool IsMovable() { return false; }
|
nuclear@0
|
252 };
|
nuclear@0
|
253
|
nuclear@0
|
254
|
nuclear@0
|
255 //-----------------------------------------------------------------------------------
|
nuclear@0
|
256 // ***** Container Allocator with movement policy
|
nuclear@0
|
257 //
|
nuclear@0
|
258 // Simple wraps as specialized allocators
|
nuclear@0
|
259 template<class T> struct ContainerAllocator_POD : ContainerAllocatorBase, ConstructorPOD<T> {};
|
nuclear@0
|
260 template<class T> struct ContainerAllocator : ContainerAllocatorBase, ConstructorMov<T> {};
|
nuclear@0
|
261 template<class T> struct ContainerAllocator_CPP : ContainerAllocatorBase, ConstructorCPP<T> {};
|
nuclear@0
|
262
|
nuclear@0
|
263
|
nuclear@0
|
264 } // OVR
|
nuclear@0
|
265
|
nuclear@0
|
266
|
nuclear@0
|
267 #endif
|