/* Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). */
VOID* EFIAPI AllocateAlignedPages(IN UINTN Pages, IN UINTN Alignment)
/* Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). */
VOID* EFIAPI AllocateAlignedPages(IN UINTN Pages, IN UINTN Alignment)
{
  VOID   *Memory;
  UINTN  AlignmentMask;
  ASSERT ((Alignment & (Alignment - 1)) == 0);
  if (Pages == 0) {
    return NULL;
  }
  ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
  Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
  if (Alignment == 0) {
    AlignmentMask = Alignment;
  } else {
    AlignmentMask = Alignment - 1;
  }
  return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
}