]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/StdLib/Malloc.c
2 Definitions for memory allocation routines: calloc, malloc, realloc, free.
4 The order and contiguity of storage allocated by successive calls to the
5 calloc, malloc, and realloc functions is unspecified. The pointer returned
6 if the allocation succeeds is suitably aligned so that it may be assigned to
7 a pointer of any type of object and then used to access such an object or an
8 array of such objects in the space allocated (until the space is explicitly
9 freed or reallocated). Each such allocation shall yield a pointer to an
10 object disjoint from any other object. The pointer returned points to the
11 start (lowest byte address) of the allocated space. If the space can not be
12 allocated, a null pointer is returned. If the size of the space requested
13 is zero, the behavior is implementation-defined; the value returned shall be
14 either a null pointer or a unique pointer. The value of a pointer that
15 refers to freed space is indeterminate.
17 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/UefiBootServicesTableLib.h>
29 #include <Library/BaseLib.h>
30 #include <Library/BaseMemoryLib.h>
31 #include <Library/DebugLib.h>
33 #include <LibConfig.h>
39 #define CPOOL_HEAD_SIGNATURE SIGNATURE_32('C','p','h','d')
41 /** The UEFI functions do not provide a way to determine the size of an
42 allocated region of memory given just a pointer to the start of that
43 region. Since this is required for the implementation of realloc,
44 the memory head structure, CPOOL_HEAD, containing the necessary
45 information is prepended to the requested space.
47 The order of members is important. This structure is 8-byte aligned,
48 as per the UEFI specification for memory allocation functions. By
49 specifying Size as a 64-bit value and placing it immediately before
50 Data, it ensures that Data will always be 8-byte aligned.
52 On IA32 systems, this structure is 24 bytes long, excluding Data.
53 On X64 systems, this structure is 32 bytes long, excluding Data.
62 // List of memory allocated by malloc/calloc/etc.
63 static LIST_ENTRY MemPoolHead
= INITIALIZE_LIST_HEAD_VARIABLE(MemPoolHead
);
65 /****************************/
67 /** The malloc function allocates space for an object whose size is specified
68 by size and whose value is indeterminate.
70 This implementation uses the UEFI memory allocation boot services to get a
71 region of memory that is 8-byte aligned and of the specified size. The
72 region is allocated with type EfiLoaderData.
74 @param size Size, in bytes, of the region to allocate.
76 @return NULL is returned if the space could not be allocated and errno
77 contains the cause. Otherwise, a pointer to an 8-byte aligned
78 region of the requested size is returned.<BR>
79 If NULL is returned, errno may contain:
80 - EINVAL: Requested Size is zero.
81 - ENOMEM: Memory could not be allocated.
92 errno
= EINVAL
; // Make errno diffenent, just in case of a lingering ENOMEM.
93 DEBUG((DEBUG_ERROR
, "ERROR malloc: Zero Size\n"));
97 NodeSize
= (UINTN
)(Size
+ sizeof(CPOOL_HEAD
));
99 DEBUG((DEBUG_POOL
, "malloc(%d): NodeSz: %d", Size
, NodeSize
));
101 Status
= gBS
->AllocatePool( EfiLoaderData
, NodeSize
, (void**)&Head
);
102 if( Status
!= EFI_SUCCESS
) {
105 DEBUG((DEBUG_ERROR
, "\nERROR malloc: AllocatePool returned %r\n", Status
));
108 assert(Head
!= NULL
);
109 // Fill out the pool header
110 Head
->Signature
= CPOOL_HEAD_SIGNATURE
;
111 Head
->Size
= NodeSize
;
113 // Add this node to the list
114 (void)InsertTailList(&MemPoolHead
, (LIST_ENTRY
*)Head
);
116 // Return a pointer to the data
117 RetVal
= (void*)Head
->Data
;
118 DEBUG((DEBUG_POOL
, " Head: %p, Returns %p\n", Head
, RetVal
));
124 /** The calloc function allocates space for an array of Num objects, each of
125 whose size is Size. The space is initialized to all bits zero.
127 This implementation uses the UEFI memory allocation boot services to get a
128 region of memory that is 8-byte aligned and of the specified size. The
129 region is allocated with type EfiLoaderData.
131 @param Num Number of objects to allocate.
132 @param Size Size, in bytes, of the objects to allocate space for.
134 @return NULL is returned if the space could not be allocated and errno
135 contains the cause. Otherwise, a pointer to an 8-byte aligned
136 region of the requested size is returned.
139 calloc(size_t Num
, size_t Size
)
144 NumSize
= Num
* Size
;
147 RetVal
= malloc(NumSize
);
148 if( RetVal
!= NULL
) {
149 (VOID
)ZeroMem( RetVal
, NumSize
);
152 DEBUG((DEBUG_POOL
, "0x%p = calloc(%d, %d)\n", RetVal
, Num
, Size
));
157 /** The free function causes the space pointed to by Ptr to be deallocated,
158 that is, made available for further allocation.
160 If Ptr is a null pointer, no action occurs. Otherwise, if the argument
161 does not match a pointer earlier returned by the calloc, malloc, or realloc
162 function, or if the space has been deallocated by a call to free or
163 realloc, the behavior is undefined.
165 @param Ptr Pointer to a previously allocated region of memory to be freed.
173 Head
= BASE_CR(Ptr
, CPOOL_HEAD
, Data
);
174 assert(Head
!= NULL
);
175 DEBUG((DEBUG_POOL
, "free(%p): Head: %p\n", Ptr
, Head
));
178 if (Head
->Signature
== CPOOL_HEAD_SIGNATURE
) {
179 (void) RemoveEntryList((LIST_ENTRY
*)Head
); // Remove this node from the malloc pool
180 (void) gBS
->FreePool (Head
); // Now free the associated memory
184 DEBUG((DEBUG_ERROR
, "ERROR free(0x%p): Signature is 0x%8X, expected 0x%8X\n",
185 Ptr
, Head
->Signature
, CPOOL_HEAD_SIGNATURE
));
188 DEBUG((DEBUG_POOL
, "free Done\n"));
191 /** The realloc function changes the size of the object pointed to by Ptr to
192 the size specified by NewSize.
194 The contents of the object are unchanged up to the lesser of the new and
195 old sizes. If the new size is larger, the value of the newly allocated
196 portion of the object is indeterminate.
198 If Ptr is a null pointer, the realloc function behaves like the malloc
199 function for the specified size.
201 If Ptr does not match a pointer earlier returned by the calloc, malloc, or
202 realloc function, or if the space has been deallocated by a call to the free
203 or realloc function, the behavior is undefined.
205 If the space cannot be allocated, the object pointed to by Ptr is unchanged.
207 If NewSize is zero and Ptr is not a null pointer, the object it points to
210 This implementation uses the UEFI memory allocation boot services to get a
211 region of memory that is 8-byte aligned and of the specified size. The
212 region is allocated with type EfiLoaderData.
214 The following combinations of Ptr and NewSize can occur:<BR>
216 -------- -------------------<BR>
217 - NULL 0 Returns NULL;
218 - NULL > 0 Same as malloc(NewSize)
219 - invalid X Returns NULL;
220 - valid NewSize >= OldSize Returns malloc(NewSize) with Oldsize bytes copied from Ptr
221 - valid NewSize < OldSize Returns new buffer with Oldsize bytes copied from Ptr
222 - valid 0 Return NULL. Frees Ptr.
225 @param Ptr Pointer to a previously allocated region of memory to be resized.
226 @param NewSize Size, in bytes, of the new object to allocate space for.
228 @return NULL is returned if the space could not be allocated and errno
229 contains the cause. Otherwise, a pointer to an 8-byte aligned
230 region of the requested size is returned. If NewSize is zero,
231 NULL is returned and errno will be unchanged.
234 realloc(void *Ptr
, size_t ReqSize
)
237 CPOOL_HEAD
*Head
= NULL
;
242 // Find out the size of the OLD memory region
244 Head
= BASE_CR (Ptr
, CPOOL_HEAD
, Data
);
245 assert(Head
!= NULL
);
246 if (Head
->Signature
!= CPOOL_HEAD_SIGNATURE
) {
248 DEBUG((DEBUG_ERROR
, "ERROR realloc(0x%p): Signature is 0x%8X, expected 0x%8X\n",
249 Ptr
, Head
->Signature
, CPOOL_HEAD_SIGNATURE
));
252 OldSize
= (size_t)Head
->Size
;
255 // At this point, Ptr is either NULL or a valid pointer to an allocated space
256 NewSize
= (size_t)(ReqSize
+ (sizeof(CPOOL_HEAD
)));
259 RetVal
= malloc(NewSize
); // Get the NEW memory region
260 if( Ptr
!= NULL
) { // If there is an OLD region...
261 if( RetVal
!= NULL
) { // and the NEW region was successfully allocated
263 if( OldSize
> NewSize
) {
266 (VOID
)CopyMem( RetVal
, Ptr
, NumCpy
); // Copy old data to the new region.
267 free( Ptr
); // and reclaim the old region.
275 free( Ptr
); // Reclaim the old region.
277 DEBUG((DEBUG_POOL
, "0x%p = realloc(%p, %d): Head: %p NewSz: %d\n",
278 RetVal
, Ptr
, ReqSize
, Head
, NewSize
));