Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/22
Memory management Dynadata Copyright, 2014 © DynaData S.A. 2/22
Dynadata Memory management Six sets of memory management functions exist in Win32, all of which were designed to be used independently of one another. Virtual memory functions. A process's virtual address space, system pagefile, system memory and hard disk space Memory-mapped file functions. A process's virtual address space, system pagefile, standard file I/O, System memory and hard disk space Heap memory functions. A process's virtual address space, system memory, process heap resource structure Global heap memory functions. A process's heap resource structure Local heap memory functions. A process's heap resource structure C run-time reference library. A process's heap resource structure Dynadata Copyright, 2014 © DynaData S.A. 3/22
Dynadata Memory management II Determining which function or set of functions to use for managing memory in your Win32™ application is difficult without a solid understanding of how each group of functions works and the overall impact they each have on the Microsoft® Windows NT™ operating system. Dynadata Copyright, 2014 © DynaData S.A. 4/22
Dynadata Memory management III The first version of the Microsoft® Windows™ operating system introduced a method of managing dynamic memory based on a single global heap, which all applications and the system share, and multiple, private local heaps, one for each application. Local and global memory management functions were also provided, offering extended features for this new memory management system. More recently, the Microsoft C run-time (CRT) libraries were modified to include capabilities for managing these heaps in Windows using native CRT functions such as malloc and free. Dynadata Copyright, 2014 © DynaData S.A. 5/22
Dynadata Memory management IV With the addition of the Win32 API, the number of choices increases. Win32 offers three additional groups of functions for managing memory in applications: Memory-mapped file functions Heap memory functions Virtual memory functions. These new functions do not replace the existing memory management functions found in Windows version 3.1; rather, they provide new features that generally make life easier for developers when writing the memory management portions of their applications for Win32. Dynadata Copyright, 2014 © DynaData S.A. 6/22
Memory management V Dynadata Copyright, 2014 © DynaData S.A. 7/22
Heap Memory Every process in Windows NT has one heap called the default heap. Processes can also have as many other dynamic heaps as they wish, simply by creating and destroying them on the fly. The Win32 subsystem uses the default heap for all global and local memory management functions, and the C run-time library uses the default heap for supporting malloc functions. The heap memory functions, which indicate a specific heap by its handle, use dynamic heaps. Dynadata Copyright, 2014 © DynaData S.A. 8/22
Dynadata Heap Memory II The default and dynamic heaps are basically the same thing, but the default heap has the special characteristic of being identifiable as the default. An application can create as many dynamic heaps as it like by simply calling HeapCreate. You must specify the maximum size of the heap so that the function knows how much address space to reserve. Dynadata Copyright, 2014 © DynaData S.A. 9/22
Dynadata Heap Memory III Since an application can have many dynamic heaps in its process, it need a handle to identify each heap to access it. The HeapCreate() function returns a handle witch identify each heap created. Having to identify heaps by handle also makes managing dynamic heaps more difficult than managing the default heap. Dynadata Copyright, 2014 © DynaData S.A. 10/22
Global memory functions GlobalAlloc GlobalDiscard GlobalFlags GlobalFree GlobalHandle GlobalLock GlobalReAlloc GlobalSize GlobalUnlock Dynadata Copyright, 2014 © DynaData S.A. 11/22
Types of Global and Local Memory In the Win32 API, the global and local memory management functions provide two types of memory, MOVEABLE and FIXED. MOVEABLE memory can be further qualified as DISCARDABLE memory. The number of memory handles available for MOVEABLE memory is limited to 65,535 handles. The Win32 subsystem imposes this limitation on each process. This limitation only applies to MOVEABLE memory. You can allocate as many chunks of FIXED memory as you like, provided the memory and address space are available in your process. Dynadata Copyright, 2014 © DynaData S.A. 12/22
Dynadata Virtual Memory Windows NT provides a page-based virtual memory management scheme that allows applications to realize a 32-bit linear address space for 4 gigabytes (GB) of memory. Internally, the system manages all memory in 4096-byte segments called pages. As a result, each application has its own private address space from which it can use the lower 2 GB—the system reserves the upper 2 GB of every process's address space for its own use. Dynadata Copyright, 2014 © DynaData S.A. 13/22
Virtual Memory II The virtual-memory manager (VMM) in Windows NT is a separate process that is primarily responsible for managing the use of physical memory and pagefiles. In previous versions of Windows, an application had to allocate memory before being able to manipulate the addresses in that memory. In Windows NT, the address space of each process is already allocated; whether there is any memory associated with the addresses in the address space is a different issue. 14/22
Virtual Memory III The Win32 virtual memory management functions provide low-level support for independently managing both the addresses and memory of a process. The entire set of Win32 virtual memory functions is: VirtualAlloc and VirtualFree VirtualLock and VirtualUnlock VirtualQuery or VirtualQueryEx VirtualProtect or VirtualProtectEx 15/22
Virtual Memory IV Every address in a process can be thought of as either free, reserved, or committed at any given time. A process begins with all addresses free, meaning they are free to be committed to memory or reserved for future use. Before any free address may be used, it must first be allocated as reserved or committed. Attempting to access an address that is either reserved or free generates an access violation exception. 16/22
Dynadata Virtual Memory V To use reserved addresses, memory must first be committed to the addresses. Committing memory to addresses is similar to reserving it—call VirtualAlloc() with the dwAllocation parameter equal to MEM_COMMIT. Once addresses have been allocated as either reserved or committed, VirtualFree() is the only way to release them—that is, return them to free addresses. Dynadata Copyright, 2014 © DynaData S.A. 17/22
Dynadata Memory mapped files Memory-mapped files (MMFs) offer a unique memory management feature that allows applications to access files on disk in the same way they access dynamic memory—through pointers. With Memory-mapped files an application can map a view of all or part of a file on disk to a specific range of addresses within the process's address space. And once that is done, accessing the content of a memory-mapped file is as simple as dereferencing a pointer in the designated range of addresses. Dynadata Copyright, 2014 © DynaData S.A. 18/22
Dynadata Memory mapped files II So, writing data to a file can be as simple as assigning a value to a dereferenced pointer as in: *pMem = 23; Similarly, reading from a specific location within the file is simply: nTokenLen = *pMem; Dynadata Copyright, 2014 © DynaData S.A. 19/22
Memory mapped files III File mapping is the association of a file's contents with a portion of the virtual address space of a process. The system creates a file-mapping object to maintain this association. A file view is the portion of virtual address space that the process uses to access the file's contents. Dynadata Copyright, 2014 © DynaData S.A. 20/22
Dynadata Memory mapped files IV Processes read from and write to the file view using pointers, just as they would with dynamically allocated memory. Processes can also manipulate the file view with the virtual memory function VirtualProtect(). Dynadata Copyright, 2014 © DynaData S.A. 21/22
Dynadata Memory mapped files IV The file-mapping functions allow a process to create file-mapping objects and file views to easily access and share data. The file cannot reside on a remote computer if you want to share memory between multiple processes. Dynadata Copyright, 2014 © DynaData S.A. 22/22