From b8800fc547440d8648e49c189a60547bfc4a1615 Mon Sep 17 00:00:00 2001 From: jljusten Date: Mon, 20 Jun 2011 21:58:05 +0000 Subject: InOsEmuPkg/Unix/Sec: Add BaseMemoryLib implementation to thunk layer This implementation only covers a few BaseMemoryLib functions. The implementation leverates malloc/free in stdlib. Signed-off-by: jljusten git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11866 6f19259b-4bc3-4df7-8a09-765794883524 --- InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c | 145 ++++++++++++++++++++++++++++++ InOsEmuPkg/Unix/Sec/SecMain.c | 14 +-- InOsEmuPkg/Unix/Sec/SecMain.h | 1 + InOsEmuPkg/Unix/Sec/SecMain.inf | 5 +- 4 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c (limited to 'InOsEmuPkg/Unix/Sec') diff --git a/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c b/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c new file mode 100644 index 0000000..76b7271 --- /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 e22f5b9..86554ed 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 81970ed..01254de 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 e3ceb99..4dc0a67 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 -- cgit v1.1