ovr_sdk

view LibOVR/Src/Kernel/OVR_ContainerAllocator.h @ 0:1b39a1b46319

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