haiku/headers/private/kernel/util/Bitmap.h
Augustin Cavalier 88275138ba kernel/util: Implement more features in the Bitmap class.
* Resize(): adds more space to the end of the bitmap.
 * Shift(): moves all bits in the map up or down.
 * Use size_t instead of int for indexes.

Also add unit tests for the new functions (they seem to be passing.)

Reference material for shift implementation:
2c56d43c1e/bitops.h (L977)

Change-Id: Ia85768aaeed7bd3ffef3a9f575f05331e048fe50
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5146
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2022-03-30 20:17:50 +00:00

88 lines
1.6 KiB
C++

/*
* Copyright 2013-2022, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Paweł Dziepak, pdziepak@quarnos.org
* Augustin Cavalier <waddlesplash>
*/
#ifndef KERNEL_UTIL_BITMAP_H
#define KERNEL_UTIL_BITMAP_H
#ifdef _KERNEL_MODE
# include <debug.h>
#else
# include <Debug.h>
#endif
#include <SupportDefs.h>
namespace BKernel {
class Bitmap {
public:
Bitmap(size_t bitCount);
~Bitmap();
status_t InitCheck();
status_t Resize(size_t bitCount);
void Shift(ssize_t bitCount);
inline bool Get(size_t index) const;
inline void Set(size_t index);
inline void Clear(size_t index);
ssize_t GetHighestSet() const;
private:
size_t fElementsCount;
size_t fSize;
addr_t* fBits;
static const int kBitsPerElement = (sizeof(addr_t) * 8);
};
bool
Bitmap::Get(size_t index) const
{
ASSERT(index < fSize);
const size_t kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
return fBits[kArrayElement] & kBitMask;
}
void
Bitmap::Set(size_t index)
{
ASSERT(index < fSize);
const size_t kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
fBits[kArrayElement] |= kBitMask;
}
void
Bitmap::Clear(size_t index)
{
ASSERT(index < fSize);
const size_t kArrayElement = index / kBitsPerElement;
const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
fBits[kArrayElement] &= ~addr_t(kBitMask);
}
} // namespace BKernel
using BKernel::Bitmap;
#endif // KERNEL_UTIL_BITMAP_H