UEFI EDKII 記憶體的分配和釋放細節

2021-09-02 14:06:19 字數 1637 閱讀 6772

記憶體的分配和釋放作為一種

該函式實現了以位元組為單位的儲存空間分配,edkii 中對這種非頁面的儲存空間使用了如下資料結構

來管理。

//

// each element is the sum of the 2 previous ones: this allows us to migrate

// blocks between bins by splitting them up, while not wasting too much memory

// as we would in a strict power-of-2 sequence

//static const uint16 mpoolsizetable = ;

相同大小的pool 都被存放在同乙個鍊錶中

處理了請求分配大小超過freelist 容量的情況,對於這種情況,直接呼叫page.c 檔案中的coreallocatepoolpagesi

函式去以頁面為單位分配。

將新新分配的頁面拆分成所需要的大小,並將這些可分配pool 加入到freelist 陣列中去,拆分後剩餘的

空間「邊角料」也沒有浪費,而是被加入到更小單元的freelist 陣列中去

//

// carve up remaining space into free pool blocks

//index--;

while (offset < maxoffset)

index -= 1;

}

index -= 1  

先嘗試按最大的pool 來匹配,一直到嘗試能不能匹配上128位元組的記憶體單元。

//

// get the head & tail of the pool entry

//head = base_cr (buffer, pool_head, data);

assert(head != null);

if (head->signature != pool_head_signature &&

head->signature != poolpage_head_signature)

該函式與allocatepool 相對應,將已經分配的pool 釋放以便可以重新利用這些儲存空間。函式

的一開始做了些pool 結構的檢查工作,只有確實是通過allocatepool 得到的空間才可以使用

freepool來釋放。

同樣,對於大尺寸的pool, 我們直接呼叫corefreepoolpage 來釋放相關頁面。對於通常的

pool , 將這些空間重新加入到freelist 中去。

//

// put the pool entry onto the free pool list

//free = (pool_free *) head;

assert(free != null);

free->signature = pool_free_signature;

free->index = (uint32)index;

insertheadlist (&pool->freelist[index], &free->link);

記憶體的堆分配和棧分配

記憶體的堆分配和棧分配 備註 這一部分非常重要,如果錯誤請及時告知。謝謝 這裡是完全參考其他部落格。c 記憶體模型 1 棧區 由編譯器自動分配和釋放,存放函式的引數數值,區域性變數的值 其操作方式類似於資料結構中的棧 2 堆區 一般由使用者分配和釋放,若使用者不釋放,程式結束時候由os 它與資料結構...

記憶體的堆分配和棧分配

c 記憶體模型 1 棧區 由編譯器自動分配和釋放,存放函式的引數數值,區域性變數的值 其操作方式類似於資料結構中得棧 2 堆區 一般由使用者分配和釋放,若使用者不釋放,程式結束時候由os 它與資料結構中堆是兩回事,分配方式類似鍊錶 3 全域性區 靜態區 static 全域性變數和靜態變數的存放區域。...

靜態記憶體分配和 動態記憶體分配

1 靜態記憶體分配是在編譯時完成的,不需要占用cpu資源 動態分配記憶體是在執行時完成的,動態記憶體的分配與釋放需要占用cpu資源 2 靜態記憶體分配是在棧上分配的,動態記憶體是堆上分配的 3 動態記憶體分配需要指標或引用資料型別的支援,而靜態記憶體分配不需要 4 靜態分配記憶體需要在編譯前確定記憶...