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 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
25 #include <Library/MemoryProfileLib.h>
28 Allocates one or more 4KB pages of a certain memory type.
30 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
31 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
32 If there is not enough memory remaining to satisfy the request, then NULL is returned.
34 @param MemoryType The type of memory to allocate.
35 @param Pages The number of 4 KB pages to allocate.
37 @return A pointer to the allocated buffer or NULL if allocation fails.
41 InternalAllocatePages (
42 IN EFI_MEMORY_TYPE MemoryType
,
47 EFI_PHYSICAL_ADDRESS Memory
;
53 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
54 if (EFI_ERROR (Status
)) {
57 return (VOID
*) (UINTN
) Memory
;
61 Allocates one or more 4KB pages of type EfiBootServicesData.
63 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
64 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
65 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
68 @param Pages The number of 4 KB pages to allocate.
70 @return A pointer to the allocated buffer or NULL if allocation fails.
81 Buffer
= InternalAllocatePages (EfiBootServicesData
, Pages
);
83 MemoryProfileLibRecord (
84 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
85 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES
,
88 EFI_PAGES_TO_SIZE (Pages
),
96 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
98 Allocates the number of 4KB pages of type EfiRuntimeServicesData 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
103 @param Pages The number of 4 KB pages to allocate.
105 @return A pointer to the allocated buffer or NULL if allocation fails.
110 AllocateRuntimePages (
116 Buffer
= InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
117 if (Buffer
!= NULL
) {
118 MemoryProfileLibRecord (
119 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
120 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES
,
121 EfiRuntimeServicesData
,
123 EFI_PAGES_TO_SIZE (Pages
),
131 Allocates one or more 4KB pages of type EfiReservedMemoryType.
133 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
134 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
135 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
138 @param Pages The number of 4 KB pages to allocate.
140 @return A pointer to the allocated buffer or NULL if allocation fails.
145 AllocateReservedPages (
151 Buffer
= InternalAllocatePages (EfiReservedMemoryType
, Pages
);
152 if (Buffer
!= NULL
) {
153 MemoryProfileLibRecord (
154 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
155 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES
,
156 EfiReservedMemoryType
,
158 EFI_PAGES_TO_SIZE (Pages
),
166 Frees one or more 4KB pages that were previously allocated with one of the page allocation
167 functions in the Memory Allocation Library.
169 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
170 must have been allocated on a previous call to the page allocation services of the Memory
171 Allocation Library. If it is not possible to free allocated pages, then this function will
174 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
176 If Pages is zero, then ASSERT().
178 @param Buffer The pointer to the buffer of pages to free.
179 @param Pages The number of 4 KB pages to free.
192 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
193 ASSERT_EFI_ERROR (Status
);
197 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
199 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
200 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
201 If there is not enough memory at the specified alignment remaining to satisfy the request, then
203 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
204 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
206 @param MemoryType The type of memory to allocate.
207 @param Pages The number of 4 KB pages to allocate.
208 @param Alignment The requested alignment of the allocation. Must be a power of two.
209 If Alignment is zero, then byte alignment is used.
211 @return A pointer to the allocated buffer or NULL if allocation fails.
215 InternalAllocateAlignedPages (
216 IN EFI_MEMORY_TYPE MemoryType
,
222 EFI_PHYSICAL_ADDRESS Memory
;
225 UINTN UnalignedPages
;
229 // Alignment must be a power of two or zero.
231 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
236 if (Alignment
> EFI_PAGE_SIZE
) {
238 // Calculate the total number of pages since alignment is larger than page size.
240 AlignmentMask
= Alignment
- 1;
241 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
243 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
245 ASSERT (RealPages
> Pages
);
247 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
248 if (EFI_ERROR (Status
)) {
251 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
252 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
253 if (UnalignedPages
> 0) {
255 // Free first unaligned page(s).
257 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
258 ASSERT_EFI_ERROR (Status
);
260 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
261 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
262 if (UnalignedPages
> 0) {
264 // Free last unaligned page(s).
266 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
267 ASSERT_EFI_ERROR (Status
);
271 // Do not over-allocate pages in this case.
273 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
274 if (EFI_ERROR (Status
)) {
277 AlignedMemory
= (UINTN
) Memory
;
279 return (VOID
*) AlignedMemory
;
283 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
285 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
286 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
287 returned. If there is not enough memory at the specified alignment remaining to satisfy the
288 request, then NULL is returned.
290 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
291 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
293 @param Pages The number of 4 KB pages to allocate.
294 @param Alignment The requested alignment of the allocation. Must be a power of two.
295 If Alignment is zero, then byte alignment is used.
297 @return A pointer to the allocated buffer or NULL if allocation fails.
302 AllocateAlignedPages (
309 Buffer
= InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
310 if (Buffer
!= NULL
) {
311 MemoryProfileLibRecord (
312 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
313 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES
,
316 EFI_PAGES_TO_SIZE (Pages
),
324 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
326 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
327 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
328 returned. If there is not enough memory at the specified alignment remaining to satisfy the
329 request, then NULL is returned.
331 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
332 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
334 @param Pages The number of 4 KB pages to allocate.
335 @param Alignment The requested alignment of the allocation. Must be a power of two.
336 If Alignment is zero, then byte alignment is used.
338 @return A pointer to the allocated buffer or NULL if allocation fails.
343 AllocateAlignedRuntimePages (
350 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
351 if (Buffer
!= NULL
) {
352 MemoryProfileLibRecord (
353 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
354 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
355 EfiRuntimeServicesData
,
357 EFI_PAGES_TO_SIZE (Pages
),
365 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
367 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
368 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
369 returned. If there is not enough memory at the specified alignment remaining to satisfy the
370 request, then NULL is returned.
372 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
373 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
375 @param Pages The number of 4 KB pages to allocate.
376 @param Alignment The requested alignment of the allocation. Must be a power of two.
377 If Alignment is zero, then byte alignment is used.
379 @return A pointer to the allocated buffer or NULL if allocation fails.
384 AllocateAlignedReservedPages (
391 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
392 if (Buffer
!= NULL
) {
393 MemoryProfileLibRecord (
394 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
395 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
396 EfiReservedMemoryType
,
398 EFI_PAGES_TO_SIZE (Pages
),
406 Frees one or more 4KB pages that were previously allocated with one of the aligned page
407 allocation functions in the Memory Allocation Library.
409 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
410 must have been allocated on a previous call to the aligned page allocation services of the Memory
411 Allocation Library. If it is not possible to free allocated pages, then this function will
414 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
415 Library, then ASSERT().
416 If Pages is zero, then ASSERT().
418 @param Buffer The pointer to the buffer of pages to free.
419 @param Pages The number of 4 KB pages to free.
432 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
433 ASSERT_EFI_ERROR (Status
);
437 Allocates a buffer of a certain pool type.
439 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
440 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
441 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
443 @param MemoryType The type of memory to allocate.
444 @param AllocationSize The number of bytes to allocate.
446 @return A pointer to the allocated buffer or NULL if allocation fails.
450 InternalAllocatePool (
451 IN EFI_MEMORY_TYPE MemoryType
,
452 IN UINTN AllocationSize
458 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
459 if (EFI_ERROR (Status
)) {
466 Allocates a buffer of type EfiBootServicesData.
468 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
469 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
470 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
472 @param AllocationSize The number of bytes to allocate.
474 @return A pointer to the allocated buffer or NULL if allocation fails.
480 IN UINTN AllocationSize
485 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
486 if (Buffer
!= NULL
) {
487 MemoryProfileLibRecord (
488 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
489 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
500 Allocates a buffer of type EfiRuntimeServicesData.
502 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
503 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
504 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
506 @param AllocationSize The number of bytes to allocate.
508 @return A pointer to the allocated buffer or NULL if allocation fails.
513 AllocateRuntimePool (
514 IN UINTN AllocationSize
519 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
520 if (Buffer
!= NULL
) {
521 MemoryProfileLibRecord (
522 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
523 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
524 EfiRuntimeServicesData
,
534 Allocates a buffer of type EfiReservedMemoryType.
536 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
537 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
538 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
540 @param AllocationSize The number of bytes to allocate.
542 @return A pointer to the allocated buffer or NULL if allocation fails.
547 AllocateReservedPool (
548 IN UINTN AllocationSize
553 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
554 if (Buffer
!= NULL
) {
555 MemoryProfileLibRecord (
556 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
557 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
558 EfiReservedMemoryType
,
568 Allocates and zeros a buffer of a certain pool type.
570 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
571 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
572 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
573 then NULL is returned.
575 @param PoolType The type of memory to allocate.
576 @param AllocationSize The number of bytes to allocate and zero.
578 @return A pointer to the allocated buffer or NULL if allocation fails.
582 InternalAllocateZeroPool (
583 IN EFI_MEMORY_TYPE PoolType
,
584 IN UINTN AllocationSize
589 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
590 if (Memory
!= NULL
) {
591 Memory
= ZeroMem (Memory
, AllocationSize
);
597 Allocates and zeros a buffer of type EfiBootServicesData.
599 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
600 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
601 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
602 request, then NULL is returned.
604 @param AllocationSize The number of bytes to allocate and zero.
606 @return A pointer to the allocated buffer or NULL if allocation fails.
612 IN UINTN AllocationSize
617 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
618 if (Buffer
!= NULL
) {
619 MemoryProfileLibRecord (
620 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
621 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
632 Allocates and zeros a buffer of type EfiRuntimeServicesData.
634 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
635 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
636 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
637 request, then NULL is returned.
639 @param AllocationSize The number of bytes to allocate and zero.
641 @return A pointer to the allocated buffer or NULL if allocation fails.
646 AllocateRuntimeZeroPool (
647 IN UINTN AllocationSize
652 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
653 if (Buffer
!= NULL
) {
654 MemoryProfileLibRecord (
655 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
656 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
657 EfiRuntimeServicesData
,
667 Allocates and zeros a buffer of type EfiReservedMemoryType.
669 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
670 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
671 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
672 request, then NULL is returned.
674 @param AllocationSize The number of bytes to allocate and zero.
676 @return A pointer to the allocated buffer or NULL if allocation fails.
681 AllocateReservedZeroPool (
682 IN UINTN AllocationSize
687 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
688 if (Buffer
!= NULL
) {
689 MemoryProfileLibRecord (
690 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
691 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
692 EfiReservedMemoryType
,
702 Copies a buffer to an allocated buffer of a certain pool type.
704 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
705 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
706 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
707 is not enough memory remaining to satisfy the request, then NULL is returned.
708 If Buffer is NULL, then ASSERT().
709 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
711 @param PoolType The type of pool to allocate.
712 @param AllocationSize The number of bytes to allocate and zero.
713 @param Buffer The buffer to copy to the allocated buffer.
715 @return A pointer to the allocated buffer or NULL if allocation fails.
719 InternalAllocateCopyPool (
720 IN EFI_MEMORY_TYPE PoolType
,
721 IN UINTN AllocationSize
,
722 IN CONST VOID
*Buffer
727 ASSERT (Buffer
!= NULL
);
728 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
730 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
731 if (Memory
!= NULL
) {
732 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
738 Copies a buffer to an allocated buffer of type EfiBootServicesData.
740 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
741 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
742 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
743 is not enough memory remaining to satisfy the request, then NULL is returned.
745 If Buffer is NULL, then ASSERT().
746 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
748 @param AllocationSize The number of bytes to allocate and zero.
749 @param Buffer The buffer to copy to the allocated buffer.
751 @return A pointer to the allocated buffer or NULL if allocation fails.
757 IN UINTN AllocationSize
,
758 IN CONST VOID
*Buffer
763 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
764 if (NewBuffer
!= NULL
) {
765 MemoryProfileLibRecord (
766 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
767 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
778 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
780 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
781 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
782 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
783 is not enough memory remaining to satisfy the request, then NULL is returned.
785 If Buffer is NULL, then ASSERT().
786 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
788 @param AllocationSize The number of bytes to allocate and zero.
789 @param Buffer The buffer to copy to the allocated buffer.
791 @return A pointer to the allocated buffer or NULL if allocation fails.
796 AllocateRuntimeCopyPool (
797 IN UINTN AllocationSize
,
798 IN CONST VOID
*Buffer
803 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
804 if (NewBuffer
!= NULL
) {
805 MemoryProfileLibRecord (
806 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
807 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
808 EfiRuntimeServicesData
,
818 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
820 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
821 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
822 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
823 is not enough memory remaining to satisfy the request, then NULL is returned.
825 If Buffer is NULL, then ASSERT().
826 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
828 @param AllocationSize The number of bytes to allocate and zero.
829 @param Buffer The buffer to copy to the allocated buffer.
831 @return A pointer to the allocated buffer or NULL if allocation fails.
836 AllocateReservedCopyPool (
837 IN UINTN AllocationSize
,
838 IN CONST VOID
*Buffer
843 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
844 if (NewBuffer
!= NULL
) {
845 MemoryProfileLibRecord (
846 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
847 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
848 EfiRuntimeServicesData
,
858 Reallocates a buffer of a specified memory type.
860 Allocates and zeros the number bytes specified by NewSize from memory of the type
861 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
862 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
863 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
864 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
865 enough memory remaining to satisfy the request, then NULL is returned.
867 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
868 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
870 @param PoolType The type of pool to allocate.
871 @param OldSize The size, in bytes, of OldBuffer.
872 @param NewSize The size, in bytes, of the buffer to reallocate.
873 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
874 parameter that may be NULL.
876 @return A pointer to the allocated buffer or NULL if allocation fails.
880 InternalReallocatePool (
881 IN EFI_MEMORY_TYPE PoolType
,
884 IN VOID
*OldBuffer OPTIONAL
889 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
890 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
891 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
892 FreePool (OldBuffer
);
898 Reallocates a buffer of type EfiBootServicesData.
900 Allocates and zeros the number bytes specified by NewSize from memory of type
901 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
902 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
903 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
904 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
905 enough memory remaining to satisfy the request, then NULL is returned.
907 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
908 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
910 @param OldSize The size, in bytes, of OldBuffer.
911 @param NewSize The size, in bytes, of the buffer to reallocate.
912 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
913 parameter that may be NULL.
915 @return A pointer to the allocated buffer or NULL if allocation fails.
923 IN VOID
*OldBuffer OPTIONAL
928 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
929 if (Buffer
!= NULL
) {
930 MemoryProfileLibRecord (
931 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
932 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
943 Reallocates a buffer of type EfiRuntimeServicesData.
945 Allocates and zeros the number bytes specified by NewSize from memory of type
946 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
947 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
948 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
949 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
950 enough memory remaining to satisfy the request, then NULL is returned.
952 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
953 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
955 @param OldSize The size, in bytes, of OldBuffer.
956 @param NewSize The size, in bytes, of the buffer to reallocate.
957 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
958 parameter that may be NULL.
960 @return A pointer to the allocated buffer or NULL if allocation fails.
965 ReallocateRuntimePool (
968 IN VOID
*OldBuffer OPTIONAL
973 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
974 if (Buffer
!= NULL
) {
975 MemoryProfileLibRecord (
976 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
977 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
978 EfiRuntimeServicesData
,
988 Reallocates a buffer of type EfiReservedMemoryType.
990 Allocates and zeros the number bytes specified by NewSize from memory of type
991 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
992 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
993 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
994 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
995 enough memory remaining to satisfy the request, then NULL is returned.
997 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
998 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
1000 @param OldSize The size, in bytes, of OldBuffer.
1001 @param NewSize The size, in bytes, of the buffer to reallocate.
1002 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
1003 parameter that may be NULL.
1005 @return A pointer to the allocated buffer or NULL if allocation fails.
1010 ReallocateReservedPool (
1013 IN VOID
*OldBuffer OPTIONAL
1018 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1019 if (Buffer
!= NULL
) {
1020 MemoryProfileLibRecord (
1021 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
1022 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1023 EfiReservedMemoryType
,
1033 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1034 Memory Allocation Library.
1036 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1037 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1038 resources, then this function will perform no actions.
1040 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1043 @param Buffer The pointer to the buffer to free.
1054 Status
= gBS
->FreePool (Buffer
);
1055 ASSERT_EFI_ERROR (Status
);