kernel/util: Add bitmap implementation

This commit is contained in:
Pawel Dziepak
2013-10-03 04:27:49 +02:00
parent 7087b865e2
commit 149c82a8ec
4 changed files with 168 additions and 0 deletions
+18
View File
@@ -38,5 +38,23 @@ countSetBits(uint32 v)
}
static inline uint32
log2(uint32 v)
{
static const int MultiplyDeBruijnBitPosition[32] = {
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return MultiplyDeBruijnBitPosition[(uint32)(v * 0x07C4ACDDU) >> 27];
}
#endif // KERNEL_UTIL_RANDOM_H
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright 2013 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Paweł Dziepak, [email protected]
*/
#ifndef KERNEL_UTIL_BITMAP_H
#define KERNEL_UTIL_BITMAP_H
#include <Debug.h>
#include <SupportDefs.h>
class Bitmap {
public:
Bitmap(int bitCount);
~Bitmap();
inline status_t GetInitStatus();
inline bool Get(int index) const;
inline void Set(int index);
inline void Clear(int index);
int GetHighestSet() const;
private:
status_t fInitStatus;
int fElementsCount;
int fSize;
addr_t* fBits;
static const int kBitsPerElement;
};
status_t
Bitmap::GetInitStatus()
{
return fInitStatus;
}
bool
Bitmap::Get(int index) const
{
ASSERT(index < fSize);
const int kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
return fBits[kArrayElement] & kBitMask;
}
void
Bitmap::Set(int index)
{
ASSERT(index < fSize);
const int kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
fBits[kArrayElement] |= kBitMask;
}
void
Bitmap::Clear(int index)
{
ASSERT(index < fSize);
const int kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
fBits[kArrayElement] &= ~addr_t(kBitMask);
}
#endif // KERNEL_UTIL_BITMAP_H