diff --git a/InOsEmuPkg/Library/ThunkPpiList/ThunkPpiList.c b/InOsEmuPkg/Library/ThunkPpiList/ThunkPpiList.c index 3531b84bf5..3c2fd93e7f 100644 --- a/InOsEmuPkg/Library/ThunkPpiList/ThunkPpiList.c +++ b/InOsEmuPkg/Library/ThunkPpiList/ThunkPpiList.c @@ -2,6 +2,7 @@ Emulator Thunk to abstract OS services from pure EFI code Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.
+ Copyright (c) 2011, 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 @@ -15,8 +16,7 @@ #include #include - -#include +#include UINTN gThunkPpiListSize = 0; @@ -51,8 +51,12 @@ AddThunkPpi ( ) { UINTN Index; - - gThunkPpiList = realloc (gThunkPpiList, gThunkPpiListSize + sizeof (EFI_PEI_PPI_DESCRIPTOR)); + + gThunkPpiList = ReallocatePool ( + gThunkPpiListSize, + gThunkPpiListSize + sizeof (EFI_PEI_PPI_DESCRIPTOR), + gThunkPpiList + ); if (gThunkPpiList == NULL) { return EFI_OUT_OF_RESOURCES; } diff --git a/InOsEmuPkg/Library/ThunkProtocolList/ThunkProtocolList.c b/InOsEmuPkg/Library/ThunkProtocolList/ThunkProtocolList.c index 3c53716765..5aed594b6e 100644 --- a/InOsEmuPkg/Library/ThunkProtocolList/ThunkProtocolList.c +++ b/InOsEmuPkg/Library/ThunkProtocolList/ThunkProtocolList.c @@ -2,6 +2,7 @@ Emulator Thunk to abstract OS services from pure EFI code Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.
+ Copyright (c) 2011, 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 @@ -17,11 +18,10 @@ #include #include #include +#include #include -#include - #define EMU_IO_THUNK_PROTOCOL_DATA_SIGNATURE SIGNATURE_32('E','m','u','T') @@ -53,7 +53,7 @@ AddThunkProtocol ( } Instance = 0; - StartString = malloc (StrSize (ConfigString)); + StartString = AllocatePool (StrSize (ConfigString)); StrCpy (StartString, ConfigString); while (*StartString != '\0') { @@ -74,7 +74,7 @@ AddThunkProtocol ( SubString++; } - Private = malloc (sizeof (EMU_IO_THUNK_PROTOCOL_DATA)); + Private = AllocatePool (sizeof (EMU_IO_THUNK_PROTOCOL_DATA)); if (Private == NULL) { return EFI_OUT_OF_RESOURCES; } diff --git a/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c b/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c new file mode 100644 index 0000000000..76b7271872 --- /dev/null +++ b/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c @@ -0,0 +1,145 @@ +/*++ @file + + Copyright (c) 2011, 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 + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include "Base.h" +#include "Library/BaseMemoryLib.h" +#include "Library/MemoryAllocationLib.h" + +#include + +/** + Allocates a buffer of type EfiBootServicesData. + + Allocates the number bytes specified by AllocationSize of type EfiBootServicesData 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. + + @param AllocationSize The number of bytes to allocate. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +EFIAPI +AllocatePool ( + IN UINTN AllocationSize + ) +{ + return (VOID*) malloc (AllocationSize); +} + + +/** + Allocates and zeros a buffer of type EfiBootServicesData. + + Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, 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. + + @param AllocationSize The number of bytes to allocate and zero. + + @return A pointer to the allocated buffer or NULL if allocation fails. + +**/ +VOID * +EFIAPI +AllocateZeroPool ( + IN UINTN AllocationSize + ) +{ + VOID *Buffer; + + Buffer = AllocatePool (AllocationSize); + if (Buffer == NULL) { + return NULL; + } + + ZeroMem (Buffer, AllocationSize); + + return Buffer; +} + + +/** + Reallocates a buffer of type EfiBootServicesData. + + 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 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 +ReallocatePool ( + IN UINTN OldSize, + IN UINTN NewSize, + IN VOID *OldBuffer OPTIONAL + ) +{ + VOID *NewBuffer; + + NewBuffer = AllocatePool (NewSize); + if (NewBuffer == NULL) { + return NULL; + } + + if (OldBuffer != NULL) { + if (OldSize > 0) { + CopyMem (NewBuffer, OldBuffer, OldSize); + } + + FreePool (OldBuffer); + } + + return NewBuffer; +} + + +/** + 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 + 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. + +**/ +VOID +EFIAPI +FreePool ( + IN VOID *Buffer + ) +{ + free ((void *) Buffer); +} + diff --git a/InOsEmuPkg/Unix/Sec/SecMain.c b/InOsEmuPkg/Unix/Sec/SecMain.c index e22f5b9f87..86554ed8d6 100644 --- a/InOsEmuPkg/Unix/Sec/SecMain.c +++ b/InOsEmuPkg/Unix/Sec/SecMain.c @@ -136,7 +136,7 @@ main ( // symbols when we load every PE/COFF image. // Index = strlen (*Argv); - gGdbWorkingFileName = malloc (Index + strlen(".gdb") + 1); + gGdbWorkingFileName = AllocatePool (Index + strlen(".gdb") + 1); strcpy (gGdbWorkingFileName, *Argv); strcat (gGdbWorkingFileName, ".gdb"); #endif @@ -146,7 +146,7 @@ main ( // Allocate space for gSystemMemory Array // gSystemMemoryCount = CountSeperatorsInString (MemorySizeStr, '!') + 1; - gSystemMemory = calloc (gSystemMemoryCount, sizeof (EMU_SYSTEM_MEMORY)); + gSystemMemory = AllocateZeroPool (gSystemMemoryCount * sizeof (EMU_SYSTEM_MEMORY)); if (gSystemMemory == NULL) { printf ("ERROR : Can not allocate memory for system. Exiting.\n"); exit (1); @@ -155,7 +155,7 @@ main ( // Allocate space for gSystemMemory Array // gFdInfoCount = CountSeperatorsInString (FirmwareVolumesStr, '!') + 1; - gFdInfo = calloc (gFdInfoCount, sizeof (EMU_FD_INFO)); + gFdInfo = AllocateZeroPool (gFdInfoCount * sizeof (EMU_FD_INFO)); if (gFdInfo == NULL) { printf ("ERROR : Can not allocate memory for fd info. Exiting.\n"); exit (1); @@ -192,7 +192,7 @@ main ( // // Open All the firmware volumes and remember the info in the gFdInfo global // - FileName = (CHAR8 *)malloc (StrLen (FirmwareVolumesStr) + 1); + FileName = (CHAR8 *) AllocatePool (StrLen (FirmwareVolumesStr) + 1); if (FileName == NULL) { printf ("ERROR : Can not allocate memory for firmware volume string\n"); exit (1); @@ -870,7 +870,11 @@ AddHandle ( PreviousSize = mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE); mImageContextModHandleArraySize += MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARRAY_SIZE; - mImageContextModHandleArray = realloc (mImageContextModHandleArray, mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE)); + mImageContextModHandleArray = ReallocatePool ( + (mImageContextModHandleArraySize - 1) * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE), + mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE), + mImageContextModHandleArray + ); if (mImageContextModHandleArray == NULL) { ASSERT (FALSE); return EFI_OUT_OF_RESOURCES; diff --git a/InOsEmuPkg/Unix/Sec/SecMain.h b/InOsEmuPkg/Unix/Sec/SecMain.h index 81970edb6c..01254de0c7 100644 --- a/InOsEmuPkg/Unix/Sec/SecMain.h +++ b/InOsEmuPkg/Unix/Sec/SecMain.h @@ -90,6 +90,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include #include +#include #include #include #include diff --git a/InOsEmuPkg/Unix/Sec/SecMain.inf b/InOsEmuPkg/Unix/Sec/SecMain.inf index e3ceb99ef9..4dc0a67008 100644 --- a/InOsEmuPkg/Unix/Sec/SecMain.inf +++ b/InOsEmuPkg/Unix/Sec/SecMain.inf @@ -35,8 +35,9 @@ Pthreads.c PosixFileSystem.c BlockIo.c - LinuxPacketFilter.c - BerkeleyPacketFilter.c + LinuxPacketFilter.c + BerkeleyPacketFilter.c + MemoryAllocationLib.c [Sources.X64] X64/Gasket.S # convert between Emu x86_64 ABI and EFI X64 ABI