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