PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
bitmap_find_first_set.c
Go to the documentation of this file.
1#include <sys/bitmap.h>
2
4{
5 if (startIdx >= map->length)
6 {
7 return map->length;
8 }
9
10 uint64_t startQwordIdx = startIdx / 64;
11 uint64_t startBitIdx = startIdx % 64;
12 uint64_t endQwordIdx = BITMAP_BITS_TO_QWORDS(MIN(endIdx, map->length));
13
14 while (startQwordIdx < endQwordIdx)
15 {
16 uint64_t qword = map->buffer[startQwordIdx];
17 if (startBitIdx != 0)
18 {
19 qword &= ~((1ULL << startBitIdx) - 1);
20 }
21
22 if (qword != 0)
23 {
24 return startQwordIdx * 64 + __builtin_ctzll(qword);
25 }
26
27 startQwordIdx++;
28 startBitIdx = 0;
29 }
30
31 return map->length;
32}
uint64_t bitmap_find_first_set(bitmap_t *map, uint64_t startIdx, uint64_t endIdx)
Find the first set bit in the bitmap.
#define BITMAP_BITS_TO_QWORDS(bits)
Convert number of bits to number of qwords.
Definition bitmap.h:34
#define MIN(x, y)
Definition math.h:16
boot_memory_map_t * map
Definition mem.c:19
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
Bitmap structure.
Definition bitmap.h:22
uint64_t length
Definition boot_info.h:58