This function allocates a block of memory from a heap. The allocated memory is not movable.
LPVOID HeapAlloc( HANDLE hHeap, DWORD dwFlags, DWORD dwBytes );
[in] Handle to the heap from which the memory will be allocated.
[in] Heap allocation options. Specifying any of these flags will override the corresponding flag specified when the heap was created with HeapCreate.
The following table shows the flags you can specify. You can specify one or more of these flags.
Is ignored. The heap is always serialized.
Specifies that the allocated memory will be initialized to zero. Otherwise, the memory is not initialized to zero.
[in] Number of bytes to be allocated.
If the heap specified by the hHeap parameter is a nongrowable heap, dwBytes must be less than 0x7FFF8.
You create a nongrowable heap by calling the HeapCreate function with a nonzero value.
If HeapAlloc succeeds, it allocates at least the amount of memory requested. If the amount allocated is greater than the amount requested, the process can use the entire amount. To determine the actual size of the allocated block, use the HeapSize function.
To free a block of memory allocated by HeapAlloc, use the HeapFree function.
Memory allocated by HeapAlloc is not movable. Because the memory is not movable, the heap can become fragmented.
Serialization ensures mutual exclusion when two or more threads attempt to simultaneously allocate or free blocks from the same heap. There is a small performance cost, but it must be used whenever multiple threads allocate and free memory from the same heap.
A critical section is always used to serialize access to an individual heap. There is a critical section per heap to protect access to each heap.
An attempt to grab a critical section that is not owned is a fast path operation that incurs little overhead. This is similar to using the HEAP_NO_SERIALIZE flag if only one thread was ever accessing a particular heap.
If there is contention for the critical section and therefore the heap, a new thread request to allocate heap space will serialize.
HeapAlloc(GetProcessHeap(), ...) is equivalent to LocalAlloc(...).