/* This function checks to see if the given memory map descriptor in a memory map can be merged with any guarded free pages. */
VOID MergeGuardPages(IN EFI_MEMORY_DESCRIPTOR *MemoryMapEntry, IN EFI_PHYSICAL_ADDRESS MaxAddress)
/* This function checks to see if the given memory map descriptor in a memory map can be merged with any guarded free pages. */
VOID MergeGuardPages(IN EFI_MEMORY_DESCRIPTOR *MemoryMapEntry, IN EFI_PHYSICAL_ADDRESS MaxAddress)
{
  EFI_PHYSICAL_ADDRESS  EndAddress;
  UINT64                Bitmap;
  INTN                  Pages;
  if (!IsHeapGuardEnabled (GUARD_HEAP_TYPE_FREED) ||
      (MemoryMapEntry->Type >= EfiMemoryMappedIO))
  {
    return;
  }
  Bitmap = 0;
  Pages  = EFI_SIZE_TO_PAGES ((UINTN)(MaxAddress - MemoryMapEntry->PhysicalStart));
  Pages -= (INTN)MemoryMapEntry->NumberOfPages;
  while (Pages > 0) {
    if (Bitmap == 0) {
      EndAddress = MemoryMapEntry->PhysicalStart +
                   EFI_PAGES_TO_SIZE ((UINTN)MemoryMapEntry->NumberOfPages);
      Bitmap = GetGuardedMemoryBits (EndAddress, GUARDED_HEAP_MAP_ENTRY_BITS);
    }
    if ((Bitmap & 1) == 0) {
      break;
    }
    Pages--;
    MemoryMapEntry->NumberOfPages++;
    Bitmap = RShiftU64 (Bitmap, 1);
  }
}