[refactor] 整理注释
This commit is contained in:
+14
-14
@@ -42,7 +42,7 @@ static void heap_expand(UINTN min_size) {
|
||||
new_block->size = pages * PAGE_SIZE;
|
||||
new_block->next = NULL;
|
||||
|
||||
// Add to free list (sorted by address for coalescing)
|
||||
// 添加到空闲链表(按地址排序以便合并)
|
||||
struct heap_block** prev = &g_heap_free_list;
|
||||
while (*prev && (UINT8*)*prev < (UINT8*)new_block) {
|
||||
prev = &(*prev)->next;
|
||||
@@ -50,7 +50,7 @@ static void heap_expand(UINTN min_size) {
|
||||
new_block->next = *prev;
|
||||
*prev = new_block;
|
||||
|
||||
// Try to merge with the previous free block if adjacent
|
||||
// 尝试与前一个空闲块合并(如果相邻)
|
||||
if (prev != &g_heap_free_list) {
|
||||
struct heap_block* prev_block = g_heap_free_list;
|
||||
while (prev_block->next != new_block) {
|
||||
@@ -104,21 +104,21 @@ void* kmalloc(UINTN size) {
|
||||
while (*prev) {
|
||||
UINTN block_sz = BLOCK_SIZE(*prev);
|
||||
if (block_sz >= alloc_size) {
|
||||
// Found a suitable block
|
||||
// 找到合适的块
|
||||
struct heap_block* block = *prev;
|
||||
|
||||
// Split if remaining space is useful
|
||||
// 如果剩余空间足够则分割
|
||||
if (block_sz >= alloc_size + MIN_BLOCK_SIZE) {
|
||||
struct heap_block* split = (struct heap_block*)((UINT8*)block + alloc_size);
|
||||
split->size = block_sz - alloc_size;
|
||||
// Insert split into free list
|
||||
// 将分割后的块插入空闲链表
|
||||
split->next = block->next;
|
||||
block->size = alloc_size | 1;
|
||||
*prev = split;
|
||||
} else {
|
||||
// Use the whole block
|
||||
// 使用整个块
|
||||
*prev = block->next;
|
||||
block->size = block_sz | 1; // mark used
|
||||
block->size = block_sz | 1; // 标记为已使用
|
||||
}
|
||||
|
||||
if (size > 1024) {
|
||||
@@ -134,11 +134,11 @@ void* kmalloc(UINTN size) {
|
||||
prev = &(*prev)->next;
|
||||
}
|
||||
|
||||
// Out of memory in current heap — expand
|
||||
// 当前堆空间不足,扩展堆
|
||||
UINTN expand_size = alloc_size > PAGE_SIZE ? alloc_size : PAGE_SIZE;
|
||||
heap_expand(expand_size);
|
||||
|
||||
// Retry after expansion
|
||||
// 扩展后重试
|
||||
return kmalloc(size);
|
||||
}
|
||||
|
||||
@@ -151,14 +151,14 @@ void kfree(void* ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as free
|
||||
// 标记为空闲
|
||||
block->size &= ~(UINTN)1;
|
||||
|
||||
// Merge with next block if it's free
|
||||
// 与下一个空闲块合并
|
||||
struct heap_block* next = next_block(block);
|
||||
if ((UINT8*)next < (UINT8*)g_heap_end) {
|
||||
if (IS_FREE(next)) {
|
||||
// Remove next from free list and merge
|
||||
// 从空闲链表中移除 next 并合并
|
||||
block->size += next->size;
|
||||
struct heap_block** prev = &g_heap_free_list;
|
||||
while (*prev && *prev != next) {
|
||||
@@ -168,7 +168,7 @@ void kfree(void* ptr) {
|
||||
}
|
||||
}
|
||||
|
||||
// Insert block into free list
|
||||
// 将块插入空闲链表
|
||||
struct heap_block** prev = &g_heap_free_list;
|
||||
while (*prev && (UINT8*)*prev < (UINT8*)block) {
|
||||
prev = &(*prev)->next;
|
||||
@@ -201,7 +201,7 @@ void* krealloc(void* ptr, UINTN new_size) {
|
||||
UINTN old_size = BLOCK_SIZE(block) - HEADER_SIZE;
|
||||
|
||||
if (old_size >= new_size) {
|
||||
// Can we split the shrinkage?
|
||||
// 能否分割缩小的部分?
|
||||
UINTN shrink = old_size - new_size;
|
||||
if (shrink >= MIN_BLOCK_SIZE) {
|
||||
block->size = (new_size + HEADER_SIZE) | 1;
|
||||
|
||||
+11
-11
@@ -19,7 +19,7 @@ static inline BOOLEAN bitmap_test(UINTN idx) {
|
||||
return (g_pmm.bitmap[idx / 8] >> (idx % 8)) & 1;
|
||||
}
|
||||
|
||||
// Clean stale entries from free list head
|
||||
// 清理空闲链表头部的过期条目
|
||||
static void clean_free_list() {
|
||||
while (g_pmm.free_list_head != NULL &&
|
||||
bitmap_test((UINTN)g_pmm.free_list_head / PAGE_SIZE)) {
|
||||
@@ -58,7 +58,7 @@ EFI_STATUS pmm_init() {
|
||||
|
||||
UINTN entry_count = map_size / desc_size;
|
||||
|
||||
// First pass: count total pages and find max physical address
|
||||
// 第一遍:统计总页数并找到最大物理地址
|
||||
UINT64 max_addr = 0;
|
||||
UINT64 total_free = 0;
|
||||
for (UINTN i = 0; i < entry_count; i++) {
|
||||
@@ -73,16 +73,16 @@ EFI_STATUS pmm_init() {
|
||||
g_pmm.base_addr = 0;
|
||||
g_pmm.max_addr = max_addr;
|
||||
|
||||
// How many pages does the bitmap cover?
|
||||
// 位图覆盖的页数
|
||||
UINTN total_pages = (UINTN)(max_addr / PAGE_SIZE);
|
||||
g_pmm.total_pages = total_pages;
|
||||
|
||||
// Bitmap size in bytes, rounded up to page boundary
|
||||
// 位图大小(字节),向上取整到页边界
|
||||
g_pmm.bitmap_size = ((total_pages + 7) / 8);
|
||||
UINTN bitmap_pages = (g_pmm.bitmap_size + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||
g_pmm.bitmap_size = bitmap_pages * PAGE_SIZE; // round to full pages
|
||||
|
||||
// Place bitmap at the end of the highest free conventional memory region
|
||||
// 将位图放在最高空闲常规内存区域的末尾
|
||||
UINT64 bitmap_addr = 0;
|
||||
for (UINTN i = 0; i < entry_count; i++) {
|
||||
EFI_MEMORY_DESCRIPTOR* desc = (EFI_MEMORY_DESCRIPTOR*)((UINT8*)mem_map + i * desc_size);
|
||||
@@ -105,12 +105,12 @@ EFI_STATUS pmm_init() {
|
||||
|
||||
g_pmm.bitmap = (UINT8*)(UINTN)bitmap_addr;
|
||||
|
||||
// Init bitmap: mark ALL pages as used (0xFF)
|
||||
// 初始化位图:将所有页标记为已使用
|
||||
for (UINTN i = 0; i < g_pmm.bitmap_size; i++) {
|
||||
g_pmm.bitmap[i] = 0xFF;
|
||||
}
|
||||
|
||||
// Mark free pages (EfiConventionalMemory) as free in bitmap
|
||||
// 将空闲页(EfiConventionalMemory)在位图中标记为空闲
|
||||
g_pmm.free_pages = 0;
|
||||
UINT64 bm_start_page = bitmap_addr / PAGE_SIZE;
|
||||
UINT64 bm_end_page = (bitmap_addr + g_pmm.bitmap_size + PAGE_SIZE - 1) / PAGE_SIZE;
|
||||
@@ -123,19 +123,19 @@ EFI_STATUS pmm_init() {
|
||||
UINT64 end_page = start_page + desc->NumberOfPages;
|
||||
|
||||
for (UINT64 p = start_page; p < end_page; p++) {
|
||||
// Skip bitmap pages
|
||||
// 跳过位图占用的页
|
||||
if (p >= bm_start_page && p < bm_end_page) continue;
|
||||
bitmap_clear((UINTN)p);
|
||||
g_pmm.free_pages++;
|
||||
}
|
||||
}
|
||||
|
||||
// Mark bitmap pages as used
|
||||
// 将位图占用的页标记为已使用
|
||||
for (UINT64 p = bm_start_page; p < bm_end_page; p++) {
|
||||
bitmap_set((UINTN)p);
|
||||
}
|
||||
|
||||
// Reserve low memory (first 4 MB) — UEFI firmware may use it during BS calls
|
||||
// 保留低内存(前 4MB)— 固件可能在 Boot Services 调用期间使用
|
||||
UINT64 low_reserve_pages = 0x400;
|
||||
for (UINT64 p = 0; p < low_reserve_pages && p < g_pmm.total_pages; p++) {
|
||||
if (!bitmap_test((UINTN)p)) {
|
||||
@@ -144,7 +144,7 @@ EFI_STATUS pmm_init() {
|
||||
}
|
||||
}
|
||||
|
||||
// Build free list by linking free pages
|
||||
// 通过链接空闲页构建空闲链表
|
||||
g_pmm.free_list_head = NULL;
|
||||
void* prev = NULL;
|
||||
for (UINTN i = 0; i < entry_count; i++) {
|
||||
|
||||
Reference in New Issue
Block a user