]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/StdLib/Malloc.c
StdLib/LibC/StdLib/Malloc.c: Make the free() function conform to the ISO/IEC 9899...
[mirror_edk2.git] / StdLib / LibC / StdLib / Malloc.c
CommitLineData
2aa62f2b 1/** @file\r
2 Definitions for memory allocation routines: calloc, malloc, realloc, free.\r
3\r
4 The order and contiguity of storage allocated by successive calls to the\r
5 calloc, malloc, and realloc functions is unspecified. The pointer returned\r
6 if the allocation succeeds is suitably aligned so that it may be assigned to\r
7 a pointer of any type of object and then used to access such an object or an\r
8 array of such objects in the space allocated (until the space is explicitly\r
9 freed or reallocated). Each such allocation shall yield a pointer to an\r
10 object disjoint from any other object. The pointer returned points to the\r
11 start (lowest byte address) of the allocated space. If the space can not be\r
12 allocated, a null pointer is returned. If the size of the space requested\r
13 is zero, the behavior is implementation-defined; the value returned shall be\r
14 either a null pointer or a unique pointer. The value of a pointer that\r
15 refers to freed space is indeterminate.\r
16\r
17Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
18This program and the accompanying materials\r
19are licensed and made available under the terms and conditions of the BSD License\r
20which accompanies this distribution. The full text of the license may be found at\r
21http://opensource.org/licenses/bsd-license.php\r
22\r
23THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
24WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
25\r
26 */\r
27#include <Base.h>\r
28#include <Uefi.h>\r
29#include <Library/MemoryAllocationLib.h>\r
30#include <Library/UefiBootServicesTableLib.h>\r
31#include <Library/BaseMemoryLib.h>\r
32#include <Library/DebugLib.h>\r
33\r
34#include <LibConfig.h>\r
35\r
36#include <assert.h>\r
37#include <stdlib.h>\r
38#include <errno.h>\r
39\r
40/** The UEFI functions do not provide a way to determine the size of an\r
41 allocated region of memory given just a pointer to the start of that\r
42 region. Since this is required for the implementation of realloc,\r
43 the memory head structure from Core/Dxe/Mem/Pool.c has been reproduced\r
44 here.\r
45\r
46 NOTE: If the UEFI implementation is changed, the realloc function may cease\r
47 to function properly.\r
48**/\r
49#define POOL_HEAD_SIGNATURE SIGNATURE_32('p','h','d','0')\r
50typedef struct {\r
51 UINT32 Signature;\r
52 UINT32 Size;\r
53 EFI_MEMORY_TYPE Type;\r
54 UINTN Reserved;\r
55 CHAR8 Data[1];\r
56} POOL_HEAD;\r
57\r
58/****************************/\r
59\r
60/** The malloc function allocates space for an object whose size is specified\r
61 by size and whose value is indeterminate.\r
62\r
63 This implementation uses the UEFI memory allocation boot services to get a\r
64 region of memory that is 8-byte aligned and of the specified size. The\r
65 region is allocated with type EfiLoaderData.\r
66\r
67 @param size Size, in bytes, of the region to allocate.\r
68\r
69 @return NULL is returned if the space could not be allocated and errno\r
70 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
71 region of the requested size is returned.<BR>\r
72 If NULL is returned, errno may contain:\r
73 - EINVAL: Requested Size is zero.\r
74 - ENOMEM: Memory could not be allocated.\r
75**/\r
76void *\r
77malloc(size_t Size)\r
78{\r
79 void *RetVal;\r
80 EFI_STATUS Status;\r
81\r
82 if( Size == 0) {\r
83 errno = EINVAL; // Make errno diffenent, just in case of a lingering ENOMEM.\r
84 return NULL;\r
85 }\r
86\r
87 Status = gBS->AllocatePool( EfiLoaderData, (UINTN)Size, &RetVal);\r
88 if( Status != EFI_SUCCESS) {\r
89 RetVal = NULL;\r
90 errno = ENOMEM;\r
91 }\r
92 return RetVal;\r
93}\r
94\r
95/** The calloc function allocates space for an array of Num objects, each of\r
96 whose size is Size. The space is initialized to all bits zero.\r
97\r
98 This implementation uses the UEFI memory allocation boot services to get a\r
99 region of memory that is 8-byte aligned and of the specified size. The\r
100 region is allocated with type EfiLoaderData.\r
101\r
102 @param Num Number of objects to allocate.\r
103 @param Size Size, in bytes, of the objects to allocate space for.\r
104\r
105 @return NULL is returned if the space could not be allocated and errno\r
106 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
107 region of the requested size is returned.\r
108**/\r
109void *\r
110calloc(size_t Num, size_t Size)\r
111{\r
112 void *RetVal;\r
113 size_t NumSize;\r
114\r
115 NumSize = Num * Size;\r
116 if (NumSize == 0) {\r
117 return NULL;\r
118 }\r
119 RetVal = malloc(NumSize);\r
120 if( RetVal != NULL) {\r
121 (VOID)ZeroMem( RetVal, NumSize);\r
122 }\r
123 return RetVal;\r
124}\r
125\r
126/** The free function causes the space pointed to by Ptr to be deallocated,\r
127 that is, made available for further allocation.\r
128\r
129 If Ptr is a null pointer, no action occurs. Otherwise, if the argument\r
130 does not match a pointer earlier returned by the calloc, malloc, or realloc\r
131 function, or if the space has been deallocated by a call to free or\r
132 realloc, the behavior is undefined.\r
133\r
134 @param Ptr Pointer to a previously allocated region of memory to be freed.\r
135\r
136**/\r
137void\r
138free(void *Ptr)\r
139{\r
565b3c80 140 if(Ptr != NULL) {\r
141 (void) gBS->FreePool (Ptr);\r
142 }\r
2aa62f2b 143}\r
144\r
145/** The realloc function changes the size of the object pointed to by Ptr to\r
146 the size specified by NewSize.\r
147\r
148 The contents of the object are unchanged up to the lesser of the new and\r
149 old sizes. If the new size is larger, the value of the newly allocated\r
150 portion of the object is indeterminate.\r
151\r
152 If Ptr is a null pointer, the realloc function behaves like the malloc\r
153 function for the specified size.\r
154\r
155 If Ptr does not match a pointer earlier returned by the calloc, malloc, or\r
156 realloc function, or if the space has been deallocated by a call to the free\r
157 or realloc function, the behavior is undefined.\r
158\r
159 If the space cannot be allocated, the object pointed to by Ptr is unchanged.\r
160\r
161 If NewSize is zero and Ptr is not a null pointer, the object it points to\r
162 is freed.\r
163\r
164 This implementation uses the UEFI memory allocation boot services to get a\r
165 region of memory that is 8-byte aligned and of the specified size. The\r
166 region is allocated with type EfiLoaderData.\r
167\r
168 The following combinations of Ptr and NewSize can occur:<BR>\r
169 Ptr NewSize<BR>\r
170 -------- -------------------<BR>\r
171 - NULL 0 Returns NULL;\r
172 - NULL > 0 Same as malloc(NewSize)\r
173 - invalid X Returns NULL;\r
174 - valid NewSize >= OldSize Returns malloc(NewSize) with Oldsize bytes copied from Ptr\r
175 - valid NewSize < OldSize Returns new buffer with Oldsize bytes copied from Ptr\r
176 - valid 0 Return NULL. Frees Ptr.\r
177\r
178\r
179 @param Ptr Pointer to a previously allocated region of memory to be resized.\r
180 @param NewSize Size, in bytes, of the new object to allocate space for.\r
181\r
182 @return NULL is returned if the space could not be allocated and errno\r
183 contains the cause. Otherwise, a pointer to an 8-byte aligned\r
184 region of the requested size is returned. If NewSize is zero,\r
185 NULL is returned and errno will be unchanged.\r
186**/\r
187void *\r
188realloc(void *Ptr, size_t NewSize)\r
189{\r
190 void *RetVal = NULL;\r
191 POOL_HEAD *Head;\r
192 UINTN OldSize = 0;\r
193 UINTN NumCpy;\r
194\r
195 // Find out the size of the OLD memory region\r
196 if( Ptr != NULL) {\r
197 Head = BASE_CR (Ptr, POOL_HEAD, Data);\r
198 assert(Head != NULL);\r
199 if (Head->Signature != POOL_HEAD_SIGNATURE) {\r
200 errno = EFAULT;\r
201 return NULL;\r
202 }\r
203 OldSize = Head->Size;\r
204 }\r
205\r
206 // At this point, Ptr is either NULL or a valid pointer to an allocated space\r
207\r
208 if( NewSize > 0) {\r
209 RetVal = malloc(NewSize); // Get the NEW memory region\r
210 if( Ptr != NULL) { // If there is an OLD region...\r
211 if( RetVal != NULL) { // and the NEW region was successfully allocated\r
212 NumCpy = OldSize;\r
213 if( OldSize > NewSize) {\r
214 NumCpy = NewSize;\r
215 }\r
216 (VOID)CopyMem( RetVal, Ptr, NumCpy); // Copy old data to the new region.\r
217 free( Ptr); // and reclaim the old region.\r
218 }\r
219 }\r
220 }\r
221 else {\r
222 if( Ptr != NULL) {\r
223 free( Ptr); // Reclaim the old region.\r
224 }\r
225 }\r
226\r
227 return RetVal;\r
228}\r