Support routines for memory allocation routines \r
based on PeiService for PEI phase drivers.\r
\r
- Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
- All rights reserved. This program and the accompanying materials \r
+ Copyright (c) 2006 - 2017, 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
+ 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
#include <Library/PeiServicesLib.h>\r
#include <Library/BaseMemoryLib.h>\r
#include <Library/DebugLib.h>\r
+#include <Library/HobLib.h>\r
\r
\r
/**\r
if (EFI_ERROR (Status)) {\r
return NULL;\r
}\r
+\r
return (VOID *) (UINTN) Memory;\r
}\r
\r
then ASSERT().\r
If Pages is zero, then ASSERT().\r
\r
- @param Buffer Pointer to the buffer of pages to free.\r
+ @param Buffer The pointer to the buffer of pages to free.\r
@param Pages The number of 4 KB pages to free.\r
\r
**/\r
IN UINTN Pages\r
)\r
{\r
+ EFI_STATUS Status;\r
+\r
ASSERT (Pages != 0);\r
- //\r
- // PEI phase does not support to free pages, so leave it as NOP.\r
- //\r
+ Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+ ASSERT_EFI_ERROR (Status);\r
}\r
\r
/**\r
If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
NULL is returned.\r
If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+ If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
\r
@param MemoryType The type of memory to allocate.\r
@param Pages The number of 4 KB pages to allocate.\r
- @param Alignment The requested alignment of the allocation. Must be a power of two.\r
+ @param Alignment The requested alignment of the allocation. \r
+ Must be a power of two.\r
If Alignment is zero, then byte alignment is used.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
IN UINTN Alignment\r
)\r
{\r
- VOID *Memory;\r
- UINTN AlignmentMask;\r
+ EFI_STATUS Status;\r
+ EFI_PHYSICAL_ADDRESS Memory;\r
+ UINTN AlignedMemory;\r
+ UINTN AlignmentMask;\r
+ UINTN UnalignedPages;\r
+ UINTN RealPages;\r
\r
//\r
// Alignment must be a power of two or zero.\r
if (Pages == 0) {\r
return NULL;\r
}\r
- //\r
- // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
- //\r
- ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
- //\r
- // We would rather waste some memory to save PEI code size.\r
- //\r
- Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r
- if (Alignment == 0) {\r
- AlignmentMask = Alignment;\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
+ //\r
+ // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
+ //\r
+ ASSERT (RealPages > Pages);\r
+\r
+ Status = PeiServicesAllocatePages (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
+ if (UnalignedPages > 0) {\r
+ //\r
+ // Free first unaligned page(s).\r
+ //\r
+ Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
+ Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
+ UnalignedPages = RealPages - Pages - UnalignedPages;\r
+ if (UnalignedPages > 0) {\r
+ //\r
+ // Free last unaligned page(s).\r
+ //\r
+ Status = PeiServicesFreePages (Memory, UnalignedPages);\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
} else {\r
- AlignmentMask = Alignment - 1; \r
+ //\r
+ // Do not over-allocate pages in this case.\r
+ //\r
+ Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
+ if (EFI_ERROR (Status)) {\r
+ return NULL;\r
+ }\r
+ AlignedMemory = (UINTN) Memory;\r
}\r
- return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
+ return (VOID *) AlignedMemory;\r
}\r
\r
/**\r
request, then NULL is returned.\r
\r
If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+ If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
\r
@param Pages The number of 4 KB pages to allocate.\r
- @param Alignment The requested alignment of the allocation. Must be a power of two.\r
+ @param Alignment The requested alignment of the allocation. \r
+ Must be a power of two.\r
If Alignment is zero, then byte alignment is used.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
request, then NULL is returned.\r
\r
If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+ If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
\r
@param Pages The number of 4 KB pages to allocate.\r
- @param Alignment The requested alignment of the allocation. Must be a power of two.\r
+ @param Alignment The requested alignment of the allocation. \r
+ Must be a power of two.\r
If Alignment is zero, then byte alignment is used.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
request, then NULL is returned.\r
\r
If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
+ If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
\r
@param Pages The number of 4 KB pages to allocate.\r
- @param Alignment The requested alignment of the allocation. Must be a power of two.\r
+ @param Alignment The requested alignment of the allocation. \r
+ Must be a power of two.\r
If Alignment is zero, then byte alignment is used.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
Library, then ASSERT().\r
If Pages is zero, then ASSERT().\r
\r
- @param Buffer Pointer to the buffer of pages to free.\r
+ @param Buffer The pointer to the buffer of pages to free.\r
@param Pages The number of 4 KB pages to free.\r
\r
**/\r
IN UINTN Pages\r
)\r
{\r
+ EFI_STATUS Status;\r
+\r
ASSERT (Pages != 0);\r
- //\r
- // PEI phase does not support to free pages, so leave it as NOP.\r
- //\r
+ Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages);\r
+ ASSERT_EFI_ERROR (Status);\r
}\r
\r
/**\r
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
enough memory remaining to satisfy the request, then NULL is returned.\r
\r
- If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1),\r
- then ASSERT(). \r
+ If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
+ is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
\r
@param PoolType The type of pool to allocate.\r
@param OldSize The size, in bytes, of OldBuffer.\r
@param NewSize The size, in bytes, of the buffer to reallocate.\r
- @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
- parameter that may be NULL.\r
+ @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
+ optional parameter that may be NULL.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
\r
{\r
VOID *NewBuffer;\r
\r
- //\r
- // Check the boundary for OldBuffer even if the allocation failure.\r
- //\r
- ASSERT (OldBuffer == NULL || MIN (OldSize, NewSize) <= MAX_ADDRESS - (UINTN) OldBuffer + 1);\r
NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
if (NewBuffer != NULL && OldBuffer != NULL) {\r
CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
enough memory remaining to satisfy the request, then NULL is returned.\r
\r
- If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1),\r
- then ASSERT(). \r
+ If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
+ is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
\r
@param OldSize The size, in bytes, of OldBuffer.\r
@param NewSize The size, in bytes, of the buffer to reallocate.\r
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
enough memory remaining to satisfy the request, then NULL is returned.\r
\r
- If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1),\r
- then ASSERT(). \r
+ If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
+ is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
\r
@param OldSize The size, in bytes, of OldBuffer.\r
@param NewSize The size, in bytes, of the buffer to reallocate.\r
If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
enough memory remaining to satisfy the request, then NULL is returned.\r
\r
- If the smaller of NewSize and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1),\r
- then ASSERT(). \r
+ If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
+ is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
\r
@param OldSize The size, in bytes, of OldBuffer.\r
@param NewSize The size, in bytes, of the buffer to reallocate.\r
- @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
- parameter that may be NULL.\r
+ @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
+ optional parameter that may be NULL.\r
\r
@return A pointer to the allocated buffer or NULL if allocation fails.\r
\r
If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
then ASSERT().\r
\r
- @param Buffer Pointer to the buffer to free.\r
+ @param Buffer The pointer to the buffer to free.\r
\r
**/\r
VOID\r