xref: /haiku/headers/private/kernel/util/Bitmap.h (revision 6f80a9801fedbe7355c4360bd204ba746ec3ec2d)
1 /*
2  * Copyright 2013-2022, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Paweł Dziepak, pdziepak@quarnos.org
7  *		Augustin Cavalier <waddlesplash>
8  */
9 #ifndef KERNEL_UTIL_BITMAP_H
10 #define KERNEL_UTIL_BITMAP_H
11 
12 
13 #ifdef _KERNEL_MODE
14 #	include <debug.h>
15 #else
16 #	include <Debug.h>
17 #endif
18 
19 #include <SupportDefs.h>
20 
21 namespace BKernel {
22 
23 class Bitmap {
24 public:
25 						Bitmap(size_t bitCount);
26 						~Bitmap();
27 
28 			status_t	InitCheck();
29 
30 			status_t	Resize(size_t bitCount);
31 			void		Shift(ssize_t bitCount);
32 
33 	inline	bool		Get(size_t index) const;
34 	inline	void		Set(size_t index);
35 	inline	void		Clear(size_t index);
36 
37 			ssize_t		GetHighestSet() const;
38 
39 private:
40 			size_t		fElementsCount;
41 			size_t		fSize;
42 			addr_t*		fBits;
43 
44 	static	const int	kBitsPerElement = (sizeof(addr_t) * 8);
45 };
46 
47 
48 bool
49 Bitmap::Get(size_t index) const
50 {
51 	ASSERT(index < fSize);
52 
53 	const size_t 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(size_t index)
61 {
62 	ASSERT(index < fSize);
63 
64 	const size_t 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(size_t index)
72 {
73 	ASSERT(index < fSize);
74 
75 	const size_t kArrayElement = index / kBitsPerElement;
76 	const addr_t kBitMask = addr_t(1) << (index % kBitsPerElement);
77 	fBits[kArrayElement] &= ~addr_t(kBitMask);
78 }
79 
80 } // namespace BKernel
81 
82 
83 using BKernel::Bitmap;
84 
85 
86 #endif	// KERNEL_UTIL_BITMAP_H
87 
88