Win32 Memory Management API Reference
Overview
This section provides detailed reference information for the Win32 API functions used for managing memory within Windows applications. Effective memory management is crucial for performance, stability, and security.
Understanding how to allocate, deallocate, protect, and map memory regions is fundamental for any Windows developer. The Win32 API offers a rich set of tools to control memory behavior at a granular level.
Key Concepts
- Virtual Memory: Windows uses a virtual memory system, providing each process with its own private virtual address space.
- Memory Allocation: Functions to reserve and commit memory pages from the virtual address space.
- Memory Protection: Mechanisms to control read, write, and execute permissions for memory regions.
- Memory Mapping: Techniques to map files or device memory into the process address space.
- Heap Management: Low-level functions for managing the heap within a process.
Core Functions
The following are the most commonly used Win32 API functions for memory management:
-
VirtualAllocReserves, commits, or changes the state of a region of pages in the virtual address space of the calling process.
-
VirtualAllocExReserves, commits, or changes the state of a region of pages in the virtual address space of a specified process.
-
VirtualFreeReleases, decommits, or releases and decommits a specified region of pages.
-
VirtualFreeExReleases, decommits, or releases and decommits a specified region of pages in the virtual address space of a specified process.
-
VirtualProtectChanges the protection on the specified region of committed pages of memory in the calling process.
-
VirtualProtectExChanges the protection on the specified region of committed pages of memory in a specified process.
-
VirtualQueryRetrieves detailed information about a region of pages within the virtual address space of the calling process.
-
HeapAllocAllocates a specified number of bytes from a specified heap.
-
HeapFreeFrees a specified allocated buffer in a specified heap and invalidates its handle.
-
CreateHeapCreates a new heap that can be used by the calling process.
-
GetProcessHeapRetrieves a handle to the process's default heap.
-
Mmap(Conceptual - often implemented viaCreateFileMappingandMapViewOfFile)Maps files or devices into the process address space.
Function Details
VirtualAlloc
LPVOID VirtualAlloc(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flAllocationType,
DWORD flProtect
);
Parameters:
lpAddress: The starting address of the region to allocate.dwSize: The size of the region, in bytes.flAllocationType: The type of memory allocation (e.g.,MEM_COMMIT | MEM_RESERVE).flProtect: The memory protection for the region (e.g.,PAGE_READWRITE).
Return Value: If the function succeeds, the return value is the base address of the allocated region. If the function fails, the return value is NULL.
VirtualAlloc must be freed with VirtualFree.
VirtualFree
BOOL VirtualFree(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD dwFreeType
);
Parameters:
lpAddress: A pointer to the starting address of the region of pages to be freed.dwSize: The size of the region, in bytes.dwFreeType: The type of free operation (e.g.,MEM_RELEASEorMEM_DECOMMIT).
Return Value: If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.
VirtualProtect
BOOL VirtualProtect(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flNewProtect,
PDWORD lpflOldProtect
);
Parameters:
lpAddress: A pointer to the base address of the region of pages to be changed.dwSize: The size, in bytes, of the region of pages to be changed.flNewProtect: The new memory protection for the region.lpflOldProtect: A pointer to a variable that receives the previous access protection value.
Return Value: If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE.
HeapAlloc
LPVOID HeapAlloc(
HANDLE hHeap,
DWORD dwFlags,
SIZE_T dwBytes
);
Parameters:
hHeap: A handle to the heap. The handle is returned by theCreateHeapfunction.dwFlags: The memory allocation attributes.dwBytes: The number of bytes to allocate from the specified heap.
Return Value: If the function succeeds, the return value is a pointer to the allocated buffer. If the function fails, the return value is NULL.
HeapAlloc and HeapFree for more localized memory management within a specific heap, often for frequently allocated small objects.