]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/UefiMemoryAllocationLib/MemoryAllocationLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / UefiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
e386b444 1/** @file\r
9095d37b 2 Support routines for memory allocation routines based\r
9e6fa6d2 3 on boot services for Dxe phase drivers.\r
e386b444 4\r
9095d37b 5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
9344f092 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e386b444 7\r
e386b444 8**/\r
9\r
60c93673 10#include <Uefi.h>\r
c892d846 11\r
c7d265a9 12#include <Library/MemoryAllocationLib.h>\r
13#include <Library/UefiBootServicesTableLib.h>\r
14#include <Library/BaseMemoryLib.h>\r
15#include <Library/DebugLib.h>\r
e386b444 16\r
e386b444 17/**\r
18 Allocates one or more 4KB pages of a certain memory type.\r
19\r
20 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
21 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
22 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
23\r
24 @param MemoryType The type of memory to allocate.\r
25 @param Pages The number of 4 KB pages to allocate.\r
26\r
27 @return A pointer to the allocated buffer or NULL if allocation fails.\r
28\r
29**/\r
30VOID *\r
31InternalAllocatePages (\r
9095d37b 32 IN EFI_MEMORY_TYPE MemoryType,\r
e386b444 33 IN UINTN Pages\r
34 )\r
35{\r
36 EFI_STATUS Status;\r
9095d37b 37 EFI_PHYSICAL_ADDRESS Memory;\r
e386b444 38\r
39 if (Pages == 0) {\r
40 return NULL;\r
41 }\r
42\r
43 Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
44 if (EFI_ERROR (Status)) {\r
9e6fa6d2 45 return NULL;\r
e386b444 46 }\r
2f88bd3a
MK
47\r
48 return (VOID *)(UINTN)Memory;\r
e386b444 49}\r
50\r
51/**\r
52 Allocates one or more 4KB pages of type EfiBootServicesData.\r
53\r
54 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
55 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
56 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
57 returned.\r
58\r
59 @param Pages The number of 4 KB pages to allocate.\r
60\r
61 @return A pointer to the allocated buffer or NULL if allocation fails.\r
62\r
63**/\r
64VOID *\r
65EFIAPI\r
66AllocatePages (\r
67 IN UINTN Pages\r
68 )\r
69{\r
70 return InternalAllocatePages (EfiBootServicesData, Pages);\r
71}\r
72\r
73/**\r
74 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
75\r
76 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
77 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
78 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
79 returned.\r
80\r
81 @param Pages The number of 4 KB pages to allocate.\r
82\r
83 @return A pointer to the allocated buffer or NULL if allocation fails.\r
84\r
85**/\r
86VOID *\r
87EFIAPI\r
88AllocateRuntimePages (\r
89 IN UINTN Pages\r
90 )\r
91{\r
92 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
93}\r
94\r
95/**\r
96 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
97\r
98 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
99 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
100 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
101 returned.\r
102\r
103 @param Pages The number of 4 KB pages to allocate.\r
104\r
105 @return A pointer to the allocated buffer or NULL if allocation fails.\r
106\r
107**/\r
108VOID *\r
109EFIAPI\r
110AllocateReservedPages (\r
111 IN UINTN Pages\r
112 )\r
113{\r
114 return InternalAllocatePages (EfiReservedMemoryType, Pages);\r
115}\r
116\r
117/**\r
118 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
119 functions in the Memory Allocation Library.\r
120\r
121 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
122 must have been allocated on a previous call to the page allocation services of the Memory\r
6e10b70a 123 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 124 perform no actions.\r
9095d37b 125\r
e386b444 126 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
127 then ASSERT().\r
128 If Pages is zero, then ASSERT().\r
9095d37b 129\r
2fc59a00 130 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 131 @param Pages The number of 4 KB pages to free.\r
132\r
133**/\r
134VOID\r
135EFIAPI\r
136FreePages (\r
137 IN VOID *Buffer,\r
138 IN UINTN Pages\r
139 )\r
140{\r
141 EFI_STATUS Status;\r
142\r
143 ASSERT (Pages != 0);\r
2f88bd3a 144 Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
e386b444 145 ASSERT_EFI_ERROR (Status);\r
146}\r
147\r
148/**\r
149 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
150\r
151 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
152 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
153 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
154 NULL is returned.\r
155 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 156 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 157\r
158 @param MemoryType The type of memory to allocate.\r
159 @param Pages The number of 4 KB pages to allocate.\r
160 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
161 If Alignment is zero, then byte alignment is used.\r
162\r
163 @return A pointer to the allocated buffer or NULL if allocation fails.\r
164\r
165**/\r
166VOID *\r
167InternalAllocateAlignedPages (\r
9095d37b 168 IN EFI_MEMORY_TYPE MemoryType,\r
e386b444 169 IN UINTN Pages,\r
170 IN UINTN Alignment\r
171 )\r
172{\r
173 EFI_STATUS Status;\r
174 EFI_PHYSICAL_ADDRESS Memory;\r
175 UINTN AlignedMemory;\r
176 UINTN AlignmentMask;\r
177 UINTN UnalignedPages;\r
178 UINTN RealPages;\r
179\r
180 //\r
181 // Alignment must be a power of two or zero.\r
182 //\r
183 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
9095d37b 184\r
e386b444 185 if (Pages == 0) {\r
186 return NULL;\r
187 }\r
2f88bd3a 188\r
e386b444 189 if (Alignment > EFI_PAGE_SIZE) {\r
190 //\r
4f953ed7 191 // Calculate the total number of pages since alignment is larger than page size.\r
e386b444 192 //\r
2f88bd3a
MK
193 AlignmentMask = Alignment - 1;\r
194 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
e386b444 195 //\r
196 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
197 //\r
198 ASSERT (RealPages > Pages);\r
9095d37b 199\r
2f88bd3a 200 Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
e386b444 201 if (EFI_ERROR (Status)) {\r
202 return NULL;\r
203 }\r
2f88bd3a
MK
204\r
205 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
206 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
e386b444 207 if (UnalignedPages > 0) {\r
208 //\r
209 // Free first unaligned page(s).\r
210 //\r
211 Status = gBS->FreePages (Memory, UnalignedPages);\r
212 ASSERT_EFI_ERROR (Status);\r
213 }\r
2f88bd3a 214\r
95ba3d92 215 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
e386b444 216 UnalignedPages = RealPages - Pages - UnalignedPages;\r
217 if (UnalignedPages > 0) {\r
218 //\r
219 // Free last unaligned page(s).\r
220 //\r
221 Status = gBS->FreePages (Memory, UnalignedPages);\r
222 ASSERT_EFI_ERROR (Status);\r
223 }\r
224 } else {\r
225 //\r
226 // Do not over-allocate pages in this case.\r
227 //\r
228 Status = gBS->AllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);\r
229 if (EFI_ERROR (Status)) {\r
230 return NULL;\r
231 }\r
2f88bd3a
MK
232\r
233 AlignedMemory = (UINTN)Memory;\r
e386b444 234 }\r
2f88bd3a
MK
235\r
236 return (VOID *)AlignedMemory;\r
e386b444 237}\r
238\r
239/**\r
240 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
241\r
242 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
243 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
244 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
245 request, then NULL is returned.\r
9095d37b 246\r
e386b444 247 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 248 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 249\r
250 @param Pages The number of 4 KB pages to allocate.\r
251 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
252 If Alignment is zero, then byte alignment is used.\r
253\r
254 @return A pointer to the allocated buffer or NULL if allocation fails.\r
255\r
256**/\r
257VOID *\r
258EFIAPI\r
259AllocateAlignedPages (\r
260 IN UINTN Pages,\r
261 IN UINTN Alignment\r
262 )\r
263{\r
264 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r
265}\r
266\r
267/**\r
268 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
269\r
270 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
271 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
272 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
273 request, then NULL is returned.\r
9095d37b 274\r
e386b444 275 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 276 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 277\r
278 @param Pages The number of 4 KB pages to allocate.\r
279 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
280 If Alignment is zero, then byte alignment is used.\r
281\r
282 @return A pointer to the allocated buffer or NULL if allocation fails.\r
283\r
284**/\r
285VOID *\r
286EFIAPI\r
287AllocateAlignedRuntimePages (\r
288 IN UINTN Pages,\r
289 IN UINTN Alignment\r
290 )\r
291{\r
292 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
293}\r
294\r
295/**\r
296 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
297\r
298 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
299 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
300 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
301 request, then NULL is returned.\r
9095d37b 302\r
e386b444 303 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 304 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 305\r
306 @param Pages The number of 4 KB pages to allocate.\r
307 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
308 If Alignment is zero, then byte alignment is used.\r
309\r
310 @return A pointer to the allocated buffer or NULL if allocation fails.\r
311\r
312**/\r
313VOID *\r
314EFIAPI\r
315AllocateAlignedReservedPages (\r
316 IN UINTN Pages,\r
317 IN UINTN Alignment\r
318 )\r
319{\r
320 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r
321}\r
322\r
323/**\r
324 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
325 allocation functions in the Memory Allocation Library.\r
326\r
327 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
328 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
9095d37b 329 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 330 perform no actions.\r
9095d37b 331\r
e386b444 332 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
333 Library, then ASSERT().\r
334 If Pages is zero, then ASSERT().\r
9095d37b 335\r
2fc59a00 336 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 337 @param Pages The number of 4 KB pages to free.\r
338\r
339**/\r
340VOID\r
341EFIAPI\r
342FreeAlignedPages (\r
343 IN VOID *Buffer,\r
344 IN UINTN Pages\r
345 )\r
346{\r
347 EFI_STATUS Status;\r
348\r
349 ASSERT (Pages != 0);\r
2f88bd3a 350 Status = gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);\r
e386b444 351 ASSERT_EFI_ERROR (Status);\r
352}\r
353\r
354/**\r
355 Allocates a buffer of a certain pool type.\r
356\r
357 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
358 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
359 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
360\r
361 @param MemoryType The type of memory to allocate.\r
362 @param AllocationSize The number of bytes to allocate.\r
363\r
364 @return A pointer to the allocated buffer or NULL if allocation fails.\r
365\r
366**/\r
367VOID *\r
368InternalAllocatePool (\r
9095d37b 369 IN EFI_MEMORY_TYPE MemoryType,\r
e386b444 370 IN UINTN AllocationSize\r
371 )\r
372{\r
373 EFI_STATUS Status;\r
374 VOID *Memory;\r
375\r
376 Status = gBS->AllocatePool (MemoryType, AllocationSize, &Memory);\r
377 if (EFI_ERROR (Status)) {\r
378 Memory = NULL;\r
379 }\r
2f88bd3a 380\r
e386b444 381 return Memory;\r
382}\r
383\r
384/**\r
385 Allocates a buffer of type EfiBootServicesData.\r
386\r
387 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
388 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
389 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
390\r
391 @param AllocationSize The number of bytes to allocate.\r
392\r
393 @return A pointer to the allocated buffer or NULL if allocation fails.\r
394\r
395**/\r
396VOID *\r
397EFIAPI\r
398AllocatePool (\r
399 IN UINTN AllocationSize\r
400 )\r
401{\r
402 return InternalAllocatePool (EfiBootServicesData, AllocationSize);\r
403}\r
404\r
405/**\r
406 Allocates a buffer of type EfiRuntimeServicesData.\r
407\r
408 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
409 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
410 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
411\r
412 @param AllocationSize The number of bytes to allocate.\r
413\r
414 @return A pointer to the allocated buffer or NULL if allocation fails.\r
415\r
416**/\r
417VOID *\r
418EFIAPI\r
419AllocateRuntimePool (\r
420 IN UINTN AllocationSize\r
421 )\r
422{\r
423 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
424}\r
425\r
426/**\r
8789c5e0 427 Allocates a buffer of type EfiReservedMemoryType.\r
e386b444 428\r
8789c5e0 429 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
e386b444 430 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
431 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
432\r
433 @param AllocationSize The number of bytes to allocate.\r
434\r
435 @return A pointer to the allocated buffer or NULL if allocation fails.\r
436\r
437**/\r
438VOID *\r
439EFIAPI\r
440AllocateReservedPool (\r
441 IN UINTN AllocationSize\r
442 )\r
443{\r
444 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
445}\r
446\r
447/**\r
446b94b0 448 Allocates and zeros a buffer of a certain pool type.\r
e386b444 449\r
446b94b0 450 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
e386b444 451 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
452 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
453 then NULL is returned.\r
454\r
455 @param PoolType The type of memory to allocate.\r
456 @param AllocationSize The number of bytes to allocate and zero.\r
457\r
458 @return A pointer to the allocated buffer or NULL if allocation fails.\r
459\r
460**/\r
461VOID *\r
462InternalAllocateZeroPool (\r
9095d37b 463 IN EFI_MEMORY_TYPE PoolType,\r
e386b444 464 IN UINTN AllocationSize\r
9095d37b 465 )\r
e386b444 466{\r
467 VOID *Memory;\r
468\r
469 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
470 if (Memory != NULL) {\r
471 Memory = ZeroMem (Memory, AllocationSize);\r
472 }\r
2f88bd3a 473\r
e386b444 474 return Memory;\r
475}\r
476\r
477/**\r
478 Allocates and zeros a buffer of type EfiBootServicesData.\r
479\r
480 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
481 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
482 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
483 request, then NULL is returned.\r
484\r
485 @param AllocationSize The number of bytes to allocate and zero.\r
486\r
487 @return A pointer to the allocated buffer or NULL if allocation fails.\r
488\r
489**/\r
490VOID *\r
491EFIAPI\r
492AllocateZeroPool (\r
493 IN UINTN AllocationSize\r
494 )\r
495{\r
496 return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);\r
497}\r
498\r
499/**\r
500 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
501\r
502 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
503 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
504 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
505 request, then NULL is returned.\r
506\r
507 @param AllocationSize The number of bytes to allocate and zero.\r
508\r
509 @return A pointer to the allocated buffer or NULL if allocation fails.\r
510\r
511**/\r
512VOID *\r
513EFIAPI\r
514AllocateRuntimeZeroPool (\r
515 IN UINTN AllocationSize\r
516 )\r
517{\r
518 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
519}\r
520\r
521/**\r
522 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
523\r
524 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
525 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
526 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
527 request, then NULL is returned.\r
528\r
529 @param AllocationSize The number of bytes to allocate and zero.\r
530\r
531 @return A pointer to the allocated buffer or NULL if allocation fails.\r
532\r
533**/\r
534VOID *\r
535EFIAPI\r
536AllocateReservedZeroPool (\r
537 IN UINTN AllocationSize\r
538 )\r
539{\r
540 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
541}\r
542\r
543/**\r
446b94b0 544 Copies a buffer to an allocated buffer of a certain pool type.\r
e386b444 545\r
446b94b0 546 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
e386b444 547 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
548 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
549 is not enough memory remaining to satisfy the request, then NULL is returned.\r
550 If Buffer is NULL, then ASSERT().\r
9095d37b 551 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
e386b444 552\r
553 @param PoolType The type of pool to allocate.\r
554 @param AllocationSize The number of bytes to allocate and zero.\r
555 @param Buffer The buffer to copy to the allocated buffer.\r
556\r
557 @return A pointer to the allocated buffer or NULL if allocation fails.\r
558\r
559**/\r
560VOID *\r
561InternalAllocateCopyPool (\r
9095d37b 562 IN EFI_MEMORY_TYPE PoolType,\r
e386b444 563 IN UINTN AllocationSize,\r
564 IN CONST VOID *Buffer\r
9095d37b 565 )\r
e386b444 566{\r
567 VOID *Memory;\r
568\r
569 ASSERT (Buffer != NULL);\r
2f88bd3a 570 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));\r
e386b444 571\r
572 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
573 if (Memory != NULL) {\r
2f88bd3a 574 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
e386b444 575 }\r
2f88bd3a 576\r
e386b444 577 return Memory;\r
9095d37b 578}\r
e386b444 579\r
580/**\r
581 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
582\r
583 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
584 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
585 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
586 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 587\r
e386b444 588 If Buffer is NULL, then ASSERT().\r
9095d37b 589 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
e386b444 590\r
591 @param AllocationSize The number of bytes to allocate and zero.\r
592 @param Buffer The buffer to copy to the allocated buffer.\r
593\r
594 @return A pointer to the allocated buffer or NULL if allocation fails.\r
595\r
596**/\r
597VOID *\r
598EFIAPI\r
599AllocateCopyPool (\r
600 IN UINTN AllocationSize,\r
601 IN CONST VOID *Buffer\r
602 )\r
603{\r
604 return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);\r
605}\r
606\r
607/**\r
608 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
609\r
610 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
611 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
612 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
613 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 614\r
e386b444 615 If Buffer is NULL, then ASSERT().\r
9095d37b 616 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
e386b444 617\r
618 @param AllocationSize The number of bytes to allocate and zero.\r
619 @param Buffer The buffer to copy to the allocated buffer.\r
620\r
621 @return A pointer to the allocated buffer or NULL if allocation fails.\r
622\r
623**/\r
624VOID *\r
625EFIAPI\r
626AllocateRuntimeCopyPool (\r
627 IN UINTN AllocationSize,\r
628 IN CONST VOID *Buffer\r
629 )\r
630{\r
631 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
632}\r
633\r
634/**\r
635 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
636\r
637 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
638 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
639 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
640 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 641\r
e386b444 642 If Buffer is NULL, then ASSERT().\r
9095d37b 643 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().\r
e386b444 644\r
645 @param AllocationSize The number of bytes to allocate and zero.\r
646 @param Buffer The buffer to copy to the allocated buffer.\r
647\r
648 @return A pointer to the allocated buffer or NULL if allocation fails.\r
649\r
650**/\r
651VOID *\r
652EFIAPI\r
653AllocateReservedCopyPool (\r
654 IN UINTN AllocationSize,\r
655 IN CONST VOID *Buffer\r
656 )\r
657{\r
658 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
659}\r
660\r
c2390431 661/**\r
662 Reallocates a buffer of a specified memory type.\r
663\r
0a559bb9 664 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
9095d37b
LG
665 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and\r
666 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
667 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
668 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 669 enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 670\r
56304569 671 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
672 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 673\r
674 @param PoolType The type of pool to allocate.\r
675 @param OldSize The size, in bytes, of OldBuffer.\r
676 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 677 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 678 parameter that may be NULL.\r
679\r
680 @return A pointer to the allocated buffer or NULL if allocation fails.\r
681\r
682**/\r
683VOID *\r
c2390431 684InternalReallocatePool (\r
9095d37b 685 IN EFI_MEMORY_TYPE PoolType,\r
c2390431 686 IN UINTN OldSize,\r
687 IN UINTN NewSize,\r
688 IN VOID *OldBuffer OPTIONAL\r
689 )\r
690{\r
691 VOID *NewBuffer;\r
692\r
d9f0ad27 693 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
2f88bd3a 694 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {\r
c2390431 695 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
696 FreePool (OldBuffer);\r
697 }\r
2f88bd3a 698\r
c2390431 699 return NewBuffer;\r
700}\r
701\r
702/**\r
703 Reallocates a buffer of type EfiBootServicesData.\r
704\r
0a559bb9 705 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
706 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
707 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
708 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
709 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 710 enough memory remaining to satisfy the request, then NULL is returned.\r
9095d37b 711\r
56304569 712 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
713 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 714\r
715 @param OldSize The size, in bytes, of OldBuffer.\r
716 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 717 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 718 parameter that may be NULL.\r
719\r
720 @return A pointer to the allocated buffer or NULL if allocation fails.\r
721\r
722**/\r
723VOID *\r
724EFIAPI\r
725ReallocatePool (\r
726 IN UINTN OldSize,\r
727 IN UINTN NewSize,\r
728 IN VOID *OldBuffer OPTIONAL\r
729 )\r
730{\r
731 return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r
732}\r
733\r
734/**\r
735 Reallocates a buffer of type EfiRuntimeServicesData.\r
736\r
0a559bb9 737 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
738 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and\r
739 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
740 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
741 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 742 enough memory remaining to satisfy the request, then NULL is returned.\r
743\r
56304569 744 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
745 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 746\r
747 @param OldSize The size, in bytes, of OldBuffer.\r
748 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 749 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 750 parameter that may be NULL.\r
751\r
752 @return A pointer to the allocated buffer or NULL if allocation fails.\r
753\r
754**/\r
755VOID *\r
756EFIAPI\r
757ReallocateRuntimePool (\r
758 IN UINTN OldSize,\r
759 IN UINTN NewSize,\r
760 IN VOID *OldBuffer OPTIONAL\r
761 )\r
762{\r
763 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
764}\r
765\r
766/**\r
767 Reallocates a buffer of type EfiReservedMemoryType.\r
768\r
0a559bb9 769 Allocates and zeros the number bytes specified by NewSize from memory of type\r
9095d37b
LG
770 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and\r
771 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and\r
772 OldBuffer is freed. A pointer to the newly allocated buffer is returned.\r
773 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not\r
c2390431 774 enough memory remaining to satisfy the request, then NULL is returned.\r
2297186d 775\r
56304569 776 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
777 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
c2390431 778\r
779 @param OldSize The size, in bytes, of OldBuffer.\r
780 @param NewSize The size, in bytes, of the buffer to reallocate.\r
9095d37b 781 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional\r
c2390431 782 parameter that may be NULL.\r
783\r
784 @return A pointer to the allocated buffer or NULL if allocation fails.\r
785\r
786**/\r
787VOID *\r
788EFIAPI\r
789ReallocateReservedPool (\r
790 IN UINTN OldSize,\r
791 IN UINTN NewSize,\r
792 IN VOID *OldBuffer OPTIONAL\r
793 )\r
794{\r
795 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
796}\r
797\r
e386b444 798/**\r
799 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
800 Memory Allocation Library.\r
801\r
802 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
6e10b70a 803 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
446b94b0 804 resources, then this function will perform no actions.\r
9095d37b 805\r
e386b444 806 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
807 then ASSERT().\r
808\r
2fc59a00 809 @param Buffer The pointer to the buffer to free.\r
e386b444 810\r
811**/\r
812VOID\r
813EFIAPI\r
814FreePool (\r
2f88bd3a 815 IN VOID *Buffer\r
e386b444 816 )\r
817{\r
2f88bd3a 818 EFI_STATUS Status;\r
e386b444 819\r
820 Status = gBS->FreePool (Buffer);\r
821 ASSERT_EFI_ERROR (Status);\r
822}\r