gwnavruntime/kernel/HeapPT/HeapPT_BitSet1.h Source File

HeapPT_BitSet1.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_BitSet1.h
10 Content :
11 Created : 2009
12 Authors : Maxim Shemanarev
13 
14 Notes : Allocator bit-set maintanance
15 
16 **************************************************************************/
17 
18 #ifndef INC_KY_Kernel_HeapPT_BitSet1_H
19 #define INC_KY_Kernel_HeapPT_BitSet1_H
20 
21 #include <string.h>
23 
24 namespace Kaim { namespace HeapPT {
25 
26 // ***** BitSet1
27 //
28 //------------------------------------------------------------------------
29 class BitSet1
30 {
31 public:
32  //--------------------------------------------------------------------
33  static void Clear(UInt32* buf, UPInt numWords)
34  {
35  memset(buf, 0, sizeof(UInt32) * numWords);
36  }
37 
38  //--------------------------------------------------------------------
39  static unsigned GetValue(const UInt32* buf, UPInt num)
40  {
41  return (buf[num >> 5] >> (num & 31)) & 1;
42  }
43 
44  static unsigned SetBit(UInt32* buf, UPInt num)
45  {
46  return buf[num >> 5] |= UInt32(1) << (num & 31);
47  }
48 
49  static unsigned ClrBit(UInt32* buf, UPInt num)
50  {
51  return buf[num >> 5] &= ~(UInt32(1) << (num & 31));
52  }
53 
54  //--------------------------------------------------------------------
55  static void SetUsed(UInt32* buf, UPInt start, UPInt num)
56  {
57  UPInt i;
58  UPInt startWord = start >> 5;
59  UPInt endWord = (start + num - 1) >> 5;
60  UPInt tail = (start + num - 1) & 31;
61  if (endWord > startWord)
62  {
63  buf[start >> 5] |= HeadUsedTable[start & 31];
64  for(i = startWord + 1; i < endWord; ++i)
65  {
66  buf[i] = ~UInt32(0);
67  }
68  buf[endWord] |= TailUsedTable[tail];
69  }
70  else
71  {
72  buf[startWord] |= HeadUsedTable[start & 31] & TailUsedTable[tail];
73  }
74  }
75 
76  //--------------------------------------------------------------------
77  static void SetFree(UInt32* buf, UPInt start, UPInt num)
78  {
79  UPInt i;
80  UPInt startWord = start >> 5;
81  UPInt endWord = (start + num - 1) >> 5;
82  UPInt tail = (start + num - 1) & 31;
83  if (endWord > startWord)
84  {
85  buf[start >> 5] &= HeadFreeTable[start & 31];
86  for(i = startWord + 1; i < endWord; ++i)
87  {
88  buf[i] = 0;
89  }
90  buf[endWord] &= TailFreeTable[tail];
91  }
92  else
93  {
94  buf[startWord] &= HeadFreeTable[start & 31] | TailFreeTable[tail];
95  }
96  }
97 
98  //--------------------------------------------------------------------
99  static UPInt FindLastUsedInWord(UInt32 bits)
100  {
101  if ((bits & 0xFFFF) != 0xFFFF)
102  {
103  return ((bits & 0xFF) == 0xFF) ?
104  LastUsedBlock[(bits >> 8 ) & 0xFF] + 8:
105  LastUsedBlock[ bits & 0xFF] + 0;
106  }
107  return ((bits & 0xFFFFFF) == 0xFFFFFF) ?
108  LastUsedBlock[(bits >> 24) & 0xFF] + 24:
109  LastUsedBlock[(bits >> 16) & 0xFF] + 16;
110  }
111 
112  //--------------------------------------------------------------------
113  static UPInt FindLastFreeInWord(UInt32 bits)
114  {
115  if (bits & 0xFFFF)
116  {
117  return (bits & 0xFF) ?
118  LastFreeBlock[ bits & 0xFF] + 0:
119  LastFreeBlock[(bits >> 8 ) & 0xFF] + 8;
120  }
121  return (bits & 0xFF0000) ?
122  LastFreeBlock[(bits >> 16) & 0xFF] + 16:
123  LastFreeBlock[(bits >> 24) & 0xFF] + 24;
124  }
125 
126  //--------------------------------------------------------------------
127  static UPInt FindUsedSize(const UInt32* buf, UPInt start, UPInt limit)
128  {
129  UPInt startWord = start >> 5;
130  UPInt size = 0;
131  UInt32 mask = HeadUsedTable[start & 31];
132  UInt32 bits = buf[startWord] & mask;
133  if (bits != mask)
134  {
135  return FindLastUsedInWord(bits >> (start & 31));
136  }
137  size += 32 - (start & 31);
138  while ((startWord+1)*32 < limit && buf[++startWord] == ~UInt32(0))
139  {
140  size += 32;
141  }
142  return size + FindLastUsedInWord(buf[startWord]);
143  }
144 
145  //--------------------------------------------------------------------
146  static UPInt FindFreeSize(const UInt32* buf, UPInt start)
147  {
148  UPInt startWord = start >> 5;
149  UPInt size = 0;
150  UInt32 mask = HeadFreeTable[start & 31];
151  UInt32 bits = buf[startWord] | mask;
152  if (bits != mask)
153  {
154  return FindLastFreeInWord(bits >> (start & 31));
155  }
156  size += 32 - (start & 31);
157  while (buf[++startWord] == 0)
158  {
159  size += 32;
160  }
161  return size + FindLastFreeInWord(buf[startWord]);
162  }
163 
164 private:
165  //--------------------------------------------------------------------
166  static const UInt32 HeadUsedTable[32];
167  static const UInt32 TailUsedTable[32];
168  static const UInt32 HeadFreeTable[32];
169  static const UInt32 TailFreeTable[32];
170  static const UByte LastFreeBlock[256];
171  static const UByte LastUsedBlock[256];
172 };
173 
174 
175 }} // Kaim::Heap
176 
177 #endif
Definition: gamekitcrowddispersion.h:20