2 Support routines for memory allocation routines based
3 on boot services for Dxe phase drivers, with memory profile support.
5 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/MemoryAllocationLib.h>
15 #include <Library/UefiBootServicesTableLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
19 #include <Library/MemoryProfileLib.h>
22 Allocates one or more 4KB pages of a certain memory type.
24 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
25 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
26 If there is not enough memory remaining to satisfy the request, then NULL is returned.
28 @param MemoryType The type of memory to allocate.
29 @param Pages The number of 4 KB pages to allocate.
31 @return A pointer to the allocated buffer or NULL if allocation fails.
35 InternalAllocatePages (
36 IN EFI_MEMORY_TYPE MemoryType
,
41 EFI_PHYSICAL_ADDRESS Memory
;
47 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
48 if (EFI_ERROR (Status
)) {
51 return (VOID
*) (UINTN
) Memory
;
55 Allocates one or more 4KB pages of type EfiBootServicesData.
57 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
58 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
59 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
62 @param Pages The number of 4 KB pages to allocate.
64 @return A pointer to the allocated buffer or NULL if allocation fails.
75 Buffer
= InternalAllocatePages (EfiBootServicesData
, Pages
);
77 MemoryProfileLibRecord (
78 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
79 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES
,
82 EFI_PAGES_TO_SIZE (Pages
),
90 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
92 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
93 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
94 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
97 @param Pages The number of 4 KB pages to allocate.
99 @return A pointer to the allocated buffer or NULL if allocation fails.
104 AllocateRuntimePages (
110 Buffer
= InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
111 if (Buffer
!= NULL
) {
112 MemoryProfileLibRecord (
113 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
114 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES
,
115 EfiRuntimeServicesData
,
117 EFI_PAGES_TO_SIZE (Pages
),
125 Allocates one or more 4KB pages of type EfiReservedMemoryType.
127 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
128 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
129 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
132 @param Pages The number of 4 KB pages to allocate.
134 @return A pointer to the allocated buffer or NULL if allocation fails.
139 AllocateReservedPages (
145 Buffer
= InternalAllocatePages (EfiReservedMemoryType
, Pages
);
146 if (Buffer
!= NULL
) {
147 MemoryProfileLibRecord (
148 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
149 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES
,
150 EfiReservedMemoryType
,
152 EFI_PAGES_TO_SIZE (Pages
),
160 Frees one or more 4KB pages that were previously allocated with one of the page allocation
161 functions in the Memory Allocation Library.
163 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
164 must have been allocated on a previous call to the page allocation services of the Memory
165 Allocation Library. If it is not possible to free allocated pages, then this function will
168 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
170 If Pages is zero, then ASSERT().
172 @param Buffer The pointer to the buffer of pages to free.
173 @param Pages The number of 4 KB pages to free.
186 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
187 ASSERT_EFI_ERROR (Status
);
191 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
193 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
194 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
195 If there is not enough memory at the specified alignment remaining to satisfy the request, then
197 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
198 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
200 @param MemoryType The type of memory to allocate.
201 @param Pages The number of 4 KB pages to allocate.
202 @param Alignment The requested alignment of the allocation. Must be a power of two.
203 If Alignment is zero, then byte alignment is used.
205 @return A pointer to the allocated buffer or NULL if allocation fails.
209 InternalAllocateAlignedPages (
210 IN EFI_MEMORY_TYPE MemoryType
,
216 EFI_PHYSICAL_ADDRESS Memory
;
219 UINTN UnalignedPages
;
223 // Alignment must be a power of two or zero.
225 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
230 if (Alignment
> EFI_PAGE_SIZE
) {
232 // Calculate the total number of pages since alignment is larger than page size.
234 AlignmentMask
= Alignment
- 1;
235 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
237 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
239 ASSERT (RealPages
> Pages
);
241 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
242 if (EFI_ERROR (Status
)) {
245 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
246 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
247 if (UnalignedPages
> 0) {
249 // Free first unaligned page(s).
251 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
252 ASSERT_EFI_ERROR (Status
);
254 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
255 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
256 if (UnalignedPages
> 0) {
258 // Free last unaligned page(s).
260 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
261 ASSERT_EFI_ERROR (Status
);
265 // Do not over-allocate pages in this case.
267 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
268 if (EFI_ERROR (Status
)) {
271 AlignedMemory
= (UINTN
) Memory
;
273 return (VOID
*) AlignedMemory
;
277 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
279 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
280 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
281 returned. If there is not enough memory at the specified alignment remaining to satisfy the
282 request, then NULL is returned.
284 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
285 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
287 @param Pages The number of 4 KB pages to allocate.
288 @param Alignment The requested alignment of the allocation. Must be a power of two.
289 If Alignment is zero, then byte alignment is used.
291 @return A pointer to the allocated buffer or NULL if allocation fails.
296 AllocateAlignedPages (
303 Buffer
= InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
304 if (Buffer
!= NULL
) {
305 MemoryProfileLibRecord (
306 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
307 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES
,
310 EFI_PAGES_TO_SIZE (Pages
),
318 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
320 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
321 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
322 returned. If there is not enough memory at the specified alignment remaining to satisfy the
323 request, then NULL is returned.
325 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
326 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
328 @param Pages The number of 4 KB pages to allocate.
329 @param Alignment The requested alignment of the allocation. Must be a power of two.
330 If Alignment is zero, then byte alignment is used.
332 @return A pointer to the allocated buffer or NULL if allocation fails.
337 AllocateAlignedRuntimePages (
344 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
345 if (Buffer
!= NULL
) {
346 MemoryProfileLibRecord (
347 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
348 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
349 EfiRuntimeServicesData
,
351 EFI_PAGES_TO_SIZE (Pages
),
359 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
361 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
362 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
363 returned. If there is not enough memory at the specified alignment remaining to satisfy the
364 request, then NULL is returned.
366 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
367 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
369 @param Pages The number of 4 KB pages to allocate.
370 @param Alignment The requested alignment of the allocation. Must be a power of two.
371 If Alignment is zero, then byte alignment is used.
373 @return A pointer to the allocated buffer or NULL if allocation fails.
378 AllocateAlignedReservedPages (
385 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
386 if (Buffer
!= NULL
) {
387 MemoryProfileLibRecord (
388 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
389 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
390 EfiReservedMemoryType
,
392 EFI_PAGES_TO_SIZE (Pages
),
400 Frees one or more 4KB pages that were previously allocated with one of the aligned page
401 allocation functions in the Memory Allocation Library.
403 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
404 must have been allocated on a previous call to the aligned page allocation services of the Memory
405 Allocation Library. If it is not possible to free allocated pages, then this function will
408 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
409 Library, then ASSERT().
410 If Pages is zero, then ASSERT().
412 @param Buffer The pointer to the buffer of pages to free.
413 @param Pages The number of 4 KB pages to free.
426 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
427 ASSERT_EFI_ERROR (Status
);
431 Allocates a buffer of a certain pool type.
433 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
434 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
435 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
437 @param MemoryType The type of memory to allocate.
438 @param AllocationSize The number of bytes to allocate.
440 @return A pointer to the allocated buffer or NULL if allocation fails.
444 InternalAllocatePool (
445 IN EFI_MEMORY_TYPE MemoryType
,
446 IN UINTN AllocationSize
452 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
453 if (EFI_ERROR (Status
)) {
460 Allocates a buffer of type EfiBootServicesData.
462 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
463 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
464 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
466 @param AllocationSize The number of bytes to allocate.
468 @return A pointer to the allocated buffer or NULL if allocation fails.
474 IN UINTN AllocationSize
479 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
480 if (Buffer
!= NULL
) {
481 MemoryProfileLibRecord (
482 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
483 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
494 Allocates a buffer of type EfiRuntimeServicesData.
496 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
497 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
498 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
500 @param AllocationSize The number of bytes to allocate.
502 @return A pointer to the allocated buffer or NULL if allocation fails.
507 AllocateRuntimePool (
508 IN UINTN AllocationSize
513 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
514 if (Buffer
!= NULL
) {
515 MemoryProfileLibRecord (
516 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
517 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
518 EfiRuntimeServicesData
,
528 Allocates a buffer of type EfiReservedMemoryType.
530 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
531 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
532 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
534 @param AllocationSize The number of bytes to allocate.
536 @return A pointer to the allocated buffer or NULL if allocation fails.
541 AllocateReservedPool (
542 IN UINTN AllocationSize
547 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
548 if (Buffer
!= NULL
) {
549 MemoryProfileLibRecord (
550 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
551 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
552 EfiReservedMemoryType
,
562 Allocates and zeros a buffer of a certain pool type.
564 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
565 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
566 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
567 then NULL is returned.
569 @param PoolType The type of memory to allocate.
570 @param AllocationSize The number of bytes to allocate and zero.
572 @return A pointer to the allocated buffer or NULL if allocation fails.
576 InternalAllocateZeroPool (
577 IN EFI_MEMORY_TYPE PoolType
,
578 IN UINTN AllocationSize
583 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
584 if (Memory
!= NULL
) {
585 Memory
= ZeroMem (Memory
, AllocationSize
);
591 Allocates and zeros a buffer of type EfiBootServicesData.
593 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
594 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
595 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
596 request, then NULL is returned.
598 @param AllocationSize The number of bytes to allocate and zero.
600 @return A pointer to the allocated buffer or NULL if allocation fails.
606 IN UINTN AllocationSize
611 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
612 if (Buffer
!= NULL
) {
613 MemoryProfileLibRecord (
614 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
615 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
626 Allocates and zeros a buffer of type EfiRuntimeServicesData.
628 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
629 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
630 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
631 request, then NULL is returned.
633 @param AllocationSize The number of bytes to allocate and zero.
635 @return A pointer to the allocated buffer or NULL if allocation fails.
640 AllocateRuntimeZeroPool (
641 IN UINTN AllocationSize
646 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
647 if (Buffer
!= NULL
) {
648 MemoryProfileLibRecord (
649 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
650 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
651 EfiRuntimeServicesData
,
661 Allocates and zeros a buffer of type EfiReservedMemoryType.
663 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
664 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
665 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
666 request, then NULL is returned.
668 @param AllocationSize The number of bytes to allocate and zero.
670 @return A pointer to the allocated buffer or NULL if allocation fails.
675 AllocateReservedZeroPool (
676 IN UINTN AllocationSize
681 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
682 if (Buffer
!= NULL
) {
683 MemoryProfileLibRecord (
684 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
685 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
686 EfiReservedMemoryType
,
696 Copies a buffer to an allocated buffer of a certain pool type.
698 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
699 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
700 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
701 is not enough memory remaining to satisfy the request, then NULL is returned.
702 If Buffer is NULL, then ASSERT().
703 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
705 @param PoolType The type of pool to allocate.
706 @param AllocationSize The number of bytes to allocate and zero.
707 @param Buffer The buffer to copy to the allocated buffer.
709 @return A pointer to the allocated buffer or NULL if allocation fails.
713 InternalAllocateCopyPool (
714 IN EFI_MEMORY_TYPE PoolType
,
715 IN UINTN AllocationSize
,
716 IN CONST VOID
*Buffer
721 ASSERT (Buffer
!= NULL
);
722 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
724 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
725 if (Memory
!= NULL
) {
726 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
732 Copies a buffer to an allocated buffer of type EfiBootServicesData.
734 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
735 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
736 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
737 is not enough memory remaining to satisfy the request, then NULL is returned.
739 If Buffer is NULL, then ASSERT().
740 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
742 @param AllocationSize The number of bytes to allocate and zero.
743 @param Buffer The buffer to copy to the allocated buffer.
745 @return A pointer to the allocated buffer or NULL if allocation fails.
751 IN UINTN AllocationSize
,
752 IN CONST VOID
*Buffer
757 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
758 if (NewBuffer
!= NULL
) {
759 MemoryProfileLibRecord (
760 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
761 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
772 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
774 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
775 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
776 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
777 is not enough memory remaining to satisfy the request, then NULL is returned.
779 If Buffer is NULL, then ASSERT().
780 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
782 @param AllocationSize The number of bytes to allocate and zero.
783 @param Buffer The buffer to copy to the allocated buffer.
785 @return A pointer to the allocated buffer or NULL if allocation fails.
790 AllocateRuntimeCopyPool (
791 IN UINTN AllocationSize
,
792 IN CONST VOID
*Buffer
797 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
798 if (NewBuffer
!= NULL
) {
799 MemoryProfileLibRecord (
800 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
801 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
802 EfiRuntimeServicesData
,
812 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
814 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
815 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
816 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
817 is not enough memory remaining to satisfy the request, then NULL is returned.
819 If Buffer is NULL, then ASSERT().
820 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
822 @param AllocationSize The number of bytes to allocate and zero.
823 @param Buffer The buffer to copy to the allocated buffer.
825 @return A pointer to the allocated buffer or NULL if allocation fails.
830 AllocateReservedCopyPool (
831 IN UINTN AllocationSize
,
832 IN CONST VOID
*Buffer
837 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
838 if (NewBuffer
!= NULL
) {
839 MemoryProfileLibRecord (
840 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
841 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
842 EfiRuntimeServicesData
,
852 Reallocates a buffer of a specified memory type.
854 Allocates and zeros the number bytes specified by NewSize from memory of the type
855 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
856 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
857 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
858 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
859 enough memory remaining to satisfy the request, then NULL is returned.
861 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
862 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
864 @param PoolType The type of pool to allocate.
865 @param OldSize The size, in bytes, of OldBuffer.
866 @param NewSize The size, in bytes, of the buffer to reallocate.
867 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
868 parameter that may be NULL.
870 @return A pointer to the allocated buffer or NULL if allocation fails.
874 InternalReallocatePool (
875 IN EFI_MEMORY_TYPE PoolType
,
878 IN VOID
*OldBuffer OPTIONAL
883 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
884 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
885 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
886 FreePool (OldBuffer
);
892 Reallocates a buffer of type EfiBootServicesData.
894 Allocates and zeros the number bytes specified by NewSize from memory of type
895 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
896 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
897 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
898 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
899 enough memory remaining to satisfy the request, then NULL is returned.
901 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
902 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
904 @param OldSize The size, in bytes, of OldBuffer.
905 @param NewSize The size, in bytes, of the buffer to reallocate.
906 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
907 parameter that may be NULL.
909 @return A pointer to the allocated buffer or NULL if allocation fails.
917 IN VOID
*OldBuffer OPTIONAL
922 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
923 if (Buffer
!= NULL
) {
924 MemoryProfileLibRecord (
925 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
926 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
937 Reallocates a buffer of type EfiRuntimeServicesData.
939 Allocates and zeros the number bytes specified by NewSize from memory of type
940 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
941 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
942 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
943 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
944 enough memory remaining to satisfy the request, then NULL is returned.
946 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
947 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
949 @param OldSize The size, in bytes, of OldBuffer.
950 @param NewSize The size, in bytes, of the buffer to reallocate.
951 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
952 parameter that may be NULL.
954 @return A pointer to the allocated buffer or NULL if allocation fails.
959 ReallocateRuntimePool (
962 IN VOID
*OldBuffer OPTIONAL
967 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
968 if (Buffer
!= NULL
) {
969 MemoryProfileLibRecord (
970 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
971 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
972 EfiRuntimeServicesData
,
982 Reallocates a buffer of type EfiReservedMemoryType.
984 Allocates and zeros the number bytes specified by NewSize from memory of type
985 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
986 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
987 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
988 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
989 enough memory remaining to satisfy the request, then NULL is returned.
991 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
992 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
994 @param OldSize The size, in bytes, of OldBuffer.
995 @param NewSize The size, in bytes, of the buffer to reallocate.
996 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
997 parameter that may be NULL.
999 @return A pointer to the allocated buffer or NULL if allocation fails.
1004 ReallocateReservedPool (
1007 IN VOID
*OldBuffer OPTIONAL
1012 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1013 if (Buffer
!= NULL
) {
1014 MemoryProfileLibRecord (
1015 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
1016 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1017 EfiReservedMemoryType
,
1027 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1028 Memory Allocation Library.
1030 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1031 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1032 resources, then this function will perform no actions.
1034 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1037 @param Buffer The pointer to the buffer to free.
1048 Status
= gBS
->FreePool (Buffer
);
1049 ASSERT_EFI_ERROR (Status
);