PatchworkOS  c9fea19
A non-POSIX operating system.
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1#ifndef _SYS_BITMAP_H
2#define _SYS_BITMAP_H 1
3
4#include <errno.h>
5#include <stdbool.h>
6#include <stdint.h>
7#include <sys/math.h>
8
9/**
10 * @brief A bitmap optimized using 64-bit words.
11 * @defgroup libstd_sys_bitmap Bitmap
12 * @ingroup libstd
13 *
14 * @{
15 */
16
17/**
18 * @brief Bitmap structure.
19 * @struct bitmap_t
20 */
27
28/**
29 * @brief Convert number of bits to number of qwords.
30 *
31 * @param bits Number of bits.
32 * @return Number of qwords.
33 */
34#define BITMAP_BITS_TO_QWORDS(bits) (((bits) + 63) / 64)
35
36/**
37 * @brief Convert number of bits to number of bytes.
38 *
39 * @param bits Number of bits.
40 * @return Number of bytes.
41 */
42#define BITMAP_BITS_TO_BYTES(bits) (BITMAP_BITS_TO_QWORDS(bits) * sizeof(uint64_t))
43
44/**
45 * @brief Convert number of qwords to number of bits.
46 *
47 * @param qwords Number of qwords.
48 * @return Number of bits.
49 */
50#define BITMAP_QWORDS_TO_BITS(qwords) ((qwords) * 64)
51
52/**
53 * @brief Iterate over each set bit in the bitmap.
54 *
55 * @param idx Pointer to store the current index.
56 * @param map The bitmap.
57 */
58#define BITMAP_FOR_EACH_SET(idx, map) \
59 for (uint64_t qwordIdx = 0; qwordIdx < BITMAP_BITS_TO_QWORDS((map)->length); qwordIdx++) \
60 for (uint64_t tempQword = (map)->buffer[qwordIdx]; tempQword != 0; tempQword &= (tempQword - 1)) \
61 if (({ \
62 uint64_t bit = __builtin_ctzll(tempQword); \
63 *(idx) = qwordIdx * 64 + bit; \
64 *(idx) < (map)->length; \
65 }))
66
67/**
68 * @brief Define and create a bitmap and its buffer.
69 *
70 * @param name Name of the bitmap.
71 * @param bits Length of the bitmap in bits.
72 */
73#define BITMAP_CREATE(name, bits) \
74 uint64_t name##Buffer[BITMAP_BITS_TO_QWORDS(bits)]; \
75 bitmap_t name = {.firstZeroIdx = 0, .length = (bits), .buffer = name##Buffer}
76
77/**
78 * @brief Define and create a zero-initialized bitmap and its buffer.
79 *
80 * @param name Name of the bitmap.
81 * @param bits Length of the bitmap in bits.
82 */
83#define BITMAP_CREATE_ZERO(name, bits) \
84 uint64_t name##Buffer[BITMAP_BITS_TO_QWORDS(bits)] = {0}; \
85 bitmap_t name = {.firstZeroIdx = 0, .length = (bits), .buffer = name##Buffer}
86
87/**
88 * @brief Define a bitmap and its buffer.
89 *
90 * Will not initialize the bitmap, use `BITMAP_DEFINE_INIT` to initialize it.
91 *
92 * Intended to be used for struct members.
93 *
94 * @param name Name of the bitmap.
95 * @param bits Length of the bitmap in bits.
96 */
97#define BITMAP_DEFINE(name, bits) \
98 uint64_t name##Buffer[BITMAP_BITS_TO_QWORDS(bits)]; \
99 bitmap_t name
100
101/**
102 * @brief Initialize a bitmap defined with `BITMAP_DEFINE`.
103 *
104 * @param name Name of the bitmap.
105 * @param bits Length of the bitmap in bits.
106 */
107#define BITMAP_DEFINE_INIT(name, bits) bitmap_init(&(name), name##Buffer, bits);
108
109/**
110 * @brief Initialize a bitmap.
111 *
112 * @param map The bitmap.
113 * @param buffer Pointer to the buffer, must be a multiple of 8 bytes.
114 * @param length Length of the bitmap in bits.
115 */
116void bitmap_init(bitmap_t* map, void* buffer, uint64_t length);
117
118/**
119 * @brief Check if a bit is set in the bitmap.
120 *
121 * @param map The bitmap.
122 * @param idx Index of the bit to check.
123 * @return true if the bit is set, false otherwise.
124 */
126
127/**
128 * @brief Set a bit in the bitmap.
129 *
130 * @param map Pointer to the bitmap.
131 * @param index Index of the bit to set.
132 */
133void bitmap_set(bitmap_t* map, uint64_t index);
134
135/**
136 * @brief Set a range of bits in the bitmap.
137 *
138 * @param map Pointer to the bitmap.
139 * @param low Low index of the range (inclusive).
140 * @param high High index of the range (exclusive).
141 */
143
144/**
145 * @brief Clear a bit in the bitmap.
146 *
147 * @param map Pointer to the bitmap.
148 * @param index Index of the bit to clear.
149 */
150void bitmap_clear(bitmap_t* map, uint64_t index);
151
152/**
153 * @brief Clear a range of bits in the bitmap.
154 *
155 * @param map Pointer to the bitmap.
156 * @param low Low index of the range (inclusive).
157 * @param high High index of the range (exclusive).
158 */
160
161/**
162 * @brief Find the first clear bit in the bitmap.
163 *
164 * @param map The bitmap.
165 * @param startIdx Index to start searching from.
166 * @param endIdx Index to stop searching at (exclusive).
167 * @return Index of the first clear bit, or `map->length` if none found.
168 */
170
171/**
172 * @brief Find the first set bit in the bitmap.
173 *
174 * @param map The bitmap.
175 * @param startIdx Index to start searching from.
176 * @param endIdx Index to stop searching at (exclusive).
177 * @return Index of the first set bit, or `map->length` if none found.
178 */
180
181/**
182 * @brief Find a clear region of specified length and alignment, and set it.
183 *
184 * @param map Pointer to the bitmap.
185 * @param minIdx Minimum index to start searching from.
186 * @param maxIdx Maximum index to search up to.
187 * @param length Length of the region to find.
188 * @param alignment Alignment of the region.
189 * @return Starting index of the found region, or `map->length` if not found.
190 */
192 uint64_t alignment);
193
194/** @} */
195
196#endif // _SYS_BITMAP_H
void bitmap_clear_range(bitmap_t *map, uint64_t low, uint64_t high)
Clear a range of bits in the bitmap.
bool bitmap_is_set(bitmap_t *map, uint64_t idx)
Check if a bit is set in the bitmap.
uint64_t bitmap_find_first_set(bitmap_t *map, uint64_t startIdx, uint64_t endIdx)
Find the first set bit in the bitmap.
void bitmap_init(bitmap_t *map, void *buffer, uint64_t length)
Initialize a bitmap.
Definition bitmap_init.c:3
void bitmap_set_range(bitmap_t *map, uint64_t low, uint64_t high)
Set a range of bits in the bitmap.
void bitmap_clear(bitmap_t *map, uint64_t index)
Clear a bit in the bitmap.
Definition bitmap_clear.c:3
void bitmap_set(bitmap_t *map, uint64_t index)
Set a bit in the bitmap.
Definition bitmap_set.c:3
uint64_t bitmap_find_first_clear(bitmap_t *map, uint64_t startIdx, uint64_t endIdx)
Find the first clear bit in the bitmap.
uint64_t bitmap_find_clear_region_and_set(bitmap_t *map, uint64_t minIdx, uintptr_t maxIdx, uint64_t length, uint64_t alignment)
Find a clear region of specified length and alignment, and set it.
boot_memory_map_t * map
Definition mem.c:19
EFI_PHYSICAL_ADDRESS buffer
Definition mem.c:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
Bitmap structure.
Definition bitmap.h:22
uint64_t * buffer
Definition bitmap.h:25
uint64_t length
Definition bitmap.h:24
uint64_t firstZeroIdx
Definition bitmap.h:23