X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdePkg%2FLibrary%2FPeiMemoryAllocationLib%2FMemoryAllocationLib.c;h=5cd24a38a3c66c6526618ee326453897a267fdab;hp=57a4009461b5c821bc5467edc7d8f89348068518;hb=c2ab95806bf0e5ae272f47ea6f192c12ce7ca069;hpb=c892d8464468618b0aa3c7f35edbdfbc7cc24ee5 diff --git a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c index 57a4009461..5cd24a38a3 100644 --- a/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c +++ b/MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c @@ -1,11 +1,12 @@ /** @file - Support routines for memory allocation routines for use with drivers. + Support routines for memory allocation routines + based on PeiService for PEI phase drivers. - Copyright (c) 2006, Intel Corporation
- All rights reserved. This program and the accompanying materials + Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
+ This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at - http://opensource.org/licenses/bsd-license.php + http://opensource.org/licenses/bsd-license.php. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. @@ -17,11 +18,11 @@ #include -#include +#include #include #include +#include -#include "MemoryAllocationLibInternals.h" /** Allocates one or more 4KB pages of a certain memory type. @@ -44,17 +45,16 @@ InternalAllocatePages ( { EFI_STATUS Status; EFI_PHYSICAL_ADDRESS Memory; - EFI_PEI_SERVICES **PeiServices; if (Pages == 0) { return NULL; } - PeiServices = GetPeiServicesTablePointer (); - Status = (*PeiServices)->AllocatePages (PeiServices, MemoryType, Pages, &Memory); + Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory); if (EFI_ERROR (Status)) { - Memory = 0; + return NULL; } + return (VOID *) (UINTN) Memory; } @@ -130,12 +130,14 @@ AllocateReservedPages ( Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer must have been allocated on a previous call to the page allocation services of the Memory - Allocation Library. + Allocation Library. If it is not possible to free allocated pages, then this function will + perform no actions. + If Buffer was not allocated with a page allocation function in the Memory Allocation Library, then ASSERT(). If Pages is zero, then ASSERT(). - @param Buffer Pointer to the buffer of pages to free. + @param Buffer The pointer to the buffer of pages to free. @param Pages The number of 4 KB pages to free. **/ @@ -146,9 +148,11 @@ FreePages ( IN UINTN Pages ) { - // - // PEI phase does not support to free pages, so leave it as NOP. - // + EFI_STATUS Status; + + ASSERT (Pages != 0); + Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); } /** @@ -159,10 +163,12 @@ FreePages ( If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param MemoryType The type of memory to allocate. @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -175,8 +181,12 @@ InternalAllocateAlignedPages ( IN UINTN Alignment ) { - VOID *Memory; - UINTN AlignmentMask; + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS Memory; + UINTN AlignedMemory; + UINTN AlignmentMask; + UINTN UnalignedPages; + UINTN RealPages; // // Alignment must be a power of two or zero. @@ -186,20 +196,50 @@ InternalAllocateAlignedPages ( if (Pages == 0) { return NULL; } - // - // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. - // - ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); - // - // We would rather waste some memory to save PEI code size. - // - Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment)); - if (Alignment == 0) { - AlignmentMask = Alignment; + if (Alignment > EFI_PAGE_SIZE) { + // + // Calculate the total number of pages since alignment is larger than page size. + // + AlignmentMask = Alignment - 1; + RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + // + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + ASSERT (RealPages > Pages); + + Status = PeiServicesAllocatePages (MemoryType, RealPages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; + UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + if (UnalignedPages > 0) { + // + // Free first unaligned page(s). + // + Status = PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } + Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages); + UnalignedPages = RealPages - Pages - UnalignedPages; + if (UnalignedPages > 0) { + // + // Free last unaligned page(s). + // + Status = PeiServicesFreePages (Memory, UnalignedPages); + ASSERT_EFI_ERROR (Status); + } } else { - AlignmentMask = Alignment - 1; + // + // Do not over-allocate pages in this case. + // + Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory); + if (EFI_ERROR (Status)) { + return NULL; + } + AlignedMemory = (UINTN) Memory; } - return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask); + return (VOID *) AlignedMemory; } /** @@ -209,10 +249,13 @@ InternalAllocateAlignedPages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -235,10 +278,13 @@ AllocateAlignedPages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -261,10 +307,13 @@ AllocateAlignedRuntimePages ( alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned. If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. + If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT(). @param Pages The number of 4 KB pages to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. + @param Alignment The requested alignment of the allocation. + Must be a power of two. If Alignment is zero, then byte alignment is used. @return A pointer to the allocated buffer or NULL if allocation fails. @@ -286,12 +335,14 @@ AllocateAlignedReservedPages ( Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer must have been allocated on a previous call to the aligned page allocation services of the Memory - Allocation Library. + Allocation Library. If it is not possible to free allocated pages, then this function will + perform no actions. + If Buffer was not allocated with an aligned page allocation function in the Memory Allocation Library, then ASSERT(). If Pages is zero, then ASSERT(). - @param Buffer Pointer to the buffer of pages to free. + @param Buffer The pointer to the buffer of pages to free. @param Pages The number of 4 KB pages to free. **/ @@ -302,9 +353,11 @@ FreeAlignedPages ( IN UINTN Pages ) { - // - // PEI phase does not support to free pages, so leave it as NOP. - // + EFI_STATUS Status; + + ASSERT (Pages != 0); + Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); } /** @@ -353,12 +406,9 @@ AllocatePool ( ) { EFI_STATUS Status; - EFI_PEI_SERVICES **PeiServices; VOID *Buffer; - PeiServices = GetPeiServicesTablePointer (); - - Status = (*PeiServices)->AllocatePool (PeiServices, AllocationSize, &Buffer); + Status = PeiServicesAllocatePool (AllocationSize, &Buffer); if (EFI_ERROR (Status)) { Buffer = NULL; } @@ -387,9 +437,9 @@ AllocateRuntimePool ( } /** - Allocates a buffer of type EfieservedMemoryType. + Allocates a buffer of type EfiReservedMemoryType. - Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns + Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. @@ -408,9 +458,9 @@ AllocateReservedPool ( } /** - Allocates and zeros a buffer of a certian pool type. + Allocates and zeros a buffer of a certain pool type. - Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer + Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. @@ -509,14 +559,14 @@ AllocateReservedZeroPool ( } /** - Copies a buffer to an allocated buffer of a certian pool type. + Copies a buffer to an allocated buffer of a certain pool type. - Allocates the number bytes specified by AllocationSize of a certian pool type, copies + Allocates the number bytes specified by AllocationSize of a certain pool type, copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param PoolType The type of pool to allocate. @param AllocationSize The number of bytes to allocate and zero. @@ -551,8 +601,9 @@ InternalAllocateCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -586,8 +637,9 @@ AllocateCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -612,8 +664,9 @@ AllocateRuntimeCopyPool ( AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. + If Buffer is NULL, then ASSERT(). - If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT(). + If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). @param AllocationSize The number of bytes to allocate and zero. @param Buffer The buffer to copy to the allocated buffer. @@ -632,446 +685,158 @@ AllocateReservedCopyPool ( } /** - Frees a buffer that was previously allocated with one of the pool allocation functions in the - Memory Allocation Library. + Reallocates a buffer of a specified memory type. - Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the - pool allocation services of the Memory Allocation Library. - If Buffer was not allocated with a pool allocation function in the Memory Allocation Library, - then ASSERT(). - - @param Buffer Pointer to the buffer to free. - -**/ -VOID -EFIAPI -FreePool ( - IN VOID *Buffer - ) -{ - // - // PEI phase does not support to free pool, so leave it as NOP. - // -} - -/** - Allocates a buffer of a certain pool type at a specified alignment. - - Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment - specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid - buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining - to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param PoolType The type of pool to allocate. - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -InternalAllocateAlignedPool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN AllocationSize, - IN UINTN Alignment - ) -{ - VOID *RawAddress; - UINTN AlignedAddress; - UINTN AlignmentMask; - - // - // Alignment must be a power of two or zero. - // - ASSERT ((Alignment & (Alignment - 1)) == 0); + Allocates and zeros the number bytes specified by NewSize from memory of the type + specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + enough memory remaining to satisfy the request, then NULL is returned. - if (Alignment == 0) { - AlignmentMask = Alignment; - } else { - AlignmentMask = Alignment - 1; - } - // - // Make sure that AllocationSize plus AlignmentMask does not overflow. - // - ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask)); - - RawAddress = InternalAllocatePool (PoolType, AllocationSize + AlignmentMask); - - AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask; - - return (VOID *) AlignedAddress; -} - -/** - Allocates a buffer of type EfiBootServicesData at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedPool ( - IN UINTN AllocationSize, - IN UINTN Alignment - ) -{ - VOID *RawAddress; - UINTN AlignedAddress; - UINTN AlignmentMask; - - // - // Alignment must be a power of two or zero. - // - ASSERT ((Alignment & (Alignment - 1)) == 0); - - if (Alignment == 0) { - AlignmentMask = Alignment; - } else { - AlignmentMask = Alignment - 1; - } - - // - // Make sure that AllocationSize plus AlignmentMask does not overflow. - // - ASSERT (AllocationSize <= (MAX_ADDRESS - AlignmentMask)); - - RawAddress = AllocatePool (AllocationSize + AlignmentMask); - - AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask; - - return (VOID *) AlignedAddress; -} - -/** - Allocates a buffer of type EfiRuntimeServicesData at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedRuntimePool ( - IN UINTN AllocationSize, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedPool (EfiRuntimeServicesData, AllocationSize, Alignment); -} - -/** - Allocates a buffer of type EfieservedMemoryType at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedReservedPool ( - IN UINTN AllocationSize, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedPool (EfiReservedMemoryType, AllocationSize, Alignment); -} + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). -/** - Allocates and zeros a buffer of a certain pool type at a specified alignment. - - Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment - specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated - buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not - enough memory at the specified alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param PoolType The type of pool to allocate. - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. + @param PoolType The type of pool to allocate. + @param OldSize The size, in bytes, of OldBuffer. + @param NewSize The size, in bytes, of the buffer to reallocate. + @param OldBuffer The buffer to copy to the allocated buffer. This is an + optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. **/ VOID * -InternalAllocateAlignedZeroPool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN AllocationSize, - IN UINTN Alignment +InternalReallocatePool ( + IN EFI_MEMORY_TYPE PoolType, + IN UINTN OldSize, + IN UINTN NewSize, + IN VOID *OldBuffer OPTIONAL ) { - VOID *Memory; + VOID *NewBuffer; - Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment); - if (Memory != NULL) { - Memory = ZeroMem (Memory, AllocationSize); + NewBuffer = InternalAllocateZeroPool (PoolType, NewSize); + if (NewBuffer != NULL && OldBuffer != NULL) { + CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize)); + FreePool (OldBuffer); } - return Memory; + return NewBuffer; } /** - Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment. + Reallocates a buffer of type EfiBootServicesData. - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an - alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory at the specified alignment remaining to satisfy the request, then NULL is - returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + Allocates and zeros the number bytes specified by NewSize from memory of type + EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + enough memory remaining to satisfy the request, then NULL is returned. + + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. + @param OldSize The size, in bytes, of OldBuffer. + @param NewSize The size, in bytes, of the buffer to reallocate. + @param OldBuffer The buffer to copy to the allocated buffer. This is an optional + parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. **/ VOID * EFIAPI -AllocateAlignedZeroPool ( - IN UINTN AllocationSize, - IN UINTN Alignment +ReallocatePool ( + IN UINTN OldSize, + IN UINTN NewSize, + IN VOID *OldBuffer OPTIONAL ) { - VOID *Memory; - - Memory = AllocateAlignedPool (AllocationSize, Alignment); - if (Memory != NULL) { - Memory = ZeroMem (Memory, AllocationSize); - } - return Memory; + return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer); } /** - Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment. + Reallocates a buffer of type EfiRuntimeServicesData. - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an - alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory at the specified alignment remaining to satisfy the request, then NULL is - returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + Allocates and zeros the number bytes specified by NewSize from memory of type + EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + enough memory remaining to satisfy the request, then NULL is returned. - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedRuntimeZeroPool ( - IN UINTN AllocationSize, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData, AllocationSize, Alignment); -} - -/** - Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an - alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the - allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there - is not enough memory at the specified alignment remaining to satisfy the request, then NULL is - returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. + @param OldSize The size, in bytes, of OldBuffer. + @param NewSize The size, in bytes, of the buffer to reallocate. + @param OldBuffer The buffer to copy to the allocated buffer. This is an optional + parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. **/ VOID * EFIAPI -AllocateAlignedReservedZeroPool ( - IN UINTN AllocationSize, - IN UINTN Alignment +ReallocateRuntimePool ( + IN UINTN OldSize, + IN UINTN NewSize, + IN VOID *OldBuffer OPTIONAL ) { - return InternalAllocateAlignedZeroPool (EfiReservedMemoryType, AllocationSize, Alignment); + return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer); } /** - Copies a buffer to an allocated buffer of a certain pool type at a specified alignment. + Reallocates a buffer of type EfiReservedMemoryType. - Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment - specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid - buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining - to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). + Allocates and zeros the number bytes specified by NewSize from memory of type + EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and + NewSize bytes are copied from OldBuffer to the newly allocated buffer, and + OldBuffer is freed. A pointer to the newly allocated buffer is returned. + If NewSize is 0, then a valid buffer of 0 size is returned. If there is not + enough memory remaining to satisfy the request, then NULL is returned. - @param PoolType The type of pool to allocate. - @param AllocationSize The number of bytes to allocate. - @param Buffer The buffer to copy to the allocated buffer. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. + If the allocation of the new buffer is successful and the smaller of NewSize and OldSize + is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). -**/ -VOID * -InternalAllocateAlignedCopyPool ( - IN EFI_MEMORY_TYPE PoolType, - IN UINTN AllocationSize, - IN CONST VOID *Buffer, - IN UINTN Alignment - ) -{ - VOID *Memory; - - ASSERT (Buffer != NULL); - ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); - - Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment); - if (Memory != NULL) { - Memory = CopyMem (Memory, Buffer, AllocationSize); - } - return Memory; -} - -/** - Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Buffer The buffer to copy to the allocated buffer. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer, - IN UINTN Alignment - ) -{ - VOID *Memory; - - ASSERT (Buffer != NULL); - ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1)); - - Memory = AllocateAlignedPool (AllocationSize, Alignment); - if (Memory != NULL) { - Memory = CopyMem (Memory, Buffer, AllocationSize); - } - return Memory; -} - -/** - Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Buffer The buffer to copy to the allocated buffer. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. + @param OldSize The size, in bytes, of OldBuffer. + @param NewSize The size, in bytes, of the buffer to reallocate. + @param OldBuffer The buffer to copy to the allocated buffer. This is an + optional parameter that may be NULL. @return A pointer to the allocated buffer or NULL if allocation fails. **/ VOID * EFIAPI -AllocateAlignedRuntimeCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer, - IN UINTN Alignment +ReallocateReservedPool ( + IN UINTN OldSize, + IN UINTN NewSize, + IN VOID *OldBuffer OPTIONAL ) { - return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer, Alignment); + return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer); } /** - Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment. - - Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an - alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, - then a valid buffer of 0 size is returned. If there is not enough memory at the specified - alignment remaining to satisfy the request, then NULL is returned. - If Alignment is not a power of two and Alignment is not zero, then ASSERT(). - - @param AllocationSize The number of bytes to allocate. - @param Buffer The buffer to copy to the allocated buffer. - @param Alignment The requested alignment of the allocation. Must be a power of two. - If Alignment is zero, then byte alignment is used. - - @return A pointer to the allocated buffer or NULL if allocation fails. - -**/ -VOID * -EFIAPI -AllocateAlignedReservedCopyPool ( - IN UINTN AllocationSize, - IN CONST VOID *Buffer, - IN UINTN Alignment - ) -{ - return InternalAllocateAlignedCopyPool (EfiReservedMemoryType, AllocationSize, Buffer, Alignment); -} - -/** - Frees a buffer that was previously allocated with one of the aligned pool allocation functions - in the Memory Allocation Library. + Frees a buffer that was previously allocated with one of the pool allocation functions in the + Memory Allocation Library. Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the - aligned pool allocation services of the Memory Allocation Library. - If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation - Library, then ASSERT(). + pool allocation services of the Memory Allocation Library. If it is not possible to free pool + resources, then this function will perform no actions. + + If Buffer was not allocated with a pool allocation function in the Memory Allocation Library, + then ASSERT(). - @param Buffer Pointer to the buffer to free. + @param Buffer The pointer to the buffer to free. **/ VOID EFIAPI -FreeAlignedPool ( +FreePool ( IN VOID *Buffer ) { @@ -1079,3 +844,5 @@ FreeAlignedPool ( // PEI phase does not support to free pool, so leave it as NOP. // } + +