26 uint64_t highest_address = 0;
28 for (
int i = 0; i < memmap_response->entry_count; i++)
30 if (memmap_response->entries[i]->type != LIMINE_MEMMAP_USABLE)
continue;
32 uint64_t end = memmap_response->entries[i]->base +
33 memmap_response->entries[i]->length;
35 if (end > highest_address)
36 highest_address = end;
39 if (highest_address == 0)
40 PANIC(
"No usable memory found");
42 state.frame_count = highest_address / FRAME_SIZE;
43 uint64_t bitmap_bytes = (state.frame_count + 7) / 8;
44 uint64_t bitmap_frames = (bitmap_bytes + (FRAME_SIZE - 1)) / FRAME_SIZE;
49 for (
int i = 0; i < memmap_response->entry_count; i++)
53 if (entry->type != LIMINE_MEMMAP_USABLE ||
54 entry->base < 0x100000 ||
55 entry->length < bitmap_bytes)
58 first_usable_entry = entry;
62 if (first_usable_entry == NULL)
63 PANIC(
"No usable memory found");
65 uint64_t bitmap_phys_addr = first_usable_entry->base;
68 for (uint64_t i = 0; i < bitmap_frames * (FRAME_SIZE / 8); i++)
70 state.bitmap[i] = 0xFFFFFFFFFFFFFFFF;
73 state.free_frames = 0;
75 for (uint64_t i = 0; i < memmap_response->entry_count; i++)
79 if (entry->type != LIMINE_MEMMAP_USABLE)
82 for (uint64_t j = 0; j < entry->length / FRAME_SIZE; j++)
84 if (entry->base + (j * FRAME_SIZE) < 0x100000)
continue;
86 uint64_t frame = (entry->base + (j * FRAME_SIZE)) / FRAME_SIZE;
87 state.bitmap[frame / 64] &= ~(1ULL << (frame % 64));
94 for (uint64_t i = 0; i < bitmap_frames; i++)
99 uint64_t frame = (bitmap_phys_addr + (i * FRAME_SIZE)) / FRAME_SIZE;
100 state.bitmap[frame / 64] |= 1ULL << (frame % 64);
108 LOG_DEBUG(
"Total Memory: %lluMiB\n", state.frame_count * FRAME_SIZE / 1024 / 1024);
114 for (uint64_t i = 0; i < (state.frame_count + 63) / 64; i++)
116 if (state.next_hint == (state.frame_count + 63) / 64) state.next_hint = 0;
118 if (state.bitmap[state.next_hint] != 0xFFFFFFFFFFFFFFFF)
120 uint64_t bit = __builtin_ffsll(~state.bitmap[state.next_hint]);
121 state.bitmap[state.next_hint] |= 1ULL << (bit - 1);
123 return (state.next_hint * 64 + (bit - 1)) * FRAME_SIZE;
128 if (state.next_hint == (state.frame_count + 63) / 64) state.next_hint = 0;
136 if (count == 0)
return 0;
140 uint64_t contiguous_hint = 0;
141 uint64_t contiguous_bit = 0;
143 uint64_t t_next_hint = state.next_hint;
145 for (uint64_t i = 0; i < (state.frame_count + 63) / 64; i++)
147 if (state.bitmap[t_next_hint] != 0xFFFFFFFFFFFFFFFF)
150 for (
long bit = 0; bit < 64; bit++)
152 if (frames == 0) contiguous_hint = t_next_hint;
154 uint8_t bit_value = (state.bitmap[t_next_hint] >> bit) & 1;
161 if (frames == 0) contiguous_bit = bit;
163 if (frames >= count)
break;
169 long current_bit = contiguous_bit;
170 uint64_t current_hint = contiguous_hint;
171 for (uint64_t j = 0; j < count; j++)
173 if (current_bit >= 64)
179 state.bitmap[current_hint] |= 1ULL << (current_bit);
183 state.free_frames -= count;
184 state.next_hint = t_next_hint;
185 return (contiguous_hint * 64 + (contiguous_bit)) * FRAME_SIZE;
194 if (t_next_hint == (state.frame_count + 63) / 64)
229 if (physical_addr % FRAME_SIZE != 0)
230 PANIC(
"Invalid physical address: %lx", physical_addr);
232 uint64_t frame = physical_addr / FRAME_SIZE;
234 if (frame >= state.frame_count)
235 PANIC(
"Invalid frame number %lu. Address: %lx", frame, physical_addr);
237 uint64_t word_index = frame / 64;
239 if (((state.bitmap[word_index] >> (frame % 64)) & 1) == 1)
242 state.bitmap[word_index] = ~(~state.bitmap[word_index] | (1ULL << (frame % 64)));
245 PANIC(
"Double free: %lx", physical_addr);
250 if (word_index < state.next_hint)
251 state.next_hint = word_index;