+++ /dev/null
-/** @file\r
- Definitions for memory allocation routines: calloc, malloc, realloc, free.\r
-\r
- The order and contiguity of storage allocated by successive calls to the\r
- calloc, malloc, and realloc functions is unspecified. The pointer returned\r
- if the allocation succeeds is suitably aligned so that it may be assigned to\r
- a pointer of any type of object and then used to access such an object or an\r
- array of such objects in the space allocated (until the space is explicitly\r
- freed or reallocated). Each such allocation shall yield a pointer to an\r
- object disjoint from any other object. The pointer returned points to the\r
- start (lowest byte address) of the allocated space. If the space can not be\r
- allocated, a null pointer is returned. If the size of the space requested\r
- is zero, the behavior is implementation-defined; the value returned shall be\r
- either a null pointer or a unique pointer. The value of a pointer that\r
- refers to freed space is indeterminate.\r
-\r
- Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
- This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
- */\r
-#include <Uefi.h>\r
-#include <Library/MemoryAllocationLib.h>\r
-#include <Library/UefiBootServicesTableLib.h>\r
-#include <Library/BaseLib.h>\r
-#include <Library/BaseMemoryLib.h>\r
-#include <Library/DebugLib.h>\r
-\r
-#include <LibConfig.h>\r
-\r
-#include <assert.h>\r
-#include <stdlib.h>\r
-#include <errno.h>\r
-\r
-#define CPOOL_HEAD_SIGNATURE SIGNATURE_32('C','p','h','d')\r
-\r
-/** The UEFI functions do not provide a way to determine the size of an\r
- allocated region of memory given just a pointer to the start of that\r
- region. Since this is required for the implementation of realloc,\r
- the memory head structure, CPOOL_HEAD, containing the necessary\r
- information is prepended to the requested space.\r
-\r
- The order of members is important. This structure is 8-byte aligned,\r
- as per the UEFI specification for memory allocation functions. By\r
- specifying Size as a 64-bit value and placing it immediately before\r
- Data, it ensures that Data will always be 8-byte aligned.\r
-\r
- On IA32 systems, this structure is 24 bytes long, excluding Data.\r
- On X64 systems, this structure is 32 bytes long, excluding Data.\r
-**/\r
-typedef struct {\r
- LIST_ENTRY List;\r
- UINT32 Signature;\r
- UINT64 Size;\r
- CHAR8 Data[1];\r
-} CPOOL_HEAD;\r
-\r
-// List of memory allocated by malloc/calloc/etc.\r
-static LIST_ENTRY MemPoolHead = INITIALIZE_LIST_HEAD_VARIABLE(MemPoolHead);\r
-\r
-/****************************/\r
-\r
-/** The malloc function allocates space for an object whose size is specified\r
- by size and whose value is indeterminate.\r
-\r
- This implementation uses the UEFI memory allocation boot services to get a\r
- region of memory that is 8-byte aligned and of the specified size. The\r
- region is allocated with type EfiLoaderData.\r
-\r
- @param size Size, in bytes, of the region to allocate.\r
-\r
- @return NULL is returned if the space could not be allocated and errno\r
- contains the cause. Otherwise, a pointer to an 8-byte aligned\r
- region of the requested size is returned.<BR>\r
- If NULL is returned, errno may contain:\r
- - EINVAL: Requested Size is zero.\r
- - ENOMEM: Memory could not be allocated.\r
-**/\r
-void *\r
-malloc(size_t Size)\r
-{\r
- CPOOL_HEAD *Head;\r
- void *RetVal;\r
- EFI_STATUS Status;\r
- UINTN NodeSize;\r
-\r
- if( Size == 0) {\r
- errno = EINVAL; // Make errno diffenent, just in case of a lingering ENOMEM.\r
- DEBUG((DEBUG_ERROR, "ERROR malloc: Zero Size\n"));\r
- return NULL;\r
- }\r
-\r
- NodeSize = (UINTN)(Size + sizeof(CPOOL_HEAD));\r
-\r
- DEBUG((DEBUG_POOL, "malloc(%d): NodeSz: %d", Size, NodeSize));\r
-\r
- Status = gBS->AllocatePool( EfiLoaderData, NodeSize, (void**)&Head);\r
- if( Status != EFI_SUCCESS) {\r
- RetVal = NULL;\r
- errno = ENOMEM;\r
- DEBUG((DEBUG_ERROR, "\nERROR malloc: AllocatePool returned %r\n", Status));\r
- }\r
- else {\r
- assert(Head != NULL);\r
- // Fill out the pool header\r
- Head->Signature = CPOOL_HEAD_SIGNATURE;\r
- Head->Size = NodeSize;\r
-\r
- // Add this node to the list\r
- (void)InsertTailList(&MemPoolHead, (LIST_ENTRY *)Head);\r
-\r
- // Return a pointer to the data\r
- RetVal = (void*)Head->Data;\r
- DEBUG((DEBUG_POOL, " Head: %p, Returns %p\n", Head, RetVal));\r
- }\r
-\r
- return RetVal;\r
-}\r
-\r
-/** The calloc function allocates space for an array of Num objects, each of\r
- whose size is Size. The space is initialized to all bits zero.\r
-\r
- This implementation uses the UEFI memory allocation boot services to get a\r
- region of memory that is 8-byte aligned and of the specified size. The\r
- region is allocated with type EfiLoaderData.\r
-\r
- @param Num Number of objects to allocate.\r
- @param Size Size, in bytes, of the objects to allocate space for.\r
-\r
- @return NULL is returned if the space could not be allocated and errno\r
- contains the cause. Otherwise, a pointer to an 8-byte aligned\r
- region of the requested size is returned.\r
-**/\r
-void *\r
-calloc(size_t Num, size_t Size)\r
-{\r
- void *RetVal;\r
- size_t NumSize;\r
-\r
- NumSize = Num * Size;\r
- RetVal = NULL;\r
- if (NumSize != 0) {\r
- RetVal = malloc(NumSize);\r
- if( RetVal != NULL) {\r
- (VOID)ZeroMem( RetVal, NumSize);\r
- }\r
- }\r
- DEBUG((DEBUG_POOL, "0x%p = calloc(%d, %d)\n", RetVal, Num, Size));\r
-\r
- return RetVal;\r
-}\r
-\r
-/** The free function causes the space pointed to by Ptr to be deallocated,\r
- that is, made available for further allocation.\r
-\r
- If Ptr is a null pointer, no action occurs. Otherwise, if the argument\r
- does not match a pointer earlier returned by the calloc, malloc, or realloc\r
- function, or if the space has been deallocated by a call to free or\r
- realloc, the behavior is undefined.\r
-\r
- @param Ptr Pointer to a previously allocated region of memory to be freed.\r
-\r
-**/\r
-void\r
-free(void *Ptr)\r
-{\r
- CPOOL_HEAD *Head;\r
-\r
- Head = BASE_CR(Ptr, CPOOL_HEAD, Data);\r
- assert(Head != NULL);\r
- DEBUG((DEBUG_POOL, "free(%p): Head: %p\n", Ptr, Head));\r
-\r
- if(Ptr != NULL) {\r
- if (Head->Signature == CPOOL_HEAD_SIGNATURE) {\r
- (void) RemoveEntryList((LIST_ENTRY *)Head); // Remove this node from the malloc pool\r
- (void) gBS->FreePool (Head); // Now free the associated memory\r
- }\r
- else {\r
- errno = EFAULT;\r
- DEBUG((DEBUG_ERROR, "ERROR free(0x%p): Signature is 0x%8X, expected 0x%8X\n",\r
- Ptr, Head->Signature, CPOOL_HEAD_SIGNATURE));\r
- }\r
- }\r
- DEBUG((DEBUG_POOL, "free Done\n"));\r
-}\r
-\r
-/** The realloc function changes the size of the object pointed to by Ptr to\r
- the size specified by NewSize.\r
-\r
- The contents of the object are unchanged up to the lesser of the new and\r
- old sizes. If the new size is larger, the value of the newly allocated\r
- portion of the object is indeterminate.\r
-\r
- If Ptr is a null pointer, the realloc function behaves like the malloc\r
- function for the specified size.\r
-\r
- If Ptr does not match a pointer earlier returned by the calloc, malloc, or\r
- realloc function, or if the space has been deallocated by a call to the free\r
- or realloc function, the behavior is undefined.\r
-\r
- If the space cannot be allocated, the object pointed to by Ptr is unchanged.\r
-\r
- If NewSize is zero and Ptr is not a null pointer, the object it points to\r
- is freed.\r
-\r
- This implementation uses the UEFI memory allocation boot services to get a\r
- region of memory that is 8-byte aligned and of the specified size. The\r
- region is allocated with type EfiLoaderData.\r
-\r
- The following combinations of Ptr and NewSize can occur:<BR>\r
- Ptr NewSize<BR>\r
- -------- -------------------<BR>\r
- - NULL 0 Returns NULL;\r
- - NULL > 0 Same as malloc(NewSize)\r
- - invalid X Returns NULL;\r
- - valid NewSize >= OldSize Returns malloc(NewSize) with Oldsize bytes copied from Ptr\r
- - valid NewSize < OldSize Returns new buffer with Oldsize bytes copied from Ptr\r
- - valid 0 Return NULL. Frees Ptr.\r
-\r
-\r
- @param Ptr Pointer to a previously allocated region of memory to be resized.\r
- @param NewSize Size, in bytes, of the new object to allocate space for.\r
-\r
- @return NULL is returned if the space could not be allocated and errno\r
- contains the cause. Otherwise, a pointer to an 8-byte aligned\r
- region of the requested size is returned. If NewSize is zero,\r
- NULL is returned and errno will be unchanged.\r
-**/\r
-void *\r
-realloc(void *Ptr, size_t ReqSize)\r
-{\r
- void *RetVal = NULL;\r
- CPOOL_HEAD *Head = NULL;\r
- size_t OldSize = 0;\r
- size_t NewSize;\r
- size_t NumCpy;\r
-\r
- // Find out the size of the OLD memory region\r
- if( Ptr != NULL) {\r
- Head = BASE_CR (Ptr, CPOOL_HEAD, Data);\r
- assert(Head != NULL);\r
- if (Head->Signature != CPOOL_HEAD_SIGNATURE) {\r
- errno = EFAULT;\r
- DEBUG((DEBUG_ERROR, "ERROR realloc(0x%p): Signature is 0x%8X, expected 0x%8X\n",\r
- Ptr, Head->Signature, CPOOL_HEAD_SIGNATURE));\r
- return NULL;\r
- }\r
- OldSize = (size_t)Head->Size;\r
- }\r
-\r
- // At this point, Ptr is either NULL or a valid pointer to an allocated space\r
- NewSize = (size_t)(ReqSize + (sizeof(CPOOL_HEAD)));\r
-\r
- if( ReqSize > 0) {\r
- RetVal = malloc(NewSize); // Get the NEW memory region\r
- if( Ptr != NULL) { // If there is an OLD region...\r
- if( RetVal != NULL) { // and the NEW region was successfully allocated\r
- NumCpy = OldSize;\r
- if( OldSize > NewSize) {\r
- NumCpy = NewSize;\r
- }\r
- (VOID)CopyMem( RetVal, Ptr, NumCpy); // Copy old data to the new region.\r
- free( Ptr); // and reclaim the old region.\r
- }\r
- else {\r
- errno = ENOMEM;\r
- }\r
- }\r
- }\r
- else {\r
- free( Ptr); // Reclaim the old region.\r
- }\r
- DEBUG((DEBUG_POOL, "0x%p = realloc(%p, %d): Head: %p NewSz: %d\n",\r
- RetVal, Ptr, ReqSize, Head, NewSize));\r
-\r
- return RetVal;\r
-}\r