gwnavruntime/kernel/SF_Allocator.h Source File

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