gwnavgeneration/common/generatormemory.h Source File

generatormemory.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 #ifndef GwNavGen_GeneratorMemory_H
9 #define GwNavGen_GeneratorMemory_H
10 
11 
17 
18 namespace Kaim {
19 
20 
21 class ITlsAlloc : public RefCountBaseV<ITlsAlloc, MemStat_NavDataGen>
22 {
23 public:
24  virtual ~ITlsAlloc() {}
25  virtual void* Alloc(UPInt size) = 0;
26  virtual void Free(void* ptr) = 0;
27  virtual void* Realloc(void* oldPtr, UPInt newSize) = 0;
28 };
29 
30 
31 class GeneratorMemoryHeap;
32 
33 class GeneratorMemory
34 {
35 public:
36  static void InitTlsAlloc(ITlsAlloc* tlsAlloc);
37  static void ClearTlsAlloc();
38 
39  static MemoryHeap* GlobalHeap() { return Memory::pGlobalHeap; }
40  static MemoryHeap* SysHeap() { return s_sysHeap; }
41  static MemoryHeap* TlsHeap() { return s_tlsHeap; }
42  static MemoryHeap* MagicHeap() { return s_magicHeap; }
43 
44 public:
45  static MemoryHeap* s_sysHeap;
46  static MemoryHeap* s_tlsHeap;
47  static MemoryHeap* s_magicHeap;
48 };
49 
50 
51 // Optimized allocator used in generator, use an optional TLS memory heap to reduce contention in multi-threaded computations
52 // You should derive from GeneratorSysAlloc to hook your own Alloc/Free/Realloc
53 class GeneratorSysAlloc : public SysAllocBase
54 {
55 public:
56  GeneratorSysAlloc() { Clear(); }
57  virtual ~GeneratorSysAlloc() {}
58 
59  enum AllocType { SlowSimpleAlloc = 0, FastMixedAlloc = 1 };
60 
61  // Must be called before BaseSystem::Init()
62  // The default is FastMixedAlloc, this enables to get very good multi-core performance
63  // at a very reasonable memory cost (sizeof(void*) per alloc)
64  // It is strongly recommended that you don't call this function
65  void SetAllocType(AllocType allocType)
66  {
67  if (m_heapEngineInitialized == false) // does nothing if initHeapEngine() has been called already
68  m_allocType = allocType;
69  }
70 
71  // respect the same Alloc/Free/Realloc prototypes than all SysAllocBase derivation
72  virtual void* Alloc(UPInt size, UPInt align) = 0;
73  virtual void Free(void* ptr, UPInt size, UPInt align) = 0;
74  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align) = 0;
75 
76 protected:
77  // Implementation of SysAllocBase based on GeneratorMemoryHeap
78  virtual bool initHeapEngine(const void* heapDesc);
79  virtual void shutdownHeapEngine();
80 
81  void Clear()
82  {
83  m_allocType = FastMixedAlloc;
84  m_heapEngineInitialized = false;
85  }
86 
87 protected:
88  AllocType m_allocType;
89  bool m_heapEngineInitialized;
90 };
91 
92 
93 // GeneratorSysAlloc derivation that implements Alloc/Free/Realloc with StraightMalloc
94 // GeneratorSysAllocMalloc is the class that should be used in Generator BaseSystem initialization:
95 // Kaim::BaseSystem::Config config;
96 // config.m_sysAlloc = Kaim::GeneratorSysAllocMalloc::InitSystemSingleton();
97 // Kaim::BaseSystem baseSystem(config);
98 class GeneratorSysAllocMalloc : public SysAllocBase_SingletonSupport<GeneratorSysAllocMalloc, GeneratorSysAlloc>
99 {
100 public:
101  GeneratorSysAllocMalloc() {}
102  virtual ~GeneratorSysAllocMalloc() {}
103 
104  // Implementation of SysAlloc based on malloc and free (WINDOWS an LINUX only)
105  virtual void* Alloc(UPInt size, UPInt align) { return StraightSysAllocMalloc::Alloc(size, align); }
106  virtual void Free(void* ptr, UPInt size, UPInt align) { return StraightSysAllocMalloc::Free(ptr, size, align); }
107  virtual void* Realloc(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align) { return StraightSysAllocMalloc::Realloc(oldPtr, oldSize, newSize, align); }
108 };
109 
110 
111 
112 }
113 
114 #endif
Definition: gamekitcrowddispersion.h:20