From 8e4b3d32a8baedd7e39d4e82b4e8458e217e7f6e Mon Sep 17 00:00:00 2001 From: andrewfish Date: Sat, 25 Jun 2011 01:39:09 +0000 Subject: [PATCH] InOsEmuPkg: Make the Guard MemoryAllocationLib handle EFI allocated pages correctly. signed-off-by: andrewfish git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11887 6f19259b-4bc3-4df7-8a09-765794883524 --- .../MemoryAllocationLib.c | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/InOsEmuPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c b/InOsEmuPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c index 1963c02a07..c457e7c213 100644 --- a/InOsEmuPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c +++ b/InOsEmuPkg/Library/GuardUefiMemoryAllocationLib/MemoryAllocationLib.c @@ -150,8 +150,16 @@ FreePages ( IN UINTN Pages ) { + EFI_STATUS Status; + ASSERT (Pages != 0); - FreePool (Buffer); + if (!gEmuThunk->Free (Buffer)) { + // The Free thunk will not free memory allocated in emulated EFI memory. + // The assmuption is this was allocated directly by EFI. We need this as some + // times protocols or EFI BootServices can return dynamically allocated buffers. + Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) Buffer, Pages); + ASSERT_EFI_ERROR (Status); + } } /** @@ -178,6 +186,7 @@ InternalAllocateAlignedPages ( IN UINTN Alignment ) { + EFI_STATUS Status; VOID *Memory; UINTN AlignedMemory; UINTN AlignmentMask; @@ -213,7 +222,7 @@ InternalAllocateAlignedPages ( // // Free first unaligned page(s). // - FreePool (Memory); + FreePages (Memory, UnalignedPages); } Memory = (VOID *) (AlignedMemory + EFI_PAGES_TO_SIZE (Pages)); UnalignedPages = RealPages - Pages - UnalignedPages; @@ -221,7 +230,7 @@ InternalAllocateAlignedPages ( // // Free last unaligned page(s). // - FreePool (Memory); + FreePages (Memory, UnalignedPages); } } else { // @@ -341,9 +350,7 @@ FreeAlignedPages ( IN UINTN Pages ) { - ASSERT (Pages != 0); - - FreePool (Buffer); + FreePages (Buffer, Pages); } /** @@ -799,13 +806,14 @@ FreePool ( IN VOID *Buffer ) { - EFI_STATUS Status; - + EFI_STATUS Status; + if (!gEmuThunk->Free (Buffer)) { // The Free thunk will not free memory allocated in emulated EFI memory. // The assmuption is this was allocated directly by EFI. We need this as some // times protocols or EFI BootServices can return dynamically allocated buffers. - gBS->FreePool (Buffer); + Status = gBS->FreePool (Buffer); + ASSERT_EFI_ERROR (Status); } } -- 2.39.2