mirror of
https://github.com/corda/corda.git
synced 2025-01-06 05:04:20 +00:00
move some generally useful bitset functions from heap.cpp to common.h
This commit is contained in:
parent
7a0079e258
commit
a8e9cc521c
28
src/common.h
28
src/common.h
@ -166,6 +166,34 @@ getBit(uintptr_t* map, unsigned 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>
|
||||
inline T&
|
||||
cast(void* p, unsigned offset)
|
||||
|
21
src/heap.cpp
21
src/heap.cpp
@ -219,9 +219,7 @@ class Segment {
|
||||
}
|
||||
|
||||
void clearOnlyIndex(unsigned index) {
|
||||
for (unsigned i = index, limit = index + bitsPerRecord; i < limit; ++i) {
|
||||
clearBit(i);
|
||||
}
|
||||
clearBits(data(), bitsPerRecord, index);
|
||||
}
|
||||
|
||||
void clearOnly(unsigned segmentIndex) {
|
||||
@ -238,13 +236,7 @@ class Segment {
|
||||
}
|
||||
|
||||
void setOnlyIndex(unsigned index, unsigned v = 1) {
|
||||
unsigned i = index + bitsPerRecord - 1;
|
||||
while (true) {
|
||||
if (v & 1) setBit(i); else clearBit(i);
|
||||
v >>= 1;
|
||||
if (i == index) break;
|
||||
--i;
|
||||
}
|
||||
setBits(data(), bitsPerRecord, index, v);
|
||||
}
|
||||
|
||||
void setOnly(unsigned segmentIndex, unsigned v = 1) {
|
||||
@ -262,14 +254,7 @@ class Segment {
|
||||
}
|
||||
|
||||
unsigned get(void* p) {
|
||||
unsigned index = 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;
|
||||
return getBits(data(), bitsPerRecord, indexOf(p));
|
||||
}
|
||||
|
||||
unsigned footprint(unsigned capacity) {
|
||||
|
Loading…
Reference in New Issue
Block a user