The main memory management path under Windows platform

Source: Internet
Author: User

New/delete
Malloc/free
Cotaskmemalloc/cotaskmemfree
Imalloc::alloc/imalloc/free
Globalalloc/globalfree
Localalloc/localfree
Heapalloc/heapfree
Virtualalloc/virtualfree

VirtualAlloc is allocated as a page (4K), which is the underlying function for manipulating virtual memory.

HeapAlloc allocates memory on the specified heap.
Windows sets a default heap for each process, and the application can create more heaps with heapcreate.

GlobalAlloc and LocalAlloc, which were originally 16-bit environments, retain both functions for compatibility, but no global heap exists under WIN32 (all of the heaps are process-private), so GlobalAlloc and LocalAlloc The meanings are identical under Win32, and they are all memory allocations in the process default heap. It is important to note that, under Win32, the literal meaning of GlobalAlloc is invalidated, and it does not share data between processes. Microsoft stressed that Globalalloc/localalloc is slower than HeapAlloc and is no longer recommended, but because Globalalloc/localalloc has a simple interface, they are still heavily used even in the source code provided by Microsoft.

malloc is a CRT function, and the way it is implemented depends on the specific CRT version. The malloc system of VC + + makes simple packing for HeapAlloc, while Borland C + + chooses itself to achieve malloc. When the application starts, the CRT creates its own private heap, which resides on top of the Win32 heap.

New in C + + calls operator new before invoking the constructor to generate the class object. operator new, by default, calls malloc for memory allocation. The application can reload operator new and choose a different memory allocation method.

CoTaskMemAlloc is used for COM objects, which allocate memory in the process's default heap.
The IMalloc interface is the re-encapsulation of the cotaskmemalloc/cotaskmemfree.

Call Relationship:
Msvcrt.malloc = kernel32. HeapAlloc (ntdll. RTLALLOCATEHEAP)
Kernel32. LocalAlloc = Ntdll. Rtlallocateheap
Kernel32. Globlealloc = Ntdll. Rtlallocateheap
Kernel32. HeapAlloc = = Ntdll. Rtlallocateheap (map)
Kernel32. VirtualAlloc = kernel32. VirtualAllocEx
Kernel32. VirtualAllocEx = Ntdll. Ntallocatevirtualmemory
Ntdll. Rtlallocateheap = Ntdll. Ntallocatevirtualmemory
Ntdll. Ntallocatevirtualmemory = Ntdll. Kifastsystemcall
Ntdll. Kifastsystemcall = sysenter directive (0F34)
That
Driver _pagealloc, VirtualAlloc, HeapAlloc, malloc, new

About when to use Cotaskmemalloc/cotaskmemfree to request/free memory

From the online view of the saying:
The thing is this COM (actually the RPC Runtime library, but it's easier to blame COM) have a additional requirement for [  Out] pointers. This requirement is if the type of an [out] parameter isn ' t a scalar quantity (in other words if it's a structure or Anything more complicated than a int or float) and then the memory pointed to by the [off] parameter needs to be allocated EI Ther by midl_user_allocate (for RPC) or CoTaskMemAlloc (for COM).

That is, if you need to call the COM interface, and the interface has [out] parameters, and the type is not a basic data type such as shaping or floating-point numbers, you need to call CoTaskMemAlloc to request this memory, and then call CoTaskMemFree to release after use is complete.

http://blog.csdn.net/jiangxinyu/article/details/7792216

The main memory management path under Windows platform

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.