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