Expand Minimize

RtlAllocateHeap routine

The RtlAllocateHeap routine allocates a block of memory from a heap.

Syntax


PVOID RtlAllocateHeap(
  _In_      PVOID HeapHandle,
  _In_opt_  ULONG Flags,
  _In_      SIZE_T Size
);

Parameters

HeapHandle [in]

Handle for a private heap from which the memory will be allocated. This parameter is a handle returned from a successful call to RtlCreateHeap.

Flags [in, optional]

Specifies several controllable aspects of heap allocation. Specifying any of these values will override the corresponding value specified when the heap was created with RtlCreateHeap. This parameter can be one or more of the following values.

FlagMeaning

HEAP_GENERATE_EXCEPTIONS

Specifies that the system will raise an exception to indicate a function failure, such as an out-of-memory condition, instead of returning NULL.

HEAP_NO_SERIALIZE

Specifies that mutual exclusion will not be used while RtlAllocateHeap is accessing the heap.

HEAP_ZERO_MEMORY

Specifies that the allocated memory will be initialized to zero. Otherwise, the memory is not initialized to zero.

 

Size [in]

Number of bytes to be allocated. If the heap, specified by the HeapHandle parameter, is a nongrowable heap, Size must be less than or equal to the heap's virtual memory threshold. (For more information, see the VirtualMemoryThreshold member of the Parameters parameter to RtlCreateHeap.)

Return value

If the call to RtlAllocateHeap succeeds, the return value is a pointer to the newly-allocated block.

Return codeDescription
STATUS_ACCESS_VIOLATION

The allocation attempt failed because of heap corruption or improper function parameters.

STATUS_BUFFER_TOO_SMALL

The allocation attempt failed because the heap is nongrowable and the requested Size was greater than the heap's virtual memory threshold.

STATUS_NO_MEMORY

The allocation attempt failed because of a lack of available memory or heap corruption.

 

Remarks

RtlAllocateHeap allocates a block of memory of the specified size from the specified heap.

To free a block of memory allocated by RtlAllocateHeap, call RtlFreeHeap.

Memory allocated by RtlAllocateHeap is not movable. Since the memory is not movable, it is possible for the heap to 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 to serialization, but it must be used whenever multiple threads allocate and free memory from the same heap. Setting the HEAP_NO_SERIALIZE value eliminates mutual exclusion on the heap. Without serialization, two or more threads that use the same heap handle might attempt to allocate or free memory simultaneously, likely causing corruption in the heap. The HEAP_NO_SERIALIZE value can, therefore, be safely used only in the following situations:

  • The process has only one thread.

  • The process has multiple threads, but only one thread calls the heap functions for a specific heap.

  • The process has multiple threads, and the application provides its own mechanism for mutual exclusion to a specific heap.

Note   To guard against an access violation, use structured exception handling to protect any code that writes to or reads from a heap. For more information about structured exception handling with memory accesses, see Handling Exceptions.

Requirements

Version

This routine is available on Microsoft Windows XP and later.

Header

Ntifs.h (include Ntifs.h)

Library

Ntoskrnl.lib

IRQL

< DISPATCH_LEVEL

See also

RtlCreateHeap
RtlDestroyHeap
RtlFreeHeap

 

 

Send comments about this topic to Microsoft

Show:
© 2014 Microsoft