18 #ifndef INC_KY_Kernel_HeapPT_BitSet1_H
19 #define INC_KY_Kernel_HeapPT_BitSet1_H
24 namespace Kaim {
namespace HeapPT {
33 static void Clear(UInt32* buf, UPInt numWords)
35 memset(buf, 0,
sizeof(UInt32) * numWords);
39 static unsigned GetValue(
const UInt32* buf, UPInt num)
41 return (buf[num >> 5] >> (num & 31)) & 1;
44 static unsigned SetBit(UInt32* buf, UPInt num)
46 return buf[num >> 5] |= UInt32(1) << (num & 31);
49 static unsigned ClrBit(UInt32* buf, UPInt num)
51 return buf[num >> 5] &= ~(UInt32(1) << (num & 31));
55 static void SetUsed(UInt32* buf, UPInt start, UPInt num)
58 UPInt startWord = start >> 5;
59 UPInt endWord = (start + num - 1) >> 5;
60 UPInt tail = (start + num - 1) & 31;
61 if (endWord > startWord)
63 buf[start >> 5] |= HeadUsedTable[start & 31];
64 for(i = startWord + 1; i < endWord; ++i)
68 buf[endWord] |= TailUsedTable[tail];
72 buf[startWord] |= HeadUsedTable[start & 31] & TailUsedTable[tail];
77 static void SetFree(UInt32* buf, UPInt start, UPInt num)
80 UPInt startWord = start >> 5;
81 UPInt endWord = (start + num - 1) >> 5;
82 UPInt tail = (start + num - 1) & 31;
83 if (endWord > startWord)
85 buf[start >> 5] &= HeadFreeTable[start & 31];
86 for(i = startWord + 1; i < endWord; ++i)
90 buf[endWord] &= TailFreeTable[tail];
94 buf[startWord] &= HeadFreeTable[start & 31] | TailFreeTable[tail];
99 static UPInt FindLastUsedInWord(UInt32 bits)
101 if ((bits & 0xFFFF) != 0xFFFF)
103 return ((bits & 0xFF) == 0xFF) ?
104 LastUsedBlock[(bits >> 8 ) & 0xFF] + 8:
105 LastUsedBlock[ bits & 0xFF] + 0;
107 return ((bits & 0xFFFFFF) == 0xFFFFFF) ?
108 LastUsedBlock[(bits >> 24) & 0xFF] + 24:
109 LastUsedBlock[(bits >> 16) & 0xFF] + 16;
113 static UPInt FindLastFreeInWord(UInt32 bits)
117 return (bits & 0xFF) ?
118 LastFreeBlock[ bits & 0xFF] + 0:
119 LastFreeBlock[(bits >> 8 ) & 0xFF] + 8;
121 return (bits & 0xFF0000) ?
122 LastFreeBlock[(bits >> 16) & 0xFF] + 16:
123 LastFreeBlock[(bits >> 24) & 0xFF] + 24;
127 static UPInt FindUsedSize(
const UInt32* buf, UPInt start, UPInt limit)
129 UPInt startWord = start >> 5;
131 UInt32 mask = HeadUsedTable[start & 31];
132 UInt32 bits = buf[startWord] & mask;
135 return FindLastUsedInWord(bits >> (start & 31));
137 size += 32 - (start & 31);
138 while ((startWord+1)*32 < limit && buf[++startWord] == ~UInt32(0))
142 return size + FindLastUsedInWord(buf[startWord]);
146 static UPInt FindFreeSize(
const UInt32* buf, UPInt start)
148 UPInt startWord = start >> 5;
150 UInt32 mask = HeadFreeTable[start & 31];
151 UInt32 bits = buf[startWord] | mask;
154 return FindLastFreeInWord(bits >> (start & 31));
156 size += 32 - (start & 31);
157 while (buf[++startWord] == 0)
161 return size + FindLastFreeInWord(buf[startWord]);
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];
Definition: gamekitcrowddispersion.h:20