gwnavruntime/kernel/HeapPT/HeapPT_SysAllocMapper.h Source File

HeapPT_SysAllocMapper.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_SysAllocMapper.h
10 Content : System Memory allocator based on memory mapping interface
11 Created : 2009
12 Authors : Maxim Shemanarev
13 
14 Notes : Win32 System Allocator that uses VirtualAlloc
15  and VirualFree.
16 
17 **************************************************************************/
18 
19 #ifndef INC_KY_Kernel_HeapPT_SysAllocMapper_H
20 #define INC_KY_Kernel_HeapPT_SysAllocMapper_H
21 
23 
24 namespace Kaim {
25 
26 // ***** SysAllocMapper
27 //
28 // SysAllocMapper provides a system allocator implementation for
29 // systems that allow to reserve address space and then map and unmap
30 // arbitrary memory pages. O
31 //
32 // This version differs from SysAllocWinAPI and SysAllocMMAP in such a way
33 // that it takes a full advantage of memory mapping. The allocator reserves
34 // address space segments of 128 megabytes and then commits and de-commits
35 // 4K memory pages as necessary. The restrictions are:
36 // 1) The total address space by default cannot exceed 32*128 megabytes.
37 // 2) A single allocation cannot exceed 128 megabytes.
38 // It's good enough in practice, but might be not suitable in some extreme
39 // cases, especially on 64-bit platforms.
40 //
41 // If possible, we recommend that developers use this interface directly instead
42 // of providing their own SysAlloc implementation. Since SysAllocMapper relies on
43 // the low-level OS functions, it intelligently considers both system page size and
44 // granularity, allowing it to be particularly alignment friendly. Due to this alignment
45 // efficiency, SysAllocMapper does not lose any memory on 4K alignment required
46 // internally by MemoryHeap. No alignment overhead means that SysAllocMapper uses
47 // memory and address space with maximum efficiency, making it the best choice for
48 // the Scaleform system allocator on Microsoft platforms.
49 //------------------------------------------------------------------------
50 class SysAllocMapper : public SysAllocPaged
51 {
52  struct Segment
53  {
54  UByte* Memory;
55  UPInt Size;
56  UPInt PageCount;
57  };
58 
59  enum { MaxSegments = 32 };
60 
61 public:
62  // Initializes system allocator.
63  SysAllocMapper(SysMemMapper* mapper, UPInt segSize,
64  UPInt granularity, bool bestFit = true);
65 
66  virtual void GetInfo(Info* i) const;
67  virtual void* Alloc(UPInt size, UPInt align);
68  virtual bool ReallocInPlace(void* oldPtr, UPInt oldSize, UPInt newSize, UPInt align);
69  virtual bool Free(void* ptr, UPInt size, UPInt align);
70 
71  virtual UPInt GetFootprint() const { return Footprint; }
72  virtual UPInt GetUsedSpace() const { return Footprint; }
73 
74  const UInt32* GetBitSet(UPInt numSeg) const
75  {
76  if (numSeg >= NumSegments)
77  {
78  return 0;
79  }
80  return (const UInt32*)(Segments[numSeg].Memory + SegmSize - PageSize);
81  }
82 
83  virtual UPInt GetBase() const; // DBG
84 
85 private:
86  UPInt getBitSize(UPInt size) const;
87  UInt32* getBitSet(const UByte* start, UPInt size) const;
88  UPInt getEndBit(UPInt size) const;
89  UByte* getAlignedPtr(const UByte* ptr, UPInt alignment) const;
90  bool alignmentIsOK(const UByte* ptr, UPInt size, UPInt alignment, UPInt limit) const;
91  void* allocMem(UPInt pos, UPInt size, UPInt alignment);
92  void* allocMem(UPInt size, UPInt alignment);
93  UPInt freeMem(void* ptr, UPInt size);
94 
95  bool inRange(const UByte* ptr, const Segment* seg) const;
96  UPInt binarySearch(const UByte* ptr) const;
97  UPInt findSegment(const UByte* ptr) const;
98  bool reserveSegment(UPInt size);
99  void releaseSegment(UPInt pos);
100 
101  SysMemMapper* pMapper;
102  UPInt PageSize;
103  UPInt PageShift;
104  UPInt SegmSize;
105  UPInt Granularity;
106  UPInt Footprint;
107  Segment Segments[MaxSegments];
108  UPInt NumSegments;
109  UPInt LastSegment;
110  bool BestFit;
111  UByte* LastUsed;
112 };
113 
114 } // Scaleform
115 
116 #endif
Definition: gamekitcrowddispersion.h:20