gwnavruntime/kernel/HeapMH/HeapMH_FreeBin.h Source File

HeapMH_FreeBin.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 /**************************************************************************
8 
9 Filename : HeapMH_FreeBin.h
10 Content :
11 Created : 2010
12 Authors : Maxim Shemanarev
13 
14 Notes : Containers to store information about free memory areas
15 
16 **************************************************************************/
17 
18 #pragma once
19 
21 
22 namespace Kaim { namespace HeapMH {
23 
24 //UInt32 Magic;
25 //UInt32 Index;
26 //UByte UseCount;
27 //SByte BitSet;
28 //UByte DataStart;
29 //UByte DataEnd;
30 
31 
32 struct BinNodeMH
33 {
34 #ifdef KY_64BIT_POINTERS
35  enum { MinBlocks = 2 };
36 #else
37  enum { MinBlocks = 1 };
38 #endif
39 
40  UPInt Prev;
41  UPInt Next;
42  PageMH* Page;
43 
44  UPInt GetBlocks() const
45  {
46  return (Prev & 0xF) | ((Next & 0xF) << 4);
47  }
48 
49  UPInt GetBytes() const
50  {
51  return GetBlocks() << PageMH::UnitShift;
52  }
53 
54  UPInt GetPrevBlocks() const
55  {
56  const UPInt* tail = ((const UPInt*)this) - 2;
57  return (tail[0] & 0xF) | ((tail[1] & 0xF) << 4);
58  }
59 
60  UPInt GetPrevBytes() const
61  {
62  return GetPrevBlocks() << PageMH::UnitShift;
63  }
64 
65  void SetBlocks(UPInt blocks)
66  {
67  UPInt bytes = blocks << PageMH::UnitShift;
68  UPInt* tail = ((UPInt*)this) + bytes / sizeof(UPInt) - 2;
69  tail[0] = Prev = (Prev & ~UPInt(0xF)) | (blocks & 0xF);
70  tail[1] = Next = (Next & ~UPInt(0xF)) | (blocks >> 4);
71  }
72 
73  void SetBytes(UPInt bytes)
74  {
75  UPInt blocks = bytes >> PageMH::UnitShift;
76  UPInt* tail = ((UPInt*)this) + bytes / sizeof(UPInt) - 2;
77  tail[0] = Prev = (Prev & ~UPInt(0xF)) | (blocks & 0xF);
78  tail[1] = Next = (Next & ~UPInt(0xF)) | (blocks >> 4);
79  }
80 
81  PageMH* GetPage()
82  {
83  return (GetBlocks() > MinBlocks) ? Page : 0;
84  }
85 
86  void SetPage(PageMH* page)
87  {
88  if (GetBlocks() > MinBlocks)
89  Page = page;
90  }
91 
92  BinNodeMH* GetPrev() const { return (BinNodeMH*)(Prev & ~UPInt(0xF)); }
93  BinNodeMH* GetNext() const { return (BinNodeMH*)(Next & ~UPInt(0xF)); }
94 
95  void SetPrev(BinNodeMH* prev) { Prev = UPInt(prev) | (Prev & 0xF); }
96  void SetNext(BinNodeMH* next) { Next = UPInt(next) | (Next & 0xF); }
97 
98  static BinNodeMH* MakeNode(UByte* start, UPInt bytes, PageMH* page)
99  {
100  BinNodeMH* node = (BinNodeMH*)start;
101  node->SetBytes(bytes);
102  node->SetPage(page);
103  return node;
104  }
105 };
106 
107 
108 //------------------------------------------------------------------------
109 struct ListBinMH
110 {
111  enum { BinSize = 8*sizeof(UPInt) }; // Assume Byte is 8 bits.
112 
113  ListBinMH();
114  void Reset();
115 
116  //--------------------------------------------------------------------
117  void Push(UByte* node);
118  void Pull(UByte* node);
119 
120  BinNodeMH* PullBest(UPInt blocks);
121  BinNodeMH* PullBest(UPInt blocks, UPInt alignMask);
122 
123  void Merge(UByte* node, UPInt bytes, bool left, bool right, PageMH* page);
124 
125  static UByte* GetAlignedPtr(UByte* start, UPInt alignMask);
126  static bool AlignmentIsOK(const BinNodeMH* node, UPInt blocks, UPInt alignMask);
127 
128 private:
129  void pushNode(UPInt idx, BinNodeMH* node);
130  void pullNode(UPInt idx, BinNodeMH* node);
131 
132  BinNodeMH* findAligned(BinNodeMH* root, UPInt blocks, UPInt alignMask);
133 
134 
135  BinNodeMH* getPrevAdjacent(UByte* node) const;
136  BinNodeMH* getNextAdjacent(UByte* node) const;
137 
138  //--------------------------------------------------------------------
139  UPInt Mask;
140  BinNodeMH* Roots[BinSize];
141 };
142 
143 
144 
145 }} // Kaim::HeapMH
146 
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17