move some generally useful bitset functions from heap.cpp to common.h

This commit is contained in:
Joel Dice 2008-01-06 12:21:38 -07:00
parent 7a0079e258
commit a8e9cc521c
2 changed files with 31 additions and 18 deletions

View File

@ -166,6 +166,34 @@ getBit(uintptr_t* map, unsigned i)
>> bitOf(i); >> bitOf(i);
} }
inline void
clearBits(uintptr_t* map, unsigned bitsPerRecord, unsigned index)
{
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
clearBit(map, i);
}
}
inline void
setBits(uintptr_t* map, unsigned bitsPerRecord, int index, unsigned v)
{
for (int i = index + bitsPerRecord - 1; i >= index; --i) {
if (v & 1) markBit(map, i); else clearBit(map, i);
v >>= 1;
}
}
inline unsigned
getBits(uintptr_t* map, unsigned bitsPerRecord, unsigned index)
{
unsigned v = 0;
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
v <<= 1;
v |= getBit(map, i);
}
return v;
}
template <class T> template <class T>
inline T& inline T&
cast(void* p, unsigned offset) cast(void* p, unsigned offset)

View File

@ -219,9 +219,7 @@ class Segment {
} }
void clearOnlyIndex(unsigned index) { void clearOnlyIndex(unsigned index) {
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) { clearBits(data(), bitsPerRecord, index);
clearBit(i);
}
} }
void clearOnly(unsigned segmentIndex) { void clearOnly(unsigned segmentIndex) {
@ -238,13 +236,7 @@ class Segment {
} }
void setOnlyIndex(unsigned index, unsigned v = 1) { void setOnlyIndex(unsigned index, unsigned v = 1) {
unsigned i = index + bitsPerRecord - 1; setBits(data(), bitsPerRecord, index, v);
while (true) {
if (v & 1) setBit(i); else clearBit(i);
v >>= 1;
if (i == index) break;
--i;
}
} }
void setOnly(unsigned segmentIndex, unsigned v = 1) { void setOnly(unsigned segmentIndex, unsigned v = 1) {
@ -262,14 +254,7 @@ class Segment {
} }
unsigned get(void* p) { unsigned get(void* p) {
unsigned index = indexOf(p); return getBits(data(), bitsPerRecord, indexOf(p));
unsigned v = 0;
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
unsigned wi = bitOf(i);
v <<= 1;
v |= ((data()[wordOf(i)]) & (static_cast<uintptr_t>(1) << wi)) >> wi;
}
return v;
} }
unsigned footprint(unsigned capacity) { unsigned footprint(unsigned capacity) {