]> git.proxmox.com Git - mirror_edk2.git/commitdiff
InOsEmuPkg/Unix/Sec: Add BaseMemoryLib implementation to thunk layer
authorjljusten <jljusten@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 20 Jun 2011 21:58:05 +0000 (21:58 +0000)
committerjljusten <jljusten@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 20 Jun 2011 21:58:05 +0000 (21:58 +0000)
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/Library/ThunkPpiList/ThunkPpiList.c
InOsEmuPkg/Library/ThunkProtocolList/ThunkProtocolList.c
InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c [new file with mode: 0644]
InOsEmuPkg/Unix/Sec/SecMain.c
InOsEmuPkg/Unix/Sec/SecMain.h
InOsEmuPkg/Unix/Sec/SecMain.inf

index 3531b84bf5c7e2ad01ad8e0f941de10250c19772..3c2fd93e7faf7e5c93d1254559e28e9dabaa3b51 100644 (file)
@@ -2,6 +2,7 @@
   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
@@ -15,8 +16,7 @@
 \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
@@ -51,8 +51,12 @@ AddThunkPpi (
   )\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
index 3c53716765a237b1b56d6cd4f4c0e8a0b70b4a95..5aed594b6ed6f912d18a4583cc4c47d70a0a747a 100644 (file)
@@ -2,6 +2,7 @@
   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
@@ -53,7 +53,7 @@ AddThunkProtocol (
   }\r
   \r
   Instance = 0;\r
-  StartString = malloc (StrSize (ConfigString));\r
+  StartString = AllocatePool (StrSize (ConfigString));\r
   StrCpy (StartString, ConfigString);\r
   while (*StartString != '\0') {\r
 \r
@@ -74,7 +74,7 @@ AddThunkProtocol (
       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
diff --git a/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c b/InOsEmuPkg/Unix/Sec/MemoryAllocationLib.c
new file mode 100644 (file)
index 0000000..76b7271
--- /dev/null
@@ -0,0 +1,145 @@
+/*++ @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);
+}
+
index e22f5b9f871ecd811344aa1242a59c76cff000ff..86554ed8d615903d51d27cd731e5424313860c11 100644 (file)
@@ -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;
index 81970edb6c2d24d7663a7d7793f40c92173b83d9..01254de0c7565e39333d0487e9c4c93b46c6c7e0 100644 (file)
@@ -90,6 +90,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #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
index e3ceb99ef96e8660dfd7d7897cc63a38888d590b..4dc0a67008f4f745fd25442d0b2d3ef3779d6053 100644 (file)
@@ -35,8 +35,9 @@
   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