gwnavgeneration/common/generatormemory.h Source File

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