[置顶] Win内存分配函数(GlobalAlloc/HeapAlloc/LocalAlloc/VirtualAlloc)

时间:2021-12-25 03:17:59

内存分配函数/内存管理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.