Emulator Thunk to abstract OS services from pure EFI code\r
\r
Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>\r
+ Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
\r
#include <PiPei.h>\r
#include <Library/BaseLib.h>\r
-\r
-#include <stdlib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
\r
\r
UINTN gThunkPpiListSize = 0;\r
)\r
{\r
UINTN Index;\r
- \r
- gThunkPpiList = realloc (gThunkPpiList, gThunkPpiListSize + sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
+\r
+ gThunkPpiList = ReallocatePool (\r
+ gThunkPpiListSize,\r
+ gThunkPpiListSize + sizeof (EFI_PEI_PPI_DESCRIPTOR),\r
+ gThunkPpiList\r
+ );\r
if (gThunkPpiList == NULL) {\r
return EFI_OUT_OF_RESOURCES;\r
}\r
Emulator Thunk to abstract OS services from pure EFI code\r
\r
Copyright (c) 2008 - 2011, Apple Inc. All rights reserved.<BR>\r
+ Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
#include <Library/BaseLib.h>\r
#include <Library/BaseMemoryLib.h>\r
#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
\r
#include <Protocol/EmuIoThunk.h>\r
\r
-#include <stdlib.h>\r
-\r
\r
#define EMU_IO_THUNK_PROTOCOL_DATA_SIGNATURE SIGNATURE_32('E','m','u','T')\r
\r
}\r
\r
Instance = 0;\r
- StartString = malloc (StrSize (ConfigString));\r
+ StartString = AllocatePool (StrSize (ConfigString));\r
StrCpy (StartString, ConfigString);\r
while (*StartString != '\0') {\r
\r
SubString++;\r
}\r
\r
- Private = malloc (sizeof (EMU_IO_THUNK_PROTOCOL_DATA));\r
+ Private = AllocatePool (sizeof (EMU_IO_THUNK_PROTOCOL_DATA));\r
if (Private == NULL) {\r
return EFI_OUT_OF_RESOURCES;\r
}\r
--- /dev/null
+/*++ @file
+
+ Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
+
+ 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 <stdlib.h>
+
+/**
+ 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);
+}
+
// 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
// 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);
// 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);
//
// 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);
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;
#include <Library/PeCoffLib.h>\r
#include <Library/BaseLib.h>\r
#include <Library/BaseMemoryLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
#include <Library/PrintLib.h>\r
#include <Library/PcdLib.h>\r
#include <Library/DebugLib.h>\r
Pthreads.c\r
PosixFileSystem.c\r
BlockIo.c\r
- LinuxPacketFilter.c \r
- BerkeleyPacketFilter.c \r
+ LinuxPacketFilter.c\r
+ BerkeleyPacketFilter.c\r
+ MemoryAllocationLib.c\r
\r
[Sources.X64]\r
X64/Gasket.S # convert between Emu x86_64 ABI and EFI X64 ABI\r