PatchworkOS  321f6ec
A non-POSIX operating system.
Loading...
Searching...
No Matches
bitmap_find_first_clear.c
Go to the documentation of this file.
1#include <sys/bitmap.h>
2
4{
5 if (map->firstZeroIdx >= map->length)
6 {
7 return map->length;
8 }
9
10 startIdx = MAX(startIdx, map->firstZeroIdx);
11 uint64_t qwordIdx = startIdx / 64;
12 uint64_t bitIdx = startIdx % 64;
13 uint64_t endQwordIdx = BITMAP_BITS_TO_QWORDS(MIN(endIdx, map->length));
14
15 if (bitIdx != 0)
16 {
17 uint64_t qword = map->buffer[qwordIdx];
18 uint64_t maskedQword = qword | ((1ULL << bitIdx) - 1);
19 if (maskedQword != ~0ULL)
20 {
21 return qwordIdx * 64 + __builtin_ctzll(~maskedQword);
22 }
23 qwordIdx++;
24 }
25
26 for (uint64_t i = qwordIdx; i < endQwordIdx; ++i)
27 {
28 if (map->buffer[i] != ~0ULL)
29 {
30 return i * 64 + __builtin_ctzll(~map->buffer[i]);
31 }
32 }
33
34 return map->length;
35}
uint64_t bitmap_find_first_clear(bitmap_t *map, uint64_t startIdx, uint64_t endIdx)
Find the first clear 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
#define MAX(x, y)
Definition math.h:15
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