1 /*++ @file 2 3 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR> 4 5 This program and the accompanying materials 6 are licensed and made available under the terms and conditions of the BSD License 7 which accompanies this distribution. The full text of the license may be found at 8 http://opensource.org/licenses/bsd-license.php 9 10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 12 13 **/ 14 15 #include "Base.h" 16 #include "Library/BaseMemoryLib.h" 17 #include "Library/MemoryAllocationLib.h" 18 19 #include <stdlib.h> 20 21 /** 22 Allocates a buffer of type EfiBootServicesData. 23 24 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a 25 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is 26 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned. 27 28 @param AllocationSize The number of bytes to allocate. 29 30 @return A pointer to the allocated buffer or NULL if allocation fails. 31 32 **/ 33 VOID * 34 EFIAPI 35 AllocatePool ( 36 IN UINTN AllocationSize 37 ) 38 { 39 return (VOID*) malloc (AllocationSize); 40 } 41 42 43 /** 44 Allocates and zeros a buffer of type EfiBootServicesData. 45 46 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the 47 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a 48 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the 49 request, then NULL is returned. 50 51 @param AllocationSize The number of bytes to allocate and zero. 52 53 @return A pointer to the allocated buffer or NULL if allocation fails. 54 55 **/ 56 VOID * 57 EFIAPI 58 AllocateZeroPool ( 59 IN UINTN AllocationSize 60 ) 61 { 62 VOID *Buffer; 63 64 Buffer = AllocatePool (AllocationSize); 65 if (Buffer == NULL) { 66 return NULL; 67 } 68 69 ZeroMem (Buffer, AllocationSize); 70 71 return Buffer; 72 } 73 74 75 /** 76 Reallocates a buffer of type EfiBootServicesData. 77 78 Allocates and zeros the number bytes specified by NewSize from memory of type 79 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and 80 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and 81 OldBuffer is freed. A pointer to the newly allocated buffer is returned. 82 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not 83 enough memory remaining to satisfy the request, then NULL is returned. 84 85 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize 86 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT(). 87 88 @param OldSize The size, in bytes, of OldBuffer. 89 @param NewSize The size, in bytes, of the buffer to reallocate. 90 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional 91 parameter that may be NULL. 92 93 @return A pointer to the allocated buffer or NULL if allocation fails. 94 95 **/ 96 VOID * 97 EFIAPI 98 ReallocatePool ( 99 IN UINTN OldSize, 100 IN UINTN NewSize, 101 IN VOID *OldBuffer OPTIONAL 102 ) 103 { 104 VOID *NewBuffer; 105 106 NewBuffer = AllocatePool (NewSize); 107 if (NewBuffer == NULL) { 108 return NULL; 109 } 110 111 if (OldBuffer != NULL) { 112 if (OldSize > 0) { 113 CopyMem (NewBuffer, OldBuffer, OldSize); 114 } 115 116 FreePool (OldBuffer); 117 } 118 119 return NewBuffer; 120 } 121 122 123 /** 124 Frees a buffer that was previously allocated with one of the pool allocation functions in the 125 Memory Allocation Library. 126 127 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the 128 pool allocation services of the Memory Allocation Library. If it is not possible to free pool 129 resources, then this function will perform no actions. 130 131 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library, 132 then ASSERT(). 133 134 @param Buffer Pointer to the buffer to free. 135 136 **/ 137 VOID 138 EFIAPI 139 FreePool ( 140 IN VOID *Buffer 141 ) 142 { 143 free ((void *) Buffer); 144 } 145 146