| 1 | /* @title: Bitmap allocator */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <mem/alloc.h> |
| 4 | #include <mem/page.h> |
| 5 | #include <mem/vmm.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <stdint.h> |
| 8 | #include <types/types.h> |
| 9 | |
| 10 | #define BOOT_BITMAP_SIZE ((1024 * 1024 * 128) / PAGE_SIZE / 8) |
| 11 | |
| 12 | extern uint8_t boot_bitmap[BOOT_BITMAP_SIZE]; |
| 13 | extern uint8_t *bitmap; |
| 14 | extern uint64_t bitmap_size; |
| 15 | |
| 16 | paddr_t bitmap_alloc_pages(uint64_t count, enum alloc_flags f); |
| 17 | void bitmap_free_pages(paddr_t addr, uint64_t count); |
| 18 | |
| 19 | __no_sanitize_address static inline void set_bit(uint64_t index) { |
| 20 | uint64_t byte = index / 8; |
| 21 | uint8_t mask = 1 << (index % 8); |
| 22 | if (byte > BOOT_BITMAP_SIZE) |
| 23 | return; |
| 24 | |
| 25 | __atomic_fetch_or(&bitmap[byte], mask, __ATOMIC_SEQ_CST); |
| 26 | } |
| 27 | |
| 28 | __no_sanitize_address static inline void clear_bit(uint64_t index) { |
| 29 | uint64_t byte = index / 8; |
| 30 | uint8_t mask = ~(1 << (index % 8)); |
| 31 | if (byte > BOOT_BITMAP_SIZE) |
| 32 | return; |
| 33 | |
| 34 | __atomic_fetch_and(&bitmap[byte], mask, __ATOMIC_SEQ_CST); |
| 35 | } |
| 36 | |
| 37 | __no_sanitize_address static inline bool test_bit(uint64_t index) { |
| 38 | uint64_t byte = index / 8; |
| 39 | uint8_t value; |
| 40 | if (byte > BOOT_BITMAP_SIZE) |
| 41 | return false; |
| 42 | |
| 43 | __atomic_load(&bitmap[byte], &value, __ATOMIC_SEQ_CST); |
| 44 | return (value & (1 << (index % 8))) != 0; |
| 45 | } |
| 46 |