Managing Heap MemoryObtain memory blocks from a heap by specifying the heap's handle, the block size, and several flags.
ParametershHeap is the handle of the heap in which the memory block is to be allocated. This handle should come from either GetProcessHeap or HeapCreate. dwFlags is a combination of three flags.
dwBytes is the size of the block of memory to allocate. For nongrowable heaps, this is limited to 0x7FFF8 (approximately 0.5MB). Note: Deallocating memory from a heap is simple.
dwFlags should be 0 or HEAP_NO_SERIALIZE. lpMem should be a value returned by HeapAlloc or HeapReAlloc (described next), and, of course, hHeap should be the heap from which lpMem was allocated. Memory blocks can be reallocated to change their size.
ParametersThe first parameter, hHeap, is the same heap used with the HeapAlloc call that returned the lpMem value (the third parameter). dwFlags specifies some essential control options.
lpMem specifies the existing block in hHeap to be reallocated. dwBytes is the new block size, which can be larger or smaller than the existing size. Normally, the returned pointer is the same as lpMem. If, on the other hand, a block is moved (permit this by omitting the HEAP_REALLOC_IN_PLACE_ONLY flag), the returned value will be different. Be careful to update any references to the block. The data in the block is unchanged, regardless of whether or not it is moved; however, some data will be lost if the block size is reduced. Determine the size of an allocated block by calling HeapSize (this function should have been named BlockSize because it does not obtain the size of the heap) with the heap handle and block pointer.
The HEAP_NO_SERIALIZE FlagThe functions HeapCreate, HeapAlloc, and HeapReAlloc can specify the HEAP_NO_SERIALIZE flag. There can be a small performance gain with this flag because the functions do not provide mutual exclusion to threads accessing the heap. Some simple tests that do nothing except allocate memory blocks measured a performance improvement of about 16 percent. This flag is safe in a few situations, such as the following.
The HEAP_GENERATE_EXCEPTIONS FlagForcing exceptions in the case of memory allocation failure avoids the need for annoying error tests after each allocation. Furthermore, the exception or termination handler can clean up memory that did get allocated. This technique is used in some examples. Two exception codes are possible.
Other Heap FunctionsHeapCompact attempts to consolidate, or defragment, adjacent free blocks in a heap. HeapValidate attempts to detect heap corruption. HeapWalk enumerates the blocks in a heap, and GetProcessHeaps obtains all the heap handles that are valid in a process. HeapLock and HeapUnlock allow a thread to serialize heap access, as described in Chapter 8. Note that these functions do not work under Windows 9x or CE. Also, some obsolete functions, such as GlobalAlloc and LocalAlloc, were used for compatibility with 16-bit systems. These functions are mentioned simply as a reminder that many functions continue to be supported even though they are no longer relevant. Summary: Heap ManagementThe normal process for using heaps is straightforward.
This process is illustrated in both Figure 5-2 and Program 5-1. Figure 5-2. Memory Management in Multiple Heaps[View full size image]
|
Thursday, November 12, 2009
Managing Heap Memory
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment