+/**\r
+ Frees previous allocated pages.\r
+\r
+ @param Memory Base address of memory being freed\r
+ @param NumberOfPages The number of pages to free\r
+\r
+ @retval EFI_NOT_FOUND Could not find the entry that covers the range\r
+ @retval EFI_INVALID_PARAMETER Address not aligned\r
+ @return EFI_SUCCESS -Pages successfully freed.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CoreFreePages (\r
+ IN EFI_PHYSICAL_ADDRESS Memory,\r
+ IN UINTN NumberOfPages\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_MEMORY_TYPE MemoryType;\r
+\r
+ Status = CoreInternalFreePages (Memory, NumberOfPages, &MemoryType);\r
+ if (!EFI_ERROR (Status)) {\r
+ CoreUpdateProfile ((EFI_PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS (0), MemoryProfileActionFreePages, (EFI_MEMORY_TYPE) 0, EFI_PAGES_TO_SIZE (NumberOfPages), (VOID *) (UINTN) Memory);\r
+ }\r
+ return Status;\r
+}\r
+\r
+/**\r
+ This function checks to see if the last memory map descriptor in a memory map\r
+ can be merged with any of the other memory map descriptors in a memorymap.\r
+ Memory descriptors may be merged if they are adjacent and have the same type\r
+ and attributes.\r
+\r
+ @param MemoryMap A pointer to the start of the memory map.\r
+ @param MemoryMapDescriptor A pointer to the last descriptor in MemoryMap.\r
+ @param DescriptorSize The size, in bytes, of an individual\r
+ EFI_MEMORY_DESCRIPTOR.\r
+\r
+ @return A pointer to the next available descriptor in MemoryMap\r
+\r
+**/\r
+EFI_MEMORY_DESCRIPTOR *\r
+MergeMemoryMapDescriptor (\r
+ IN EFI_MEMORY_DESCRIPTOR *MemoryMap,\r
+ IN EFI_MEMORY_DESCRIPTOR *MemoryMapDescriptor,\r
+ IN UINTN DescriptorSize\r
+ )\r
+{\r
+ //\r
+ // Traverse the array of descriptors in MemoryMap\r
+ //\r
+ for (; MemoryMap != MemoryMapDescriptor; MemoryMap = NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize)) {\r
+ //\r
+ // Check to see if the Type fields are identical.\r
+ //\r
+ if (MemoryMap->Type != MemoryMapDescriptor->Type) {\r
+ continue;\r
+ }\r
+\r
+ //\r
+ // Check to see if the Attribute fields are identical.\r
+ //\r
+ if (MemoryMap->Attribute != MemoryMapDescriptor->Attribute) {\r
+ continue;\r
+ }\r
+\r
+ //\r
+ // Check to see if MemoryMapDescriptor is immediately above MemoryMap\r
+ //\r
+ if (MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) { \r
+ //\r
+ // Merge MemoryMapDescriptor into MemoryMap\r
+ //\r
+ MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
+\r
+ //\r
+ // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
+ //\r
+ return MemoryMapDescriptor;\r
+ }\r
+\r
+ //\r
+ // Check to see if MemoryMapDescriptor is immediately below MemoryMap\r
+ //\r
+ if (MemoryMap->PhysicalStart - EFI_PAGES_TO_SIZE ((UINTN)MemoryMapDescriptor->NumberOfPages) == MemoryMapDescriptor->PhysicalStart) {\r
+ //\r
+ // Merge MemoryMapDescriptor into MemoryMap\r
+ //\r
+ MemoryMap->PhysicalStart = MemoryMapDescriptor->PhysicalStart;\r
+ MemoryMap->VirtualStart = MemoryMapDescriptor->VirtualStart;\r
+ MemoryMap->NumberOfPages += MemoryMapDescriptor->NumberOfPages;\r
+\r
+ //\r
+ // Return MemoryMapDescriptor as the next available slot int he MemoryMap array\r
+ //\r
+ return MemoryMapDescriptor;\r
+ }\r
+ }\r
+\r
+ //\r
+ // MemoryMapDescrtiptor could not be merged with any descriptors in MemoryMap.\r
+ //\r
+ // Return the slot immediately after MemoryMapDescriptor as the next available \r
+ // slot in the MemoryMap array\r
+ //\r
+ return NEXT_MEMORY_DESCRIPTOR (MemoryMapDescriptor, DescriptorSize);\r
+}\r