]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Library / DxeCoreMemoryAllocationLib / MemoryAllocationLib.c
index 3d01c27987bb44cf1e66816a63ce877c41472247..e813e87f24701ea05bde8a01762e7aabcc808b8f 100644 (file)
@@ -4,20 +4,12 @@
   with memory profile support.\r
 \r
   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
-  This program and the accompanying materials\r
-  are licensed and made available under the terms and conditions of the BSD License\r
-  which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
-\r
 #include <PiDxe.h>\r
 \r
-\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
@@ -55,7 +47,8 @@ InternalAllocatePages (
   if (EFI_ERROR (Status)) {\r
     return NULL;\r
   }\r
-  return (VOID *) (UINTN) Memory;\r
+\r
+  return (VOID *)(UINTN)Memory;\r
 }\r
 \r
 /**\r
@@ -82,7 +75,7 @@ AllocatePages (
   Buffer = InternalAllocatePages (EfiBootServicesData, Pages);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES,\r
       EfiBootServicesData,\r
       Buffer,\r
@@ -90,6 +83,7 @@ AllocatePages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -117,7 +111,7 @@ AllocateRuntimePages (
   Buffer = InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -125,6 +119,7 @@ AllocateRuntimePages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -152,7 +147,7 @@ AllocateReservedPages (
   Buffer = InternalAllocatePages (EfiReservedMemoryType, Pages);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES,\r
       EfiReservedMemoryType,\r
       Buffer,\r
@@ -160,6 +155,7 @@ AllocateReservedPages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -190,7 +186,7 @@ FreePages (
   EFI_STATUS  Status;\r
 \r
   ASSERT (Pages != 0);\r
-  Status = CoreFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  Status = CoreFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -234,23 +230,25 @@ InternalAllocateAlignedPages (
   if (Pages == 0) {\r
     return NULL;\r
   }\r
+\r
   if (Alignment > EFI_PAGE_SIZE) {\r
     //\r
     // Calculate the total number of pages since alignment is larger than page size.\r
     //\r
-    AlignmentMask  = Alignment - 1;\r
-    RealPages      = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
+    AlignmentMask = Alignment - 1;\r
+    RealPages     = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
     //\r
     // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
     //\r
     ASSERT (RealPages > Pages);\r
 \r
-    Status         = CoreAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
+    Status = CoreAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
     if (EFI_ERROR (Status)) {\r
       return NULL;\r
     }\r
-    AlignedMemory  = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
-    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
+\r
+    AlignedMemory  = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
     if (UnalignedPages > 0) {\r
       //\r
       // Free first unaligned page(s).\r
@@ -258,6 +256,7 @@ InternalAllocateAlignedPages (
       Status = CoreFreePages (Memory, UnalignedPages);\r
       ASSERT_EFI_ERROR (Status);\r
     }\r
+\r
     Memory         = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
     UnalignedPages = RealPages - Pages - UnalignedPages;\r
     if (UnalignedPages > 0) {\r
@@ -275,9 +274,11 @@ InternalAllocateAlignedPages (
     if (EFI_ERROR (Status)) {\r
       return NULL;\r
     }\r
-    AlignedMemory  = (UINTN) Memory;\r
+\r
+    AlignedMemory = (UINTN)Memory;\r
   }\r
-  return (VOID *) AlignedMemory;\r
+\r
+  return (VOID *)AlignedMemory;\r
 }\r
 \r
 /**\r
@@ -310,7 +311,7 @@ AllocateAlignedPages (
   Buffer = InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES,\r
       EfiBootServicesData,\r
       Buffer,\r
@@ -318,6 +319,7 @@ AllocateAlignedPages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -351,7 +353,7 @@ AllocateAlignedRuntimePages (
   Buffer = InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -359,6 +361,7 @@ AllocateAlignedRuntimePages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -392,7 +395,7 @@ AllocateAlignedReservedPages (
   Buffer = InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES,\r
       EfiReservedMemoryType,\r
       Buffer,\r
@@ -400,6 +403,7 @@ AllocateAlignedReservedPages (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -430,7 +434,7 @@ FreeAlignedPages (
   EFI_STATUS  Status;\r
 \r
   ASSERT (Pages != 0);\r
-  Status = CoreFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+  Status = CoreFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
@@ -462,6 +466,7 @@ InternalAllocatePool (
   if (EFI_ERROR (Status)) {\r
     Memory = NULL;\r
   }\r
+\r
   return Memory;\r
 }\r
 \r
@@ -488,7 +493,7 @@ AllocatePool (
   Buffer = InternalAllocatePool (EfiBootServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL,\r
       EfiBootServicesData,\r
       Buffer,\r
@@ -496,6 +501,7 @@ AllocatePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -522,7 +528,7 @@ AllocateRuntimePool (
   Buffer = InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -530,6 +536,7 @@ AllocateRuntimePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -556,7 +563,7 @@ AllocateReservedPool (
   Buffer = InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL,\r
       EfiReservedMemoryType,\r
       Buffer,\r
@@ -564,6 +571,7 @@ AllocateReservedPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -593,6 +601,7 @@ InternalAllocateZeroPool (
   if (Memory != NULL) {\r
     Memory = ZeroMem (Memory, AllocationSize);\r
   }\r
+\r
   return Memory;\r
 }\r
 \r
@@ -620,7 +629,7 @@ AllocateZeroPool (
   Buffer = InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL,\r
       EfiBootServicesData,\r
       Buffer,\r
@@ -628,6 +637,7 @@ AllocateZeroPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -655,7 +665,7 @@ AllocateRuntimeZeroPool (
   Buffer = InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -663,6 +673,7 @@ AllocateRuntimeZeroPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -690,7 +701,7 @@ AllocateReservedZeroPool (
   Buffer = InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL,\r
       EfiReservedMemoryType,\r
       Buffer,\r
@@ -698,6 +709,7 @@ AllocateReservedZeroPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -728,12 +740,13 @@ InternalAllocateCopyPool (
   VOID  *Memory;\r
 \r
   ASSERT (Buffer != NULL);\r
-  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
+  ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));\r
 \r
   Memory = InternalAllocatePool (PoolType, AllocationSize);\r
   if (Memory != NULL) {\r
-     Memory = CopyMem (Memory, Buffer, AllocationSize);\r
+    Memory = CopyMem (Memory, Buffer, AllocationSize);\r
   }\r
+\r
   return Memory;\r
 }\r
 \r
@@ -766,7 +779,7 @@ AllocateCopyPool (
   NewBuffer = InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);\r
   if (NewBuffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL,\r
       EfiBootServicesData,\r
       NewBuffer,\r
@@ -774,6 +787,7 @@ AllocateCopyPool (
       NULL\r
       );\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -806,7 +820,7 @@ AllocateRuntimeCopyPool (
   NewBuffer = InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
   if (NewBuffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL,\r
       EfiRuntimeServicesData,\r
       NewBuffer,\r
@@ -814,6 +828,7 @@ AllocateRuntimeCopyPool (
       NULL\r
       );\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -846,7 +861,7 @@ AllocateReservedCopyPool (
   NewBuffer = InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
   if (NewBuffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL,\r
       EfiRuntimeServicesData,\r
       NewBuffer,\r
@@ -854,6 +869,7 @@ AllocateReservedCopyPool (
       NULL\r
       );\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -890,10 +906,11 @@ InternalReallocatePool (
   VOID  *NewBuffer;\r
 \r
   NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
-  if (NewBuffer != NULL && OldBuffer != NULL) {\r
+  if ((NewBuffer != NULL) && (OldBuffer != NULL)) {\r
     CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
     FreePool (OldBuffer);\r
   }\r
+\r
   return NewBuffer;\r
 }\r
 \r
@@ -931,7 +948,7 @@ ReallocatePool (
   Buffer = InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL,\r
       EfiBootServicesData,\r
       Buffer,\r
@@ -939,6 +956,7 @@ ReallocatePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -976,7 +994,7 @@ ReallocateRuntimePool (
   Buffer = InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL,\r
       EfiRuntimeServicesData,\r
       Buffer,\r
@@ -984,6 +1002,7 @@ ReallocateRuntimePool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -1021,7 +1040,7 @@ ReallocateReservedPool (
   Buffer = InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
   if (Buffer != NULL) {\r
     MemoryProfileLibRecord (\r
-      (PHYSICAL_ADDRESS) (UINTN) RETURN_ADDRESS(0),\r
+      (PHYSICAL_ADDRESS)(UINTN)RETURN_ADDRESS (0),\r
       MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL,\r
       EfiReservedMemoryType,\r
       Buffer,\r
@@ -1029,6 +1048,7 @@ ReallocateReservedPool (
       NULL\r
       );\r
   }\r
+\r
   return Buffer;\r
 }\r
 \r
@@ -1049,12 +1069,11 @@ ReallocateReservedPool (
 VOID\r
 EFIAPI\r
 FreePool (\r
-  IN VOID   *Buffer\r
+  IN VOID  *Buffer\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
+  EFI_STATUS  Status;\r
 \r
   Status = CoreFreePool (Buffer);\r
   ASSERT_EFI_ERROR (Status);\r
 }\r
-\r