/* * Copyright 2013-2022, Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Paweł Dziepak, pdziepak@quarnos.org * Augustin Cavalier */ #ifndef KERNEL_UTIL_BITMAP_H #define KERNEL_UTIL_BITMAP_H #ifdef _KERNEL_MODE # include #else # include #endif #include 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