From a8e9cc521c68988026a960d16a91beef5897357f Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sun, 6 Jan 2008 12:21:38 -0700 Subject: [PATCH] move some generally useful bitset functions from heap.cpp to common.h --- src/common.h | 28 ++++++++++++++++++++++++++++ src/heap.cpp | 21 +++------------------ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/common.h b/src/common.h index bce6455ab2..395aa33dd4 100644 --- a/src/common.h +++ b/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 inline T& cast(void* p, unsigned offset) diff --git a/src/heap.cpp b/src/heap.cpp index 7866bb3b23..09f0dd6f2b 100644 --- a/src/heap.cpp +++ b/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(1) << wi)) >> wi; - } - return v; + return getBits(data(), bitsPerRecord, indexOf(p)); } unsigned footprint(unsigned capacity) {