34#define BITMAP_BITS_TO_QWORDS(bits) (((bits) + 63) / 64)
42#define BITMAP_BITS_TO_BYTES(bits) (BITMAP_BITS_TO_QWORDS(bits) * sizeof(uint64_t))
50#define BITMAP_QWORDS_TO_BITS(qwords) ((qwords) * 64)
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)) \
62 uint64_t bit = __builtin_ctzll(tempQword); \
63 *(idx) = qwordIdx * 64 + bit; \
64 *(idx) < (map)->length; \
76 map->firstZeroIdx = 0;
97 return (
map->buffer[qwordIdx] & (1ULL << bitInQword));
115 map->buffer[qwordIdx] |= (1ULL << bitInQword);
132 for (
uint64_t i = low; i < high; i++)
150 if (length == 0 || align == 0 || maxIdx >
map->
length)
155 for (
uint64_t i =
map->firstZeroIdx; i < maxIdx; i += align)
160 for (; j < maxIdx; j++)
195 map->buffer[qwordIdx] &= ~(1ULL << bitInQword);
196 map->firstZeroIdx =
MIN(
map->firstZeroIdx, index);
213 for (
uint64_t i = low; i < high; i++)
217 map->buffer[qwordIdx] &= ~(1ULL << bitInQword);
219 map->firstZeroIdx =
MIN(
map->firstZeroIdx, low);
240 uint64_t firstBitInQword = low % 64;
242 uint64_t lastQwordIdx = (high - 1) / 64;
243 uint64_t lastBitInQword = (high - 1) % 64;
245 if (firstQwordIdx == lastQwordIdx)
248 uint64_t startMask = ~((1ULL << firstBitInQword) - 1);
249 uint64_t endMask = (1ULL << (lastBitInQword + 1)) - 1;
250 sum += __builtin_popcountll(qword & startMask & endMask);
254 if (firstBitInQword != 0)
257 uint64_t mask = ~((1ULL << firstBitInQword) - 1);
258 sum += __builtin_popcountll(qword & mask);
262 for (
uint64_t i = firstQwordIdx; i < lastQwordIdx; ++i)
264 sum += __builtin_popcountll(
map->buffer[i]);
267 if (lastBitInQword != 63)
270 uint64_t mask = (1ULL << (lastBitInQword + 1)) - 1;
271 sum += __builtin_popcountll(qword & mask);
297 uint64_t maskedQword = qword | ((1ULL << bitIdx) - 1);
298 if (maskedQword != ~0ULL)
300 return qwordIdx * 64 + __builtin_ctzll(~maskedQword);
305 for (
uint64_t i = qwordIdx; i < endQwordIdx; ++i)
307 if (
map->buffer[i] != ~0ULL)
309 return i * 64 + __builtin_ctzll(~
map->buffer[i]);
330 uint64_t startQwordIdx = startIdx / 64;
331 uint64_t startBitIdx = startIdx % 64;
334 while (startQwordIdx < endQwordIdx)
337 if (startBitIdx != 0)
339 qword &= ~((1ULL << startBitIdx) - 1);
344 return startQwordIdx * 64 + __builtin_ctzll(qword);
static uint64_t bitmap_find_first_clear(bitmap_t *map)
Find the first clear bit in the bitmap.
static void bitmap_clear(bitmap_t *map, uint64_t index)
Clear a bit in the bitmap.
static bool bitmap_is_set(bitmap_t *map, uint64_t idx)
Check if a bit is set in the bitmap.
static void bitmap_set(bitmap_t *map, uint64_t index)
Set a bit in the bitmap.
static void bitmap_init(bitmap_t *map, void *buffer, uint64_t length)
Initialize a bitmap.
#define BITMAP_BITS_TO_QWORDS(bits)
Convert number of bits to number of qwords.
static uint64_t bitmap_find_first_set(bitmap_t *map, uint64_t startIdx)
Find the first set bit in the bitmap.
static void bitmap_set_range(bitmap_t *map, uint64_t low, uint64_t high)
Set a range of bits in the bitmap.
static void bitmap_clear_range(bitmap_t *map, uint64_t low, uint64_t high)
Clear a range of bits in the bitmap.
static uint64_t bitmap_sum(bitmap_t *map, uint64_t low, uint64_t high)
Sum the number of set bits in a range.
static uint64_t bitmap_find_clear_region_and_set(bitmap_t *map, uint64_t length, uintptr_t maxIdx, uint64_t align)
Find a clear region of specified length and alignment, and set it.
#define ROUND_UP(number, multiple)
EFI_PHYSICAL_ADDRESS buffer
__UINTPTR_TYPE__ uintptr_t