gwnavruntime/kernel/SF_Allocator.h Source File

SF_Allocator.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: Kernel
10 Filename : KY_Allocator.h
11 Content : Template allocators, constructors and functions.
12 Created : 2008
13 Authors : Michael Antonov, Maxim Shemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_Allocator_H
18 #define INC_KY_Kernel_Allocator_H
19 
22 #include <string.h>
23 
24 
26 //#if defined(KY_CC_MSVC)
28 //#pragma warning(disable : 4503)
29 //#pragma warning(disable : 4786)
30 //#endif
31 
32 
33 #undef new
34 
35 namespace Kaim {
36 
37 // ***** Construct / Destruct
38 
39 template <class T>
40 KY_INLINE T* Construct(void *p)
41 {
42  return ::new(p) T;
43 }
44 
45 template <class T>
46 KY_INLINE T* Construct(void *p, const T& source)
47 {
48  return ::new(p) T(source);
49 }
50 
51 // Same as above, but allows for a different type of constructor.
52 template <class T, class S>
53 KY_INLINE T* ConstructAlt(void *p, const S& source)
54 {
55  return ::new(p) T(source);
56 }
57 
58 template <class T, class S1, class S2>
59 KY_INLINE T* ConstructAlt(void *p, const S1& src1, const S2& src2)
60 {
61  return ::new(p) T(src1, src2);
62 }
63 
64 template <class T>
65 KY_INLINE void ConstructArray(void *p, UPInt count)
66 {
67  UByte *pdata = (UByte*)p;
68  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
69  {
70  Construct<T>(pdata);
71  }
72 }
73 
74 template <class T>
75 KY_INLINE void ConstructArray(void *p, UPInt count, const T& source)
76 {
77  UByte *pdata = (UByte*)p;
78  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
79  {
80  Construct<T>(pdata, source);
81  }
82 }
83 
84 template <class T>
85 KY_INLINE void Destruct(T *pobj)
86 {
87  pobj->~T();
88 }
89 
90 template <class T>
91 KY_INLINE void DestructArray(T *pobj, UPInt count)
92 {
93  for (UPInt i=0; i<count; ++i, ++pobj)
94  pobj->~T();
95 }
96 
97 
98 
99 // ***** Local Allocator
100 
101 // Local allocator allocates memory for containers which are a part
102 // of other data structures. This allocator determines the allocation
103 // heap and StatId based on the argument address; due to heap limitation
104 // it can not be used for stack-allocated or global values.
105 // - If SID is Stat_Default_Mem, the allocator also inherits the SID
106 //------------------------------------------------------------------------
107 template<int SID = Stat_Default_Mem>
108 class AllocatorBaseLH
109 {
110 public:
111  enum { StatId = SID };
112 
113 #ifdef KY_CONFIG_DEBUG
114  static void* Alloc(const void* pheapAddr, UPInt size, const char* pfile, unsigned line)
115  {
116  return Memory::AllocAutoHeap(pheapAddr, size, AllocInfo(StatId, pfile, line));
117  }
118  static void* Realloc(const void*, void* p, UPInt newSize, const char* pfile, unsigned line)
119  {
120  KY_UNUSED2(pfile, line);
121  return Memory::ReallocAutoHeap(p, newSize);
122  }
123 
124 #else
125 
126  static void* Alloc(const void* pheapAddr, UPInt size, const char*, unsigned)
127  {
128  return Memory::AllocAutoHeap(pheapAddr, size, AllocInfo(StatId, 0, 0));
129  }
130  static void* Realloc(const void*, void* p, UPInt newSize, const char*, unsigned)
131  {
132  return Memory::ReallocAutoHeap(p, newSize);
133  }
134 
135 #endif
136 
137  static void Free(void *p)
138  {
139  Memory::Free(p);
140  }
141 };
142 
143 
144 // ***** Dynamic-Heap Allocator
145 
146 // Dynamic-heap allocator allocates memory for containers created on
147 // the stack, or anywhere else, where the heap pointer is explicitly
148 // specified. The difference is that AllocatorBaseLH takes any
149 // pointer inside the heap, while AllocatorBaseDH requires a pointer
150 // to the heap itself.
151 // - If SID is Stat_Default_Mem, the allocator also inherits the SID
152 //------------------------------------------------------------------------
153 template<int SID = Stat_Default_Mem>
154 class AllocatorBaseDH
155 {
156 public:
157  enum { StatId = SID };
158 
159 #ifdef KY_CONFIG_DEBUG
160  static void* Alloc(const void* pheap, UPInt size, const char* pfile, unsigned line)
161  {
162  return Memory::AllocInHeap((MemoryHeap*)pheap, size, AllocInfo(StatId, pfile, line));
163  }
164  static void* Realloc(const void* pheap, void* p, UPInt newSize, const char* pfile, unsigned line)
165  {
166  KY_UNUSED2(pfile, line);
167  return Memory::ReallocInHeap((MemoryHeap*)pheap, p, newSize);
168  }
169 
170 #else
171 
172  static void* Alloc(const void* pheap, UPInt size, const char*, unsigned)
173  {
174  return Memory::AllocInHeap((MemoryHeap*)pheap, size, AllocInfo(StatId, 0, 0));
175  }
176  static void* Realloc(const void* pheap, void* p, UPInt newSize, const char*, unsigned)
177  {
178  return Memory::ReallocInHeap((MemoryHeap*)pheap, p, newSize);
179  }
180 
181 #endif
182 
183  static void Free(void *p)
184  {
185  Memory::Free(p);
186  }
187 };
188 
189 
190 // ***** Global Allocator
191 
192 // Allocator for items coming from GlobalHeap. StatId must be specified for useful
193 // memory tracking to be performed.
194 //------------------------------------------------------------------------
195 template<int SID = Stat_Default_Mem>
196 class AllocatorBaseGH
197 {
198 public:
199  enum { StatId = SID };
200 
201 #ifdef KY_CONFIG_DEBUG
202  static void* Alloc(const void*, UPInt size, const char* pfile, unsigned line)
203  {
204  return Memory::Alloc(size, AllocInfo(StatId, pfile, line));
205  }
206  static void* Realloc(const void*, void *p, UPInt newSize, const char* pfile, unsigned line)
207  {
208  KY_UNUSED2(pfile, line);
209  return Memory::Realloc(p, newSize);
210  }
211 
212 #else
213 
214  static void* Alloc(const void*, UPInt size, const char*, unsigned)
215  {
216  return Memory::Alloc(size, AllocInfo(StatId, 0, 0));
217  }
218  static void* Realloc(const void*, void *p, UPInt newSize, const char*, unsigned)
219  {
220  return Memory::Realloc(p, newSize);
221  }
222 
223 #endif
224 
225  static void Free(void *p)
226  {
227  if (p) // allow dtor for empty static arrays after ~SF::System
228  Memory::Free(p);
229  }
230 };
231 
232 
233 // ***** Constructors, Destructors, Copiers
234 
235 // Plain Old Data
236 //------------------------------------------------------------------------
237 template<class T>
238 class ConstructorPOD
239 {
240 public:
241  static void Construct(void *) {}
242  static void Construct(void *p, const T& source)
243  {
244  *(T*)p = source;
245  }
246 
247  // Same as above, but allows for a different type of constructor.
248  template <class S>
249  static void ConstructAlt(void *p, const S& source)
250  {
251  *(T*)p = source;
252  }
253 
254  static void ConstructArray(void*, UPInt) {}
255 
256  static void ConstructArray(void* p, UPInt count, const T& source)
257  {
258  UByte *pdata = (UByte*)p;
259  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
260  *(T*)pdata = source;
261  }
262 
263  static void ConstructArray(void* p, UPInt count, const T* psource)
264  {
265  memcpy(p, psource, sizeof(T) * count);
266  }
267 
268  static void Destruct(T*) {}
269  static void DestructArray(T*, UPInt) {}
270 
271  static void CopyArrayForward(T* dst, const T* src, UPInt count)
272  {
273  memmove(dst, src, count * sizeof(T));
274  }
275 
276  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
277  {
278  memmove(dst, src, count * sizeof(T));
279  }
280 
281  static bool IsMovable() { return true; }
282 };
283 
284 
285 // ***** ConstructorMov
286 //
287 // Correct C++ construction and destruction for movable objects
288 //------------------------------------------------------------------------
289 template<class T>
290 class ConstructorMov
291 {
292 public:
293  static void Construct(void* p)
294  {
295  ::new(p) T;
296  }
297 
298  static void Construct(void* p, const T& source)
299  {
300  ::new(p) T(source);
301  }
302 
303  // Same as above, but allows for a different type of constructor.
304  template <class S>
305  static void ConstructAlt(void* p, const S& source)
306  {
307  ::new(p) T(source);
308  }
309 
310  static void ConstructArray(void* p, UPInt count)
311  {
312  UByte* pdata = (UByte*)p;
313  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
314  Construct(pdata);
315  }
316 
317  static void ConstructArray(void* p, UPInt count, const T& source)
318  {
319  UByte* pdata = (UByte*)p;
320  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
321  Construct(pdata, source);
322  }
323 
324  static void ConstructArray(void* p, UPInt count, const T* psource)
325  {
326  UByte* pdata = (UByte*)p;
327  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
328  Construct(pdata, *psource++);
329  }
330 
331  static void Destruct(T* p)
332  {
333  p->~T();
334  KY_UNUSED(p); // Suppress silly MSVC warning
335  }
336 
337  static void DestructArray(T* p, UPInt count)
338  {
339  p += count - 1;
340  for (UPInt i=0; i<count; ++i, --p)
341  p->~T();
342  }
343 
344  static void CopyArrayForward(T* dst, const T* src, UPInt count)
345  {
346  memmove(dst, src, count * sizeof(T));
347  }
348 
349  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
350  {
351  memmove(dst, src, count * sizeof(T));
352  }
353 
354  static bool IsMovable() { return true; }
355 };
356 
357 
358 // ***** ConstructorCPP
359 //
360 // Correct C++ construction and destruction for movable objects
361 //------------------------------------------------------------------------
362 template<class T>
363 class ConstructorCPP
364 {
365 public:
366  static void Construct(void* p)
367  {
368  ::new(p) T;
369  }
370 
371  static void Construct(void* p, const T& source)
372  {
373  ::new(p) T(source);
374  }
375 
376  // Same as above, but allows for a different type of constructor.
377  template <class S>
378  static void ConstructAlt(void* p, const S& source)
379  {
380  ::new(p) T(source);
381  }
382 
383  static void ConstructArray(void* p, UPInt count)
384  {
385  UByte* pdata = (UByte*)p;
386  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
387  Construct(pdata);
388  }
389 
390  static void ConstructArray(void* p, UPInt count, const T& source)
391  {
392  UByte* pdata = (UByte*)p;
393  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
394  Construct(pdata, source);
395  }
396 
397  static void ConstructArray(void* p, UPInt count, const T* psource)
398  {
399  UByte* pdata = (UByte*)p;
400  for (UPInt i=0; i< count; ++i, pdata += sizeof(T))
401  Construct(pdata, *psource++);
402  }
403 
404  static void Destruct(T* p)
405  {
406  p->~T();
407  KY_UNUSED(p); // Suppress silly MSVC warning
408  }
409 
410  static void DestructArray(T* p, UPInt count)
411  {
412  p += count - 1;
413  for (UPInt i=0; i<count; ++i, --p)
414  p->~T();
415  }
416 
417  static void CopyArrayForward(T* dst, const T* src, UPInt count)
418  {
419  for(UPInt i = 0; i < count; ++i)
420  dst[i] = src[i];
421  }
422 
423  static void CopyArrayBackward(T* dst, const T* src, UPInt count)
424  {
425  for(UPInt i = count; i; --i)
426  dst[i-1] = src[i-1];
427  }
428 
429  static bool IsMovable() { return false; }
430 };
431 
432 
433 
434 // ***** Allocator*
435 //
436 // Simple wraps as specialized allocators
437 //------------------------------------------------------------------------
438 template<class T, int SID = Stat_Default_Mem> struct AllocatorGH_POD : AllocatorBaseGH<SID>, ConstructorPOD<T> {};
439 template<class T, int SID = Stat_Default_Mem> struct AllocatorGH : AllocatorBaseGH<SID>, ConstructorMov<T> {};
440 template<class T, int SID = Stat_Default_Mem> struct AllocatorGH_CPP : AllocatorBaseGH<SID>, ConstructorCPP<T> {};
441 
442 template<class T, int SID = Stat_Default_Mem> struct AllocatorLH_POD : AllocatorBaseLH<SID>, ConstructorPOD<T> {};
443 template<class T, int SID = Stat_Default_Mem> struct AllocatorLH : AllocatorBaseLH<SID>, ConstructorMov<T> {};
444 template<class T, int SID = Stat_Default_Mem> struct AllocatorLH_CPP : AllocatorBaseLH<SID>, ConstructorCPP<T> {};
445 
446 template<class T, int SID = Stat_Default_Mem> struct AllocatorDH_POD : AllocatorBaseDH<SID>, ConstructorPOD<T> {};
447 template<class T, int SID = Stat_Default_Mem> struct AllocatorDH : AllocatorBaseDH<SID>, ConstructorMov<T> {};
448 template<class T, int SID = Stat_Default_Mem> struct AllocatorDH_CPP : AllocatorBaseDH<SID>, ConstructorCPP<T> {};
449 
450 } // Scaleform
451 
452 // Redefine operator 'new' if necessary.
453 #if defined(KY_DEFINE_NEW)
454 #define new KY_DEFINE_NEW
455 #endif
456 
457 #endif
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17