Memory Management
This section covers the Windows API functions and concepts related to managing memory in both kernel and user mode. Efficient memory management is crucial for application performance and system stability.
Core Memory Allocation and Deallocation
VirtualAlloc
Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process.
Parameters:
lpAddress- The base address of the region of pages to be allocated. If this parameter is
NULL, the system determines where to allocate the region. dwSize- The size, in bytes, of the region of pages to be allocated. If
lpAddressisNULL, this parameter is ignored and the function allocates a page large enough to hold all data. flAllocationType- The type of memory allocation. This parameter can be one or more of the following values:
MEM_COMMIT,MEM_RESERVE,MEM_RESET,MEM_RESET_UNDO. flProtect- The memory protection for the region of pages to be allocated. For a list of values, see Memory Protection Constants.
If the function succeeds, the return value is the base address of the allocated region of pages. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
VirtualFree
Commits, decommits, or releases a region of pages in the virtual address space of the calling process.
Parameters:
lpAddress- A pointer to the base address of the region of pages to be freed.
dwSize- The size, in bytes, of the region of pages to be freed. This parameter must be equal to 0 if the
dwFreeTypeparameter specifiesMEM_RELEASE. dwFreeType- The type of operation. This parameter can be one of the following values:
MEM_DECOMMITorMEM_RELEASE.
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
HeapAlloc
Allocates a block of memory from a specified heap.
Parameters:
hHeap- A handle to the heap from which the memory will be allocated. This handle is returned by the
HeapCreatefunction. dwFlags- The allocation type. Can be
0orHEAP_ZERO_MEMORY. dwBytes- The number of bytes to be allocated. If this parameter is
0and thedwFlagsparameter specifiesHEAP_GENERATE_EXCEPTIONS, an exception is raised and allocated memory of0bytes is returned.
If the function succeeds, the return value is a pointer to the memory block that was allocated. If the function fails, the return value is NULL. To get extended error information, call GetLastError.
HeapFree
Frees a memory block previously allocated from a heap by the HeapAlloc or HeapReAlloc function.
Parameters:
hHeap- A handle to the heap that contains the memory block to be freed.
dwFlags- Optional. Flags that control the freeing operation. Must be zero.
lpMem- A pointer to the memory block to be freed. This pointer is returned by a previous call to
HeapAllocorHeapReAlloc.
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
Memory Protection and Information
VirtualProtect
Changes the protection on an allocated region of pages in the virtual address space of the calling process.
Parameters:
lpAddress- A pointer to the base address of the region of pages whose protection attributes are to be modified.
dwSize- The size, in bytes, of the region of pages to be changed.
flNewProtect- The memory protection option for the region of pages. For a list of values, see Memory Protection Constants.
lpflOldProtect- A pointer to a variable that receives the previous access protection value of the first page in the specified region of pages.
If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. To get extended error information, call GetLastError.
VirtualQuery
Returns detailed information about a region of pages that make up the virtual address space of the calling process.
Parameters:
lpvAddress- A pointer to the starting address of the region of pages to be queried.
lpBuffer- A pointer to a
MEMORY_BASIC_INFORMATIONstructure that receives information about the specified memory region. dwLength- The size, in bytes, of the buffer pointed to by the
lpBufferparameter.
The return value is the actual size, in bytes, of the buffer that received the information (the length of the MEMORY_BASIC_INFORMATION structure). If the function fails, the return value is 0.
Advanced Concepts
Explore related concepts like: