[refactor] 整理注释

This commit is contained in:
2026-06-06 10:31:20 +08:00
Unverified
parent a9ba4457c6
commit 30d48d2881
19 changed files with 177 additions and 192 deletions
+14 -14
View File
@@ -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;