1 /* 2 * Copyright 2013 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Paweł Dziepak, pdziepak@quarnos.org 7 */ 8 #ifndef KERNEL_UTIL_BITMAP_H 9 #define KERNEL_UTIL_BITMAP_H 10 11 12 #include <debug.h> 13 14 #include <SupportDefs.h> 15 16 17 class Bitmap { 18 public: 19 Bitmap(int bitCount); 20 ~Bitmap(); 21 22 inline status_t GetInitStatus(); 23 24 inline bool Get(int index) const; 25 inline void Set(int index); 26 inline void Clear(int index); 27 28 int GetHighestSet() const; 29 30 private: 31 status_t fInitStatus; 32 33 int fElementsCount; 34 int fSize; 35 addr_t* fBits; 36 37 static const int kBitsPerElement; 38 }; 39 40 41 status_t 42 Bitmap::GetInitStatus() 43 { 44 return fInitStatus; 45 } 46 47 48 bool 49 Bitmap::Get(int index) const 50 { 51 ASSERT(index < fSize); 52 53 const int kArrayElement = index / kBitsPerElement; 54 const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement); 55 return fBits[kArrayElement] & kBitMask; 56 } 57 58 59 void 60 Bitmap::Set(int index) 61 { 62 ASSERT(index < fSize); 63 64 const int kArrayElement = index / kBitsPerElement; 65 const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement); 66 fBits[kArrayElement] |= kBitMask; 67 } 68 69 70 void 71 Bitmap::Clear(int index) 72 { 73 ASSERT(index < fSize); 74 75 const int kArrayElement = index / kBitsPerElement; 76 const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement); 77 fBits[kArrayElement] &= ~addr_t(kBitMask); 78 } 79 80 81 #endif // KERNEL_UTIL_BITMAP_H 82 83