gwnavruntime/kernel/HeapPT/HeapPT_AllocEngine.h Source File

HeapPT_AllocEngine.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 Filename : HeapPT_AllocEngine.h
10 Content : The main allocation engine
11  :
12 Created : 2009
13 Authors : Maxim Shemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_HeapPT_AllocEngine_H
18 #define INC_KY_Kernel_HeapPT_AllocEngine_H
19 
24 
25 namespace Kaim { namespace HeapPT {
26 
27 //------------------------------------------------------------------------
28 struct HeapOtherStats
29 {
30  UPInt Segments;
31  UPInt Bookkeeping;
32  UPInt DynamicGranularity;
33  UPInt SysDirectSpace;
34 };
35 
36 //------------------------------------------------------------------------
37 class AllocEngine
38 {
39 public:
40  //-------------------------------------------------------------------
41  struct TinyBlock : ListNode<TinyBlock>
42  {
43  HeapSegment* pSegment;
44  };
45 
46 private:
47  typedef List<HeapSegment> SegmentListType;
48  typedef List<TinyBlock> TinyListType;
49 
50 public:
51  //--------------------------------------------------------------------
52  AllocEngine(SysAllocPaged* sysAlloc,
53  MemoryHeapPT* heap,
54  unsigned allocFlags,
55  UPInt minAlignSize=16,
56  UPInt granularity=8*1024,
57  UPInt reserve=8*1024,
58  UPInt internalThreshold=128*1024,
59  UPInt limit=0);
60 
61  ~AllocEngine();
62 
63  bool IsValid() const { return Valid; }
64 
65  UPInt SetLimit(UPInt lim);
66  void SetLimitHandler(void* handler) { pLimHandler = handler; }
67 
68  // Calling FreeAll() releases all memory the heap holds,
69  // but the heap remains functional.
70  void FreeAll();
71 
72  // External functions
73  //--------------------------------------------------------------------
74  void* Alloc(UPInt size);
75  void* Alloc(UPInt size, UPInt alignSize);
76  void* Realloc(HeapSegment* seg, void* oldPtr, UPInt newSize);
77  void Free(HeapSegment* seg, void* ptr);
78 
79  void* Realloc(void* oldPtr, UPInt newSize);
80  void Free(void* ptr);
81 
82  UPInt GetUsableSize(const void* ptr);
83  UPInt GetUsableSize(HeapSegment* seg, const void* ptr);
84 
85  UPInt GetFootprint() const;
86  UPInt GetUsedSpace() const;
87 
88  void ReleaseCachedMem();
89 
90  // See MemVisitor::VisitingFlags for "flags" argument.
91  void VisitMem(MemVisitor* visitor,
92  unsigned flags = MemVisitor::VF_Heap) const;
93 
94  void VisitSegments(SegVisitor* visitor) const;
95 
96  void GetHeapOtherStats(HeapOtherStats* otherStats) const;
97 
98  void CheckIntegrity() const
99  {
100  Allocator.CheckIntegrity();
101  }
102 
103 private:
104  //--------------------------------------------------------------------
105  static void compilerAsserts(); // A fake function used for KY_COMPILER_ASSERTS only
106 
107  HeapSegment* allocSegment(unsigned segType, UPInt size, UPInt sysAlignment,
108  UPInt bookkeepingSize, bool* limHandlerOK);
109 
110  HeapSegment* allocSegmentNoGranulator(UPInt dataSize, UPInt alignment, bool* limHandlerOK);
111 
112  HeapSegment* allocSegmentLocked(unsigned segType, UPInt size, UPInt sysAlignment,
113  UPInt bookkeepingSize, bool* limHandlerOK);
114 
115  void freeSegment(HeapSegment* seg);
116  void freeSegmentLocked(HeapSegment* seg);
117 
118  UPInt calcDynaSize() const;
119  HeapSegment* allocSegmentBitSet(UPInt size, UPInt alignSize,
120  UPInt granularity,
121  bool* limHandlerOK);
122 
123  HeapSegment* allocSegmentBitSet(UPInt size, UPInt alignSize, bool* limHandlerOK);
124 
125  void freeSegmentBitSet(HeapSegment* seg);
126 
127  void* allocSysDirect(UPInt dataSize, UPInt alignSize);
128  void* reallocSysDirect(HeapSegment* seg, void* oldPtr, UPInt newSize);
129 
130  void* reallocGeneral(HeapSegment* seg, void* oldPtr,
131  UPInt oldSize, UPInt newSize,
132  UPInt alignShift);
133 
134  void* allocBitSet(UPInt size);
135  void* allocBitSet(UPInt size, UPInt alignSize);
136 
137  void* allocSegmentTiny(unsigned idx);
138  void releaseSegmentTiny(HeapSegment* seg);
139  void freeSegmentTiny(HeapSegment* seg);
140  void* allocTiny(unsigned sizeIdx);
141  void freeTiny(HeapSegment* seg, TinyBlock* ptr);
142 
143 private:
144  //--------------------------------------------------------------------
145  MemoryHeapPT* pHeap;
146  SysAllocPaged* pSysAlloc;
147  Bookkeeper* pBookkeeper;
148  UPInt MinAlignShift;
149  UPInt MinAlignMask;
150  AllocBitSet2 Allocator;
151  SegmentListType SegmentList;
152  TinyListType TinyBlocks[Heap_TinyBinSize];
153  bool AllowTinyBlocks;
154  bool AllowDynaSize;
155  bool Valid;
156  bool HasRealloc;
157  UPInt SysGranularity;
158  UPInt Granularity;
159  UPInt Reserve;
160  UPInt Threshold;
161  UPInt SysDirectThreshold;
162  UPInt Footprint;
163  UPInt TinyFreeSpace;
164  UPInt SysDirectSpace;
165  HeapSegment* pCachedBSeg; // Cached bit-set segment
166  HeapSegment* pCachedTSeg; // Cached tiny blocks segment
167  UPInt Limit;
168  void* pLimHandler;
169 };
170 
171 }} // Kaim::Heap
172 
173 #endif
174 
Definition: gamekitcrowddispersion.h:20