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
12 #include <Library/MemoryAllocationLib.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
17 #include <Library/MemoryProfileLib.h>
20 Allocates one or more 4KB pages of a certain memory type.
22 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
23 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
24 If there is not enough memory remaining to satisfy the request, then NULL is returned.
26 @param MemoryType The type of memory to allocate.
27 @param Pages The number of 4 KB pages to allocate.
29 @return A pointer to the allocated buffer or NULL if allocation fails.
33 InternalAllocatePages (
34 IN EFI_MEMORY_TYPE MemoryType
,
39 EFI_PHYSICAL_ADDRESS Memory
;
45 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
46 if (EFI_ERROR (Status
)) {
50 return (VOID
*)(UINTN
)Memory
;
54 Allocates one or more 4KB pages of type EfiBootServicesData.
56 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
57 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
58 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
61 @param Pages The number of 4 KB pages to allocate.
63 @return A pointer to the allocated buffer or NULL if allocation fails.
74 Buffer
= InternalAllocatePages (EfiBootServicesData
, Pages
);
76 MemoryProfileLibRecord (
77 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
78 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES
,
81 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
),
126 Allocates one or more 4KB pages of type EfiReservedMemoryType.
128 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
129 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
130 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
133 @param Pages The number of 4 KB pages to allocate.
135 @return A pointer to the allocated buffer or NULL if allocation fails.
140 AllocateReservedPages (
146 Buffer
= InternalAllocatePages (EfiReservedMemoryType
, Pages
);
147 if (Buffer
!= NULL
) {
148 MemoryProfileLibRecord (
149 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
150 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES
,
151 EfiReservedMemoryType
,
153 EFI_PAGES_TO_SIZE (Pages
),
162 Frees one or more 4KB pages that were previously allocated with one of the page allocation
163 functions in the Memory Allocation Library.
165 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
166 must have been allocated on a previous call to the page allocation services of the Memory
167 Allocation Library. If it is not possible to free allocated pages, then this function will
170 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
172 If Pages is zero, then ASSERT().
174 @param Buffer The pointer to the buffer of pages to free.
175 @param Pages The number of 4 KB pages to free.
188 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, Pages
);
189 ASSERT_EFI_ERROR (Status
);
193 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
195 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
196 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
197 If there is not enough memory at the specified alignment remaining to satisfy the request, then
199 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
200 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
202 @param MemoryType The type of memory to allocate.
203 @param Pages The number of 4 KB pages to allocate.
204 @param Alignment The requested alignment of the allocation. Must be a power of two.
205 If Alignment is zero, then byte alignment is used.
207 @return A pointer to the allocated buffer or NULL if allocation fails.
211 InternalAllocateAlignedPages (
212 IN EFI_MEMORY_TYPE MemoryType
,
218 EFI_PHYSICAL_ADDRESS Memory
;
221 UINTN UnalignedPages
;
225 // Alignment must be a power of two or zero.
227 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
233 if (Alignment
> EFI_PAGE_SIZE
) {
235 // Calculate the total number of pages since alignment is larger than page size.
237 AlignmentMask
= Alignment
- 1;
238 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
240 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
242 ASSERT (RealPages
> Pages
);
244 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
245 if (EFI_ERROR (Status
)) {
249 AlignedMemory
= ((UINTN
)Memory
+ AlignmentMask
) & ~AlignmentMask
;
250 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
)Memory
);
251 if (UnalignedPages
> 0) {
253 // Free first unaligned page(s).
255 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
256 ASSERT_EFI_ERROR (Status
);
259 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
260 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
261 if (UnalignedPages
> 0) {
263 // Free last unaligned page(s).
265 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
266 ASSERT_EFI_ERROR (Status
);
270 // Do not over-allocate pages in this case.
272 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
273 if (EFI_ERROR (Status
)) {
277 AlignedMemory
= (UINTN
)Memory
;
280 return (VOID
*)AlignedMemory
;
284 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
286 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
287 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
288 returned. If there is not enough memory at the specified alignment remaining to satisfy the
289 request, then NULL is returned.
291 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
292 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
294 @param Pages The number of 4 KB pages to allocate.
295 @param Alignment The requested alignment of the allocation. Must be a power of two.
296 If Alignment is zero, then byte alignment is used.
298 @return A pointer to the allocated buffer or NULL if allocation fails.
303 AllocateAlignedPages (
310 Buffer
= InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
311 if (Buffer
!= NULL
) {
312 MemoryProfileLibRecord (
313 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
314 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES
,
317 EFI_PAGES_TO_SIZE (Pages
),
326 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
328 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
329 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
330 returned. If there is not enough memory at the specified alignment remaining to satisfy the
331 request, then NULL is returned.
333 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
334 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
336 @param Pages The number of 4 KB pages to allocate.
337 @param Alignment The requested alignment of the allocation. Must be a power of two.
338 If Alignment is zero, then byte alignment is used.
340 @return A pointer to the allocated buffer or NULL if allocation fails.
345 AllocateAlignedRuntimePages (
352 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
353 if (Buffer
!= NULL
) {
354 MemoryProfileLibRecord (
355 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
356 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
357 EfiRuntimeServicesData
,
359 EFI_PAGES_TO_SIZE (Pages
),
368 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
370 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
371 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
372 returned. If there is not enough memory at the specified alignment remaining to satisfy the
373 request, then NULL is returned.
375 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
376 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
378 @param Pages The number of 4 KB pages to allocate.
379 @param Alignment The requested alignment of the allocation. Must be a power of two.
380 If Alignment is zero, then byte alignment is used.
382 @return A pointer to the allocated buffer or NULL if allocation fails.
387 AllocateAlignedReservedPages (
394 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
395 if (Buffer
!= NULL
) {
396 MemoryProfileLibRecord (
397 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
398 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
399 EfiReservedMemoryType
,
401 EFI_PAGES_TO_SIZE (Pages
),
410 Frees one or more 4KB pages that were previously allocated with one of the aligned page
411 allocation functions in the Memory Allocation Library.
413 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
414 must have been allocated on a previous call to the aligned page allocation services of the Memory
415 Allocation Library. If it is not possible to free allocated pages, then this function will
418 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
419 Library, then ASSERT().
420 If Pages is zero, then ASSERT().
422 @param Buffer The pointer to the buffer of pages to free.
423 @param Pages The number of 4 KB pages to free.
436 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, Pages
);
437 ASSERT_EFI_ERROR (Status
);
441 Allocates a buffer of a certain pool type.
443 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
444 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
445 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
447 @param MemoryType The type of memory to allocate.
448 @param AllocationSize The number of bytes to allocate.
450 @return A pointer to the allocated buffer or NULL if allocation fails.
454 InternalAllocatePool (
455 IN EFI_MEMORY_TYPE MemoryType
,
456 IN UINTN AllocationSize
462 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
463 if (EFI_ERROR (Status
)) {
471 Allocates a buffer of type EfiBootServicesData.
473 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
474 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
475 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
477 @param AllocationSize The number of bytes to allocate.
479 @return A pointer to the allocated buffer or NULL if allocation fails.
485 IN UINTN AllocationSize
490 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
491 if (Buffer
!= NULL
) {
492 MemoryProfileLibRecord (
493 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
494 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
506 Allocates a buffer of type EfiRuntimeServicesData.
508 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
509 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
510 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
512 @param AllocationSize The number of bytes to allocate.
514 @return A pointer to the allocated buffer or NULL if allocation fails.
519 AllocateRuntimePool (
520 IN UINTN AllocationSize
525 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
526 if (Buffer
!= NULL
) {
527 MemoryProfileLibRecord (
528 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
529 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
530 EfiRuntimeServicesData
,
541 Allocates a buffer of type EfiReservedMemoryType.
543 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
544 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
545 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
547 @param AllocationSize The number of bytes to allocate.
549 @return A pointer to the allocated buffer or NULL if allocation fails.
554 AllocateReservedPool (
555 IN UINTN AllocationSize
560 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
561 if (Buffer
!= NULL
) {
562 MemoryProfileLibRecord (
563 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
564 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
565 EfiReservedMemoryType
,
576 Allocates and zeros a buffer of a certain pool type.
578 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
579 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
580 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
581 then NULL is returned.
583 @param PoolType The type of memory to allocate.
584 @param AllocationSize The number of bytes to allocate and zero.
586 @return A pointer to the allocated buffer or NULL if allocation fails.
590 InternalAllocateZeroPool (
591 IN EFI_MEMORY_TYPE PoolType
,
592 IN UINTN AllocationSize
597 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
598 if (Memory
!= NULL
) {
599 Memory
= ZeroMem (Memory
, AllocationSize
);
606 Allocates and zeros a buffer of type EfiBootServicesData.
608 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
609 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
610 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
611 request, then NULL is returned.
613 @param AllocationSize The number of bytes to allocate and zero.
615 @return A pointer to the allocated buffer or NULL if allocation fails.
621 IN UINTN AllocationSize
626 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
627 if (Buffer
!= NULL
) {
628 MemoryProfileLibRecord (
629 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
630 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
642 Allocates and zeros a buffer of type EfiRuntimeServicesData.
644 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
645 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
646 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
647 request, then NULL is returned.
649 @param AllocationSize The number of bytes to allocate and zero.
651 @return A pointer to the allocated buffer or NULL if allocation fails.
656 AllocateRuntimeZeroPool (
657 IN UINTN AllocationSize
662 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
663 if (Buffer
!= NULL
) {
664 MemoryProfileLibRecord (
665 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
666 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
667 EfiRuntimeServicesData
,
678 Allocates and zeros a buffer of type EfiReservedMemoryType.
680 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
681 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
682 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
683 request, then NULL is returned.
685 @param AllocationSize The number of bytes to allocate and zero.
687 @return A pointer to the allocated buffer or NULL if allocation fails.
692 AllocateReservedZeroPool (
693 IN UINTN AllocationSize
698 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
699 if (Buffer
!= NULL
) {
700 MemoryProfileLibRecord (
701 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
702 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
703 EfiReservedMemoryType
,
714 Copies a buffer to an allocated buffer of a certain pool type.
716 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
717 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
718 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
719 is not enough memory remaining to satisfy the request, then NULL is returned.
720 If Buffer is NULL, then ASSERT().
721 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
723 @param PoolType The type of pool to allocate.
724 @param AllocationSize The number of bytes to allocate and zero.
725 @param Buffer The buffer to copy to the allocated buffer.
727 @return A pointer to the allocated buffer or NULL if allocation fails.
731 InternalAllocateCopyPool (
732 IN EFI_MEMORY_TYPE PoolType
,
733 IN UINTN AllocationSize
,
734 IN CONST VOID
*Buffer
739 ASSERT (Buffer
!= NULL
);
740 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
)Buffer
+ 1));
742 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
743 if (Memory
!= NULL
) {
744 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
751 Copies a buffer to an allocated buffer of type EfiBootServicesData.
753 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
754 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
755 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
756 is not enough memory remaining to satisfy the request, then NULL is returned.
758 If Buffer is NULL, then ASSERT().
759 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
761 @param AllocationSize The number of bytes to allocate and zero.
762 @param Buffer The buffer to copy to the allocated buffer.
764 @return A pointer to the allocated buffer or NULL if allocation fails.
770 IN UINTN AllocationSize
,
771 IN CONST VOID
*Buffer
776 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
777 if (NewBuffer
!= NULL
) {
778 MemoryProfileLibRecord (
779 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
780 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
792 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
794 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
795 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
796 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
797 is not enough memory remaining to satisfy the request, then NULL is returned.
799 If Buffer is NULL, then ASSERT().
800 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
802 @param AllocationSize The number of bytes to allocate and zero.
803 @param Buffer The buffer to copy to the allocated buffer.
805 @return A pointer to the allocated buffer or NULL if allocation fails.
810 AllocateRuntimeCopyPool (
811 IN UINTN AllocationSize
,
812 IN CONST VOID
*Buffer
817 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
818 if (NewBuffer
!= NULL
) {
819 MemoryProfileLibRecord (
820 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
821 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
822 EfiRuntimeServicesData
,
833 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
835 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
836 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
837 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
838 is not enough memory remaining to satisfy the request, then NULL is returned.
840 If Buffer is NULL, then ASSERT().
841 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
843 @param AllocationSize The number of bytes to allocate and zero.
844 @param Buffer The buffer to copy to the allocated buffer.
846 @return A pointer to the allocated buffer or NULL if allocation fails.
851 AllocateReservedCopyPool (
852 IN UINTN AllocationSize
,
853 IN CONST VOID
*Buffer
858 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
859 if (NewBuffer
!= NULL
) {
860 MemoryProfileLibRecord (
861 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
862 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
863 EfiRuntimeServicesData
,
874 Reallocates a buffer of a specified memory type.
876 Allocates and zeros the number bytes specified by NewSize from memory of the type
877 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
878 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
879 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
880 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
881 enough memory remaining to satisfy the request, then NULL is returned.
883 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
884 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
886 @param PoolType The type of pool to allocate.
887 @param OldSize The size, in bytes, of OldBuffer.
888 @param NewSize The size, in bytes, of the buffer to reallocate.
889 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
890 parameter that may be NULL.
892 @return A pointer to the allocated buffer or NULL if allocation fails.
896 InternalReallocatePool (
897 IN EFI_MEMORY_TYPE PoolType
,
900 IN VOID
*OldBuffer OPTIONAL
905 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
906 if ((NewBuffer
!= NULL
) && (OldBuffer
!= NULL
)) {
907 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
908 FreePool (OldBuffer
);
915 Reallocates a buffer of type EfiBootServicesData.
917 Allocates and zeros the number bytes specified by NewSize from memory of type
918 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
919 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
920 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
921 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
922 enough memory remaining to satisfy the request, then NULL is returned.
924 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
925 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
927 @param OldSize The size, in bytes, of OldBuffer.
928 @param NewSize The size, in bytes, of the buffer to reallocate.
929 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
930 parameter that may be NULL.
932 @return A pointer to the allocated buffer or NULL if allocation fails.
940 IN VOID
*OldBuffer OPTIONAL
945 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
946 if (Buffer
!= NULL
) {
947 MemoryProfileLibRecord (
948 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
949 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
961 Reallocates a buffer of type EfiRuntimeServicesData.
963 Allocates and zeros the number bytes specified by NewSize from memory of type
964 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
965 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
966 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
967 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
968 enough memory remaining to satisfy the request, then NULL is returned.
970 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
971 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
973 @param OldSize The size, in bytes, of OldBuffer.
974 @param NewSize The size, in bytes, of the buffer to reallocate.
975 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
976 parameter that may be NULL.
978 @return A pointer to the allocated buffer or NULL if allocation fails.
983 ReallocateRuntimePool (
986 IN VOID
*OldBuffer OPTIONAL
991 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
992 if (Buffer
!= NULL
) {
993 MemoryProfileLibRecord (
994 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
995 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
996 EfiRuntimeServicesData
,
1007 Reallocates a buffer of type EfiReservedMemoryType.
1009 Allocates and zeros the number bytes specified by NewSize from memory of type
1010 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
1011 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
1012 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
1013 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
1014 enough memory remaining to satisfy the request, then NULL is returned.
1016 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
1017 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
1019 @param OldSize The size, in bytes, of OldBuffer.
1020 @param NewSize The size, in bytes, of the buffer to reallocate.
1021 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
1022 parameter that may be NULL.
1024 @return A pointer to the allocated buffer or NULL if allocation fails.
1029 ReallocateReservedPool (
1032 IN VOID
*OldBuffer OPTIONAL
1037 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1038 if (Buffer
!= NULL
) {
1039 MemoryProfileLibRecord (
1040 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
1041 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1042 EfiReservedMemoryType
,
1053 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1054 Memory Allocation Library.
1056 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1057 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1058 resources, then this function will perform no actions.
1060 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1063 @param Buffer The pointer to the buffer to free.
1074 Status
= gBS
->FreePool (Buffer
);
1075 ASSERT_EFI_ERROR (Status
);