2 Support routines for memory allocation routines based
3 on DxeCore Memory Allocation services for DxeCore,
4 with memory profile support.
6 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include "DxeCoreMemoryAllocationServices.h"
20 #include <Library/MemoryProfileLib.h>
23 Allocates one or more 4KB pages of a certain memory type.
25 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
26 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
27 If there is not enough memory remaining to satisfy the request, then NULL is returned.
29 @param MemoryType The type of memory to allocate.
30 @param Pages The number of 4 KB pages to allocate.
32 @return A pointer to the allocated buffer or NULL if allocation fails.
36 InternalAllocatePages (
37 IN EFI_MEMORY_TYPE MemoryType
,
42 EFI_PHYSICAL_ADDRESS Memory
;
48 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
49 if (EFI_ERROR (Status
)) {
52 return (VOID
*) (UINTN
) Memory
;
56 Allocates one or more 4KB pages of type EfiBootServicesData.
58 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
59 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
60 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
63 @param Pages The number of 4 KB pages to allocate.
65 @return A pointer to the allocated buffer or NULL if allocation fails.
76 Buffer
= InternalAllocatePages (EfiBootServicesData
, Pages
);
78 MemoryProfileLibRecord (
79 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
80 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES
,
83 EFI_PAGES_TO_SIZE (Pages
),
91 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
93 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
94 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
95 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
98 @param Pages The number of 4 KB pages to allocate.
100 @return A pointer to the allocated buffer or NULL if allocation fails.
105 AllocateRuntimePages (
111 Buffer
= InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
112 if (Buffer
!= NULL
) {
113 MemoryProfileLibRecord (
114 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
115 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES
,
116 EfiRuntimeServicesData
,
118 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
),
161 Frees one or more 4KB pages that were previously allocated with one of the page allocation
162 functions in the Memory Allocation Library.
164 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
165 must have been allocated on a previous call to the page allocation services of the Memory
166 Allocation Library. If it is not possible to free allocated pages, then this function will
169 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
171 If Pages is zero, then ASSERT().
173 @param Buffer Pointer to the buffer of pages to free.
174 @param Pages The number of 4 KB pages to free.
187 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
188 ASSERT_EFI_ERROR (Status
);
192 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
194 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
195 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
196 If there is not enough memory at the specified alignment remaining to satisfy the request, then
198 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
199 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
201 @param MemoryType The type of memory to allocate.
202 @param Pages The number of 4 KB pages to allocate.
203 @param Alignment The requested alignment of the allocation. Must be a power of two.
204 If Alignment is zero, then byte alignment is used.
206 @return A pointer to the allocated buffer or NULL if allocation fails.
210 InternalAllocateAlignedPages (
211 IN EFI_MEMORY_TYPE MemoryType
,
217 EFI_PHYSICAL_ADDRESS Memory
;
220 UINTN UnalignedPages
;
224 // Alignment must be a power of two or zero.
226 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
231 if (Alignment
> EFI_PAGE_SIZE
) {
233 // Calculate the total number of pages since alignment is larger than page size.
235 AlignmentMask
= Alignment
- 1;
236 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
238 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
240 ASSERT (RealPages
> Pages
);
242 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
243 if (EFI_ERROR (Status
)) {
246 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
247 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
248 if (UnalignedPages
> 0) {
250 // Free first unaligned page(s).
252 Status
= CoreFreePages (Memory
, UnalignedPages
);
253 ASSERT_EFI_ERROR (Status
);
255 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
256 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
257 if (UnalignedPages
> 0) {
259 // Free last unaligned page(s).
261 Status
= CoreFreePages (Memory
, UnalignedPages
);
262 ASSERT_EFI_ERROR (Status
);
266 // Do not over-allocate pages in this case.
268 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
269 if (EFI_ERROR (Status
)) {
272 AlignedMemory
= (UINTN
) Memory
;
274 return (VOID
*) AlignedMemory
;
278 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
280 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
281 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
282 returned. If there is not enough memory at the specified alignment remaining to satisfy the
283 request, then NULL is returned.
285 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
286 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
288 @param Pages The number of 4 KB pages to allocate.
289 @param Alignment The requested alignment of the allocation. Must be a power of two.
290 If Alignment is zero, then byte alignment is used.
292 @return A pointer to the allocated buffer or NULL if allocation fails.
297 AllocateAlignedPages (
304 Buffer
= InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
305 if (Buffer
!= NULL
) {
306 MemoryProfileLibRecord (
307 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
308 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES
,
311 EFI_PAGES_TO_SIZE (Pages
),
319 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
321 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
322 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
323 returned. If there is not enough memory at the specified alignment remaining to satisfy the
324 request, then NULL is returned.
326 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
327 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
329 @param Pages The number of 4 KB pages to allocate.
330 @param Alignment The requested alignment of the allocation. Must be a power of two.
331 If Alignment is zero, then byte alignment is used.
333 @return A pointer to the allocated buffer or NULL if allocation fails.
338 AllocateAlignedRuntimePages (
345 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
346 if (Buffer
!= NULL
) {
347 MemoryProfileLibRecord (
348 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
349 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
350 EfiRuntimeServicesData
,
352 EFI_PAGES_TO_SIZE (Pages
),
360 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
362 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
363 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
364 returned. If there is not enough memory at the specified alignment remaining to satisfy the
365 request, then NULL is returned.
367 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
368 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
370 @param Pages The number of 4 KB pages to allocate.
371 @param Alignment The requested alignment of the allocation. Must be a power of two.
372 If Alignment is zero, then byte alignment is used.
374 @return A pointer to the allocated buffer or NULL if allocation fails.
379 AllocateAlignedReservedPages (
386 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
387 if (Buffer
!= NULL
) {
388 MemoryProfileLibRecord (
389 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
390 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
391 EfiReservedMemoryType
,
393 EFI_PAGES_TO_SIZE (Pages
),
401 Frees one or more 4KB pages that were previously allocated with one of the aligned page
402 allocation functions in the Memory Allocation Library.
404 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
405 must have been allocated on a previous call to the aligned page allocation services of the Memory
406 Allocation Library. If it is not possible to free allocated pages, then this function will
409 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
410 Library, then ASSERT().
411 If Pages is zero, then ASSERT().
413 @param Buffer Pointer to the buffer of pages to free.
414 @param Pages The number of 4 KB pages to free.
427 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
428 ASSERT_EFI_ERROR (Status
);
432 Allocates a buffer of a certain pool type.
434 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
435 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
436 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
438 @param MemoryType The type of memory to allocate.
439 @param AllocationSize The number of bytes to allocate.
441 @return A pointer to the allocated buffer or NULL if allocation fails.
445 InternalAllocatePool (
446 IN EFI_MEMORY_TYPE MemoryType
,
447 IN UINTN AllocationSize
455 Status
= CoreAllocatePool (MemoryType
, AllocationSize
, &Memory
);
456 if (EFI_ERROR (Status
)) {
463 Allocates a buffer of type EfiBootServicesData.
465 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
466 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
467 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
469 @param AllocationSize The number of bytes to allocate.
471 @return A pointer to the allocated buffer or NULL if allocation fails.
477 IN UINTN AllocationSize
482 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
483 if (Buffer
!= NULL
) {
484 MemoryProfileLibRecord (
485 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
486 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
497 Allocates a buffer of type EfiRuntimeServicesData.
499 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
500 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
501 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
503 @param AllocationSize The number of bytes to allocate.
505 @return A pointer to the allocated buffer or NULL if allocation fails.
510 AllocateRuntimePool (
511 IN UINTN AllocationSize
516 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
517 if (Buffer
!= NULL
) {
518 MemoryProfileLibRecord (
519 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
520 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
521 EfiRuntimeServicesData
,
531 Allocates a buffer of type EfiReservedMemoryType.
533 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
534 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
535 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
537 @param AllocationSize The number of bytes to allocate.
539 @return A pointer to the allocated buffer or NULL if allocation fails.
544 AllocateReservedPool (
545 IN UINTN AllocationSize
550 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
551 if (Buffer
!= NULL
) {
552 MemoryProfileLibRecord (
553 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
554 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
555 EfiReservedMemoryType
,
565 Allocates and zeros a buffer of a certain pool type.
567 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
568 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
569 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
570 then NULL is returned.
572 @param PoolType The type of memory to allocate.
573 @param AllocationSize The number of bytes to allocate and zero.
575 @return A pointer to the allocated buffer or NULL if allocation fails.
579 InternalAllocateZeroPool (
580 IN EFI_MEMORY_TYPE PoolType
,
581 IN UINTN AllocationSize
586 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
587 if (Memory
!= NULL
) {
588 Memory
= ZeroMem (Memory
, AllocationSize
);
594 Allocates and zeros a buffer of type EfiBootServicesData.
596 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
597 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
598 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
599 request, then NULL is returned.
601 @param AllocationSize The number of bytes to allocate and zero.
603 @return A pointer to the allocated buffer or NULL if allocation fails.
609 IN UINTN AllocationSize
614 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
615 if (Buffer
!= NULL
) {
616 MemoryProfileLibRecord (
617 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
618 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
629 Allocates and zeros a buffer of type EfiRuntimeServicesData.
631 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
632 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
633 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
634 request, then NULL is returned.
636 @param AllocationSize The number of bytes to allocate and zero.
638 @return A pointer to the allocated buffer or NULL if allocation fails.
643 AllocateRuntimeZeroPool (
644 IN UINTN AllocationSize
649 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
650 if (Buffer
!= NULL
) {
651 MemoryProfileLibRecord (
652 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
653 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
654 EfiRuntimeServicesData
,
664 Allocates and zeros a buffer of type EfiReservedMemoryType.
666 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
667 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
668 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
669 request, then NULL is returned.
671 @param AllocationSize The number of bytes to allocate and zero.
673 @return A pointer to the allocated buffer or NULL if allocation fails.
678 AllocateReservedZeroPool (
679 IN UINTN AllocationSize
684 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
685 if (Buffer
!= NULL
) {
686 MemoryProfileLibRecord (
687 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
688 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
689 EfiReservedMemoryType
,
699 Copies a buffer to an allocated buffer of a certain pool type.
701 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
702 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
703 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
704 is not enough memory remaining to satisfy the request, then NULL is returned.
705 If Buffer is NULL, then ASSERT().
706 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
708 @param PoolType The type of pool to allocate.
709 @param AllocationSize The number of bytes to allocate and zero.
710 @param Buffer The buffer to copy to the allocated buffer.
712 @return A pointer to the allocated buffer or NULL if allocation fails.
716 InternalAllocateCopyPool (
717 IN EFI_MEMORY_TYPE PoolType
,
718 IN UINTN AllocationSize
,
719 IN CONST VOID
*Buffer
724 ASSERT (Buffer
!= NULL
);
725 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
727 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
728 if (Memory
!= NULL
) {
729 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
735 Copies a buffer to an allocated buffer of type EfiBootServicesData.
737 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
738 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
739 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
740 is not enough memory remaining to satisfy the request, then NULL is returned.
742 If Buffer is NULL, then ASSERT().
743 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
745 @param AllocationSize The number of bytes to allocate and zero.
746 @param Buffer The buffer to copy to the allocated buffer.
748 @return A pointer to the allocated buffer or NULL if allocation fails.
754 IN UINTN AllocationSize
,
755 IN CONST VOID
*Buffer
760 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
761 if (NewBuffer
!= NULL
) {
762 MemoryProfileLibRecord (
763 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
764 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
775 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
777 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
778 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
779 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
780 is not enough memory remaining to satisfy the request, then NULL is returned.
782 If Buffer is NULL, then ASSERT().
783 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
785 @param AllocationSize The number of bytes to allocate and zero.
786 @param Buffer The buffer to copy to the allocated buffer.
788 @return A pointer to the allocated buffer or NULL if allocation fails.
793 AllocateRuntimeCopyPool (
794 IN UINTN AllocationSize
,
795 IN CONST VOID
*Buffer
800 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
801 if (NewBuffer
!= NULL
) {
802 MemoryProfileLibRecord (
803 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
804 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
805 EfiRuntimeServicesData
,
815 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
817 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
818 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
819 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
820 is not enough memory remaining to satisfy the request, then NULL is returned.
822 If Buffer is NULL, then ASSERT().
823 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
825 @param AllocationSize The number of bytes to allocate and zero.
826 @param Buffer The buffer to copy to the allocated buffer.
828 @return A pointer to the allocated buffer or NULL if allocation fails.
833 AllocateReservedCopyPool (
834 IN UINTN AllocationSize
,
835 IN CONST VOID
*Buffer
840 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
841 if (NewBuffer
!= NULL
) {
842 MemoryProfileLibRecord (
843 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
844 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
845 EfiRuntimeServicesData
,
855 Reallocates a buffer of a specified memory type.
857 Allocates and zeros the number bytes specified by NewSize from memory of the type
858 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
859 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
860 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
861 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
862 enough memory remaining to satisfy the request, then NULL is returned.
864 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
865 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
867 @param PoolType The type of pool to allocate.
868 @param OldSize The size, in bytes, of OldBuffer.
869 @param NewSize The size, in bytes, of the buffer to reallocate.
870 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
871 parameter that may be NULL.
873 @return A pointer to the allocated buffer or NULL if allocation fails.
877 InternalReallocatePool (
878 IN EFI_MEMORY_TYPE PoolType
,
881 IN VOID
*OldBuffer OPTIONAL
886 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
887 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
888 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
889 FreePool (OldBuffer
);
895 Reallocates a buffer of type EfiBootServicesData.
897 Allocates and zeros the number bytes specified by NewSize from memory of type
898 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
899 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
900 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
901 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
902 enough memory remaining to satisfy the request, then NULL is returned.
904 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
905 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
907 @param OldSize The size, in bytes, of OldBuffer.
908 @param NewSize The size, in bytes, of the buffer to reallocate.
909 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
910 parameter that may be NULL.
912 @return A pointer to the allocated buffer or NULL if allocation fails.
920 IN VOID
*OldBuffer OPTIONAL
925 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
926 if (Buffer
!= NULL
) {
927 MemoryProfileLibRecord (
928 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
929 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
940 Reallocates a buffer of type EfiRuntimeServicesData.
942 Allocates and zeros the number bytes specified by NewSize from memory of type
943 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
944 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
945 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
946 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
947 enough memory remaining to satisfy the request, then NULL is returned.
949 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
950 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
952 @param OldSize The size, in bytes, of OldBuffer.
953 @param NewSize The size, in bytes, of the buffer to reallocate.
954 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
955 parameter that may be NULL.
957 @return A pointer to the allocated buffer or NULL if allocation fails.
962 ReallocateRuntimePool (
965 IN VOID
*OldBuffer OPTIONAL
970 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
971 if (Buffer
!= NULL
) {
972 MemoryProfileLibRecord (
973 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
974 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
975 EfiRuntimeServicesData
,
985 Reallocates a buffer of type EfiReservedMemoryType.
987 Allocates and zeros the number bytes specified by NewSize from memory of type
988 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
989 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
990 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
991 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
992 enough memory remaining to satisfy the request, then NULL is returned.
994 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
995 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
997 @param OldSize The size, in bytes, of OldBuffer.
998 @param NewSize The size, in bytes, of the buffer to reallocate.
999 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
1000 parameter that may be NULL.
1002 @return A pointer to the allocated buffer or NULL if allocation fails.
1007 ReallocateReservedPool (
1010 IN VOID
*OldBuffer OPTIONAL
1015 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1016 if (Buffer
!= NULL
) {
1017 MemoryProfileLibRecord (
1018 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
1019 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1020 EfiReservedMemoryType
,
1030 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1031 Memory Allocation Library.
1033 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1034 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1035 resources, then this function will perform no actions.
1037 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1040 @param Buffer Pointer to the buffer to free.
1051 Status
= CoreFreePool (Buffer
);
1052 ASSERT_EFI_ERROR (Status
);