gwnavruntime/kernel/HeapPT/HeapPT_AllocBitSet1.h Source File

HeapPT_AllocBitSet1.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_AllocBitSet1.h
10 Content : Bit-set based allocator, 1 bit per block.
11 
12 Created : 2009
13 Authors : Maxim Shemanarev
14 
15 **************************************************************************/
16 
17 #ifndef INC_KY_Kernel_HeapPT_AllocBitSet1_H
18 #define INC_KY_Kernel_HeapPT_AllocBitSet1_H
19 
21 
22 namespace Kaim { namespace HeapPT {
23 
24 //------------------------------------------------------------------------
25 class AllocBitSet1
26 {
27 public:
28  AllocBitSet1(UPInt minAlignShift);
29 
30  static UInt32* GetBitSet(const HeapSegment* seg)
31  {
32  return (UInt32*)(((UByte*)seg) + sizeof(HeapSegment));
33  }
34 
35  UPInt GetBitSetWords(UPInt dataSize) const
36  {
37  UPInt blocks = (dataSize + MinAlignMask) >> MinAlignShift;
38  return (blocks + 31) >> 5;
39  }
40 
41  UPInt GetBitSetBytes(UPInt dataSize) const
42  {
43  return GetBitSetWords(dataSize) * sizeof(UInt32);
44  }
45 
46  void InitSegment(HeapSegment* seg);
47  void ReleaseSegment(HeapSegment* seg);
48 
49  UPInt AlignSize(UPInt size) const
50  {
51  return (size + MinAlignMask) & ~MinAlignMask;
52  }
53 
54  void* Alloc(UPInt size, HeapSegment** allocSeg);
55  void Free(HeapSegment* seg, void* ptr, UPInt size);
56 
57  UPInt GetTotalFreeSpace() const
58  {
59  return Bin.GetTotalFreeSpace(MinAlignShift);
60  }
61 
62  void VisitMem(MemVisitor* visitor, MemVisitor::Category cat) const
63  {
64  Bin.VisitMem(visitor, MinAlignShift, cat);
65  }
66 
67  void VisitUnused(SegVisitor* visitor, unsigned cat) const
68  {
69  Bin.VisitUnused(visitor, MinAlignShift, cat);
70  }
71 
72 private:
73  KY_INLINE static void clrBit(UInt32* bitSet, UPInt idx)
74  {
75  bitSet[idx >> 5] &= ~(UInt32(1) << (idx & 31));
76  }
77 
78  KY_INLINE static void setBit(UInt32* bitSet, UPInt idx)
79  {
80  bitSet[idx >> 5] |= UInt32(1) << (idx & 31);
81  }
82 
83  KY_INLINE static void markBusy(UInt32* bitSet, UPInt start, UPInt size)
84  {
85  setBit(bitSet, start);
86  setBit(bitSet, start+size-1);
87  }
88 
89  KY_INLINE static void markFree(UInt32* bitSet, UPInt start, UPInt size)
90  {
91  clrBit(bitSet, start);
92  clrBit(bitSet, start+size-1);
93  }
94 
95  KY_INLINE static bool isZero(const UInt32* bitSet, UPInt idx)
96  {
97  return (bitSet[idx >> 5] & (UInt32(1) << (idx & 31))) == 0;
98  }
99 
100  UPInt MinAlignShift;
101  UPInt MinAlignMask;
102  FreeBin Bin;
103 };
104 
105 
106 }} // Kaim::Heap
107 
108 #endif
Definition: gamekitcrowddispersion.h:20