Win內存分配函數(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc)

內存分配函數/內存管理API

參考:

Windows MSDN

http://msdn.microsoft.com/en-us/library/aa908768.aspx

 

附助資料:

http://blog.csdn.net/susubuhui/article/details/7315094

http://wenku.baidu.com/link?url=yxgCWePPV1kFaIUciEspYgm34wNAnMLDoduBlfsEEo-mW0JFRVEOkixomUjPatqw_jOXZcqQ1CLoeBSZqLuse1KiyHD6ysZTMIzLy_sPgPS

http://blog.csdn.net/sharecode/article/details/7464915

 

Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu  轉載請標明來源 

 

 

 

對於Windows來說,提供了一些API接口進行Heap內存管理,是獨立於C++/C程序之外的,僅用於Windows平臺的API

 

大概分爲下面幾組接口:

 

老的Heap接口GlobalXXX(新程序不建議使用,這些函數存在主要是兼容以前寫的程序):

GlobalAlloc/GlobalRelloc/GlobalFree:Heap申請一塊內存

GlobalLock/GlobalUnlock: GlobalAlloc的內存中申請一段內存

函數參數:

 

 

新的Heap接口HeapXXX:

HeapCreate/HeapDestroy/GetProcessHeap:Heap中申請一塊內存

HeapAlloc/HeapRelloc/HeapFree/HeapSize:HeapCreate的內存中申請一段內存

HeapValidatee:查詢HeapAlloc的信息

 

當前進程Heap內存接口LocalXXX:

LocalAlloc/LocalReAlloc/LocalFree/LocalSize:從當前Heap的內存中申請一段內存,相當於從HeapAllocGetProcessHeap中申請內存。

 

虛擬地址內存接口: VirtualXXX

VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:從進程堆中申請頁面內存數據。通常用於申請一段大內存,最少申請一頁。reserves or commits a region of pages

關於這一塊詳細請參考:http://blog.csdn.net/sharecode/article/details/7464915

 

malloc,new,VirtualAlloc,HeapAlloc這幾組的執行效率:

參考:http://blog.csdn.net/susubuhui/article/details/7315094

分配大內存的時候 VirtualAlloc才能體現出優勢,申請小內存時比較慢。

至於小塊內存那就是HeapAlloc > malloc > new,因爲都是調用HeapAlloc,直接使用HeapAlloc要快很多。

 

擴展的新接口xxxEx(不是所有版本都支持)

Ex結尾,例如VitualAllocEx,可以指定process handle,從而從其它運行進程空間申請一段內存。

 

函數說明:

HeapCreate:

從內存區申請一段內存作爲堆。This function reserves memory from the shared memory area.

HeapDestroy:

刪除HeapCreate創建的堆。This function destroys the specified heap object.

GetProcessHeap:

獲取當前進程的堆Handle。注意這個不能使用Destroy刪除。This function obtains a handle to the heap of the calling process. This handle can then be used in calls to the HeapAlloc,HeapReAlloc,HeapFree, andHeapSize functions.

 

HeapAlloc:

從指定堆申請一段內存,該段內存是不被移動的。This function allocates a block of memory from a heap. The allocated memory is not movable.

HeapReAlloc:

從指定堆重新申請一段內存。This function reallocates a block of memory from a heap. The allocated memory is not movable.

HeapFree:

釋入HeapAllocHeapReAlloc申請的內存。This function frees a memory block from a heap. The memory block was allocated by the HeapAlloc or theHeapReAlloc function.

HeapSize:

根據Handle、內存開始地址,查詢一段HeapAlloc內存的大小。This function returns the size, in bytes, of a memory block allocated from a heap by the HeapAlloc orHeapReAlloc function.

 

HeapValidate:

檢查Heap HandleHeap Handle下申請的內存是否有效。

This function validates the specified heap. HeapValidate scans all the memory blocks in the heap and verifies that the heap control structures maintained by the heap manager are in a consistent state. The HeapValidate function can also be used to validate a single memory block within a specified heap without checking the validity of the entire heap.

 

LocalAlloc/LocalReAlloc/LocalFree/LocalSize:

從當前進程Heap中進行操作。該組函數相當於HeapXXX指定Heap Handle爲當前進程Handle。函數參考HeapAlloc / HeapReAlloc/ HeapFree/ HeapSize作用。

LocalAlloc : This function allocates the specified number of bytes from the heap.

In the linear Windows Embedded CE API environment, there is no difference between the local heap and the global heap.LocalAlloc is equivalent to HeapAlloc(GetProcessHeap, …).

 

VirtualAlloc/VirtualFree/ VirtualProtect/VirtualQuery:

從當前進程空間中申請頁面空間數據,最少申請一頁。

The VirtualAlloc function reserves or commits a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero, unless MEM_RESET is specified.

To allocate memory in the address space of another process, use theVirtualAllocEx function.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章