4.2.3 Use UEFI Memory Allocation Services

The AllocatePool() service does not allow the caller to specify a preferred address so this service is always safe to use and has no impact on platform compatibility. The AllocatePages() service does have a mode that allows a specific address to be specified or a range of addresses to be specified. The allocation type of AllocateAnyPages is safe to use and increases platform compatibility. The allocation types of AllocateMaxAddress and AllocateAddress may reduce platform compatibility. Refer to Chapter 5 in this guide for information about using the AllocatePages service.

The general guideline for UEFI drivers is to make as few assumptions about the memory configuration of the platform as possible. This guideline applies to the memory that a UEFI driver allocates and the DMA buffer addresses that DMA bus master's use. A UEFI driver should not allocate buffers from specific addresses or below specific addresses. These types of allocations may fail on different system configurations. The following rules help ensure a UEFI Driver makes appropriate memory allocations.

  • Use natural alignment (byte values on byte boundaries) when allocating buffers. This maximizes portability and helps avoid alignment faults on IPF platforms.

  • Buffers allocated on a 32-bit CPU architecture using the UEFI Boot Service AllocatePool()are guaranteed to be below 4GB.

  • Buffers allocated on a 64-bit CPU architecture using the UEFI Boot Service AllocatePool() may be above 4GB if memory is present above 4 GB.

  • The UEFI Boot Service AllocatePages() may be used to allocate a buffer anywhere system memory is present. This means AllocatePages() may return a buffer on a 32-bit CPU architecture that is above 4 GB if memory is present above 4GB and that buffer can never be accessed.

  • All UEFI drivers must be aware that pointers may contain values above 4 GB, and care must be taken never to strip the upper address bits.

  • To prevent memory leaks, every allocation operation must have a corresponding free operation.

  • Test UEFI drivers on 64-bit architectures with memory configurations where system memory is present above 4GB.

  • Test UEFI drivers on 64-bit architectures with memory configurations where system memory is not present above 4GB.

  • UEFI drivers should not use fixed-size arrays. Instead, memory resources should be dynamically allocated using the AllocatePages() and AllocatePool() services.