/* This function checks to see if the last memory map descriptor in a memory map can be merged with any of the other memory map descriptors in a memorymap. Memory descriptors may be merged if they are adjacent and have the same type and attributes. */
EFI_MEMORY_DESCRIPTOR* MergeMemoryMapDescriptor(IN EFI_MEMORY_DESCRIPTOR *MemoryMap, IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor, IN UINTN DescriptorSize)
/* This function checks to see if the last memory map descriptor in a memory map can be merged with any of the other memory map descriptors in a memorymap. Memory descriptors may be merged if they are adjacent and have the same type and attributes. */
EFI_MEMORY_DESCRIPTOR* MergeMemoryMapDescriptor(IN EFI_MEMORY_DESCRIPTOR *MemoryMap, IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor, IN UINTN DescriptorSize)
{
  for ( ; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {
    if (MemoryMap->Type != MemoryMapDescriptor->Type) {
      continue;
    }
    if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {
      continue;
    }
    if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
      MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;
      return MemoryMapDescriptor;
    }
    if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {
      MemoryMap->PhysicalStart  = MemoryMapDescriptor->PhysicalStart;
      MemoryMap->VirtualStart   = MemoryMapDescriptor->VirtualStart;
      MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;
      return MemoryMapDescriptor;
    }
  }
  return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);
}