Memory Management

HeapCreate() can be used to create multiple private heaps.
Since it is possible to have many co-existing dynamic heaps in your process, HeapCreate() returns a unique handle to
identify the heap, which is used to identify the target heap in other heap functions.
Having to identify a heap by its handle makes managing dynamic heaps more difficult
than managing the default heap, since you have to keep each heap handle around for the
life of the heap. As mentioned previously, it is possible to get the handle to the default
heap using GetProcessHeap()
and to use the heap functions on this handle. This is very often what you want for small,
independent allocations. Similarly you could also use C-runtime memory management
functions like malloc(), or
local/global heap memory management functions like GlobalAlloc() and LocalAlloc().
When creating a heap the maximum size of the heap must be specified, so that the
function knows how much address space to reserve, and the amount of memory to be initially
committed must also be specified. A process would normally create a heap when it starts
up, specifying an initial size sufficient to satisfy the memory requirements of the
process. If the HeapCreate()
call failed, the process could terminate or issue some sort of memory warning; but if it
succeeded, the process is assured of having the memory when it needs it.
In deterministic cases it is easy to calculate the heap size needed. In other cases, it
is more difficult to predict. Dynamic heaps have a provision for this latter circumstance.
By specifying a maximum heap size of zero, you make the heap assume a behavior like the
default heap discussed already. It will grow and spread in your process as much as
necessary, limited only by available memory. In this case, available memory means
available virtual address space in the process and available physical memory and pagefile
space on the hard disk.
Like all of the heap memory functions, HeapAlloc() requires a heap handle as its first argument, and allocates blocks of memory
from a private heap and returns a pointer to the allocated block.
HeapReAlloc() requires a pointer to a previous allocation in
order to re-size it. Although heap memory is not intrinsically "movable" as in
global heap and local heap memory, it may be moved during HeapReAlloc() . The pointer returned to the resized block of
memory, may not be referencing the same location as initially indicated by the pointer
passed to the function. This is the only time memory can be moved in dynamic heaps. It
only affects the block of memory referenced in the call to HeapReAlloc() and it is application-driven.
A pointer returned by HeapAlloc()or HeapReAlloc()
can be used in HeapFree()
to release the block, or in HeapSize() to determine the size of the block.
A dynamic heap is destroyed by calling HeapDestroy() which invalidates the heap handle.
This function will remove a heap regardless of its state and doesnt care whether you
have outstanding allocations in the heap or not. |
|
|