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
13 #include <Library/MemoryAllocationLib.h>
14 #include <Library/BaseMemoryLib.h>
15 #include <Library/DebugLib.h>
16 #include "DxeCoreMemoryAllocationServices.h"
18 #include <Library/MemoryProfileLib.h>
21 Allocates one or more 4KB pages of a certain memory type.
23 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
24 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
25 If there is not enough memory remaining to satisfy the request, then NULL is returned.
27 @param MemoryType The type of memory to allocate.
28 @param Pages The number of 4 KB pages to allocate.
30 @return A pointer to the allocated buffer or NULL if allocation fails.
34 InternalAllocatePages (
35 IN EFI_MEMORY_TYPE MemoryType
,
40 EFI_PHYSICAL_ADDRESS Memory
;
46 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
47 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
),
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
),
127 Allocates one or more 4KB pages of type EfiReservedMemoryType.
129 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
130 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
131 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
134 @param Pages The number of 4 KB pages to allocate.
136 @return A pointer to the allocated buffer or NULL if allocation fails.
141 AllocateReservedPages (
147 Buffer
= InternalAllocatePages (EfiReservedMemoryType
, Pages
);
148 if (Buffer
!= NULL
) {
149 MemoryProfileLibRecord (
150 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
151 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES
,
152 EfiReservedMemoryType
,
154 EFI_PAGES_TO_SIZE (Pages
),
163 Frees one or more 4KB pages that were previously allocated with one of the page allocation
164 functions in the Memory Allocation Library.
166 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
167 must have been allocated on a previous call to the page allocation services of the Memory
168 Allocation Library. If it is not possible to free allocated pages, then this function will
171 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
173 If Pages is zero, then ASSERT().
175 @param Buffer Pointer to the buffer of pages to free.
176 @param Pages The number of 4 KB pages to free.
189 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, Pages
);
190 ASSERT_EFI_ERROR (Status
);
194 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
196 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
197 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
198 If there is not enough memory at the specified alignment remaining to satisfy the request, then
200 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
201 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
203 @param MemoryType The type of memory to allocate.
204 @param Pages The number of 4 KB pages to allocate.
205 @param Alignment The requested alignment of the allocation. Must be a power of two.
206 If Alignment is zero, then byte alignment is used.
208 @return A pointer to the allocated buffer or NULL if allocation fails.
212 InternalAllocateAlignedPages (
213 IN EFI_MEMORY_TYPE MemoryType
,
219 EFI_PHYSICAL_ADDRESS Memory
;
222 UINTN UnalignedPages
;
226 // Alignment must be a power of two or zero.
228 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
234 if (Alignment
> EFI_PAGE_SIZE
) {
236 // Calculate the total number of pages since alignment is larger than page size.
238 AlignmentMask
= Alignment
- 1;
239 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
241 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
243 ASSERT (RealPages
> Pages
);
245 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
246 if (EFI_ERROR (Status
)) {
250 AlignedMemory
= ((UINTN
)Memory
+ AlignmentMask
) & ~AlignmentMask
;
251 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
)Memory
);
252 if (UnalignedPages
> 0) {
254 // Free first unaligned page(s).
256 Status
= CoreFreePages (Memory
, UnalignedPages
);
257 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
= CoreFreePages (Memory
, UnalignedPages
);
267 ASSERT_EFI_ERROR (Status
);
271 // Do not over-allocate pages in this case.
273 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
274 if (EFI_ERROR (Status
)) {
278 AlignedMemory
= (UINTN
)Memory
;
281 return (VOID
*)AlignedMemory
;
285 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
287 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
288 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
289 returned. If there is not enough memory at the specified alignment remaining to satisfy the
290 request, then NULL is returned.
292 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
293 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
295 @param Pages The number of 4 KB pages to allocate.
296 @param Alignment The requested alignment of the allocation. Must be a power of two.
297 If Alignment is zero, then byte alignment is used.
299 @return A pointer to the allocated buffer or NULL if allocation fails.
304 AllocateAlignedPages (
311 Buffer
= InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
312 if (Buffer
!= NULL
) {
313 MemoryProfileLibRecord (
314 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
315 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_PAGES
,
318 EFI_PAGES_TO_SIZE (Pages
),
327 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
329 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
330 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
331 returned. If there is not enough memory at the specified alignment remaining to satisfy the
332 request, then NULL is returned.
334 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
335 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
337 @param Pages The number of 4 KB pages to allocate.
338 @param Alignment The requested alignment of the allocation. Must be a power of two.
339 If Alignment is zero, then byte alignment is used.
341 @return A pointer to the allocated buffer or NULL if allocation fails.
346 AllocateAlignedRuntimePages (
353 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
354 if (Buffer
!= NULL
) {
355 MemoryProfileLibRecord (
356 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
357 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
358 EfiRuntimeServicesData
,
360 EFI_PAGES_TO_SIZE (Pages
),
369 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
371 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
372 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
373 returned. If there is not enough memory at the specified alignment remaining to satisfy the
374 request, then NULL is returned.
376 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
377 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
379 @param Pages The number of 4 KB pages to allocate.
380 @param Alignment The requested alignment of the allocation. Must be a power of two.
381 If Alignment is zero, then byte alignment is used.
383 @return A pointer to the allocated buffer or NULL if allocation fails.
388 AllocateAlignedReservedPages (
395 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
396 if (Buffer
!= NULL
) {
397 MemoryProfileLibRecord (
398 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
399 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
400 EfiReservedMemoryType
,
402 EFI_PAGES_TO_SIZE (Pages
),
411 Frees one or more 4KB pages that were previously allocated with one of the aligned page
412 allocation functions in the Memory Allocation Library.
414 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
415 must have been allocated on a previous call to the aligned page allocation services of the Memory
416 Allocation Library. If it is not possible to free allocated pages, then this function will
419 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
420 Library, then ASSERT().
421 If Pages is zero, then ASSERT().
423 @param Buffer Pointer to the buffer of pages to free.
424 @param Pages The number of 4 KB pages to free.
437 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Buffer
, Pages
);
438 ASSERT_EFI_ERROR (Status
);
442 Allocates a buffer of a certain pool type.
444 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
445 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
446 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
448 @param MemoryType The type of memory to allocate.
449 @param AllocationSize The number of bytes to allocate.
451 @return A pointer to the allocated buffer or NULL if allocation fails.
455 InternalAllocatePool (
456 IN EFI_MEMORY_TYPE MemoryType
,
457 IN UINTN AllocationSize
465 Status
= CoreAllocatePool (MemoryType
, AllocationSize
, &Memory
);
466 if (EFI_ERROR (Status
)) {
474 Allocates a buffer of type EfiBootServicesData.
476 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
477 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
478 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
480 @param AllocationSize The number of bytes to allocate.
482 @return A pointer to the allocated buffer or NULL if allocation fails.
488 IN UINTN AllocationSize
493 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
494 if (Buffer
!= NULL
) {
495 MemoryProfileLibRecord (
496 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
497 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
509 Allocates a buffer of type EfiRuntimeServicesData.
511 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
512 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
513 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
515 @param AllocationSize The number of bytes to allocate.
517 @return A pointer to the allocated buffer or NULL if allocation fails.
522 AllocateRuntimePool (
523 IN UINTN AllocationSize
528 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
529 if (Buffer
!= NULL
) {
530 MemoryProfileLibRecord (
531 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
532 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
533 EfiRuntimeServicesData
,
544 Allocates a buffer of type EfiReservedMemoryType.
546 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
547 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
548 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
550 @param AllocationSize The number of bytes to allocate.
552 @return A pointer to the allocated buffer or NULL if allocation fails.
557 AllocateReservedPool (
558 IN UINTN AllocationSize
563 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
564 if (Buffer
!= NULL
) {
565 MemoryProfileLibRecord (
566 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
567 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
568 EfiReservedMemoryType
,
579 Allocates and zeros a buffer of a certain pool type.
581 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
582 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
583 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
584 then NULL is returned.
586 @param PoolType The type of memory to allocate.
587 @param AllocationSize The number of bytes to allocate and zero.
589 @return A pointer to the allocated buffer or NULL if allocation fails.
593 InternalAllocateZeroPool (
594 IN EFI_MEMORY_TYPE PoolType
,
595 IN UINTN AllocationSize
600 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
601 if (Memory
!= NULL
) {
602 Memory
= ZeroMem (Memory
, AllocationSize
);
609 Allocates and zeros a buffer of type EfiBootServicesData.
611 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
612 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
613 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
614 request, then NULL is returned.
616 @param AllocationSize The number of bytes to allocate and zero.
618 @return A pointer to the allocated buffer or NULL if allocation fails.
624 IN UINTN AllocationSize
629 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
630 if (Buffer
!= NULL
) {
631 MemoryProfileLibRecord (
632 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
633 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
645 Allocates and zeros a buffer of type EfiRuntimeServicesData.
647 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
648 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
649 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
650 request, then NULL is returned.
652 @param AllocationSize The number of bytes to allocate and zero.
654 @return A pointer to the allocated buffer or NULL if allocation fails.
659 AllocateRuntimeZeroPool (
660 IN UINTN AllocationSize
665 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
666 if (Buffer
!= NULL
) {
667 MemoryProfileLibRecord (
668 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
669 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
670 EfiRuntimeServicesData
,
681 Allocates and zeros a buffer of type EfiReservedMemoryType.
683 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
684 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
685 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
686 request, then NULL is returned.
688 @param AllocationSize The number of bytes to allocate and zero.
690 @return A pointer to the allocated buffer or NULL if allocation fails.
695 AllocateReservedZeroPool (
696 IN UINTN AllocationSize
701 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
702 if (Buffer
!= NULL
) {
703 MemoryProfileLibRecord (
704 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
705 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
706 EfiReservedMemoryType
,
717 Copies a buffer to an allocated buffer of a certain pool type.
719 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
720 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
721 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
722 is not enough memory remaining to satisfy the request, then NULL is returned.
723 If Buffer is NULL, then ASSERT().
724 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
726 @param PoolType The type of pool to allocate.
727 @param AllocationSize The number of bytes to allocate and zero.
728 @param Buffer The buffer to copy to the allocated buffer.
730 @return A pointer to the allocated buffer or NULL if allocation fails.
734 InternalAllocateCopyPool (
735 IN EFI_MEMORY_TYPE PoolType
,
736 IN UINTN AllocationSize
,
737 IN CONST VOID
*Buffer
742 ASSERT (Buffer
!= NULL
);
743 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
)Buffer
+ 1));
745 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
746 if (Memory
!= NULL
) {
747 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
754 Copies a buffer to an allocated buffer of type EfiBootServicesData.
756 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
757 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
758 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
759 is not enough memory remaining to satisfy the request, then NULL is returned.
761 If Buffer is NULL, then ASSERT().
762 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
764 @param AllocationSize The number of bytes to allocate and zero.
765 @param Buffer The buffer to copy to the allocated buffer.
767 @return A pointer to the allocated buffer or NULL if allocation fails.
773 IN UINTN AllocationSize
,
774 IN CONST VOID
*Buffer
779 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
780 if (NewBuffer
!= NULL
) {
781 MemoryProfileLibRecord (
782 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
783 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
795 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
797 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
798 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
799 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
800 is not enough memory remaining to satisfy the request, then NULL is returned.
802 If Buffer is NULL, then ASSERT().
803 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
805 @param AllocationSize The number of bytes to allocate and zero.
806 @param Buffer The buffer to copy to the allocated buffer.
808 @return A pointer to the allocated buffer or NULL if allocation fails.
813 AllocateRuntimeCopyPool (
814 IN UINTN AllocationSize
,
815 IN CONST VOID
*Buffer
820 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
821 if (NewBuffer
!= NULL
) {
822 MemoryProfileLibRecord (
823 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
824 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
825 EfiRuntimeServicesData
,
836 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
838 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
839 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
840 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
841 is not enough memory remaining to satisfy the request, then NULL is returned.
843 If Buffer is NULL, then ASSERT().
844 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
846 @param AllocationSize The number of bytes to allocate and zero.
847 @param Buffer The buffer to copy to the allocated buffer.
849 @return A pointer to the allocated buffer or NULL if allocation fails.
854 AllocateReservedCopyPool (
855 IN UINTN AllocationSize
,
856 IN CONST VOID
*Buffer
861 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
862 if (NewBuffer
!= NULL
) {
863 MemoryProfileLibRecord (
864 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
865 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
866 EfiRuntimeServicesData
,
877 Reallocates a buffer of a specified memory type.
879 Allocates and zeros the number bytes specified by NewSize from memory of the type
880 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
881 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
882 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
883 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
884 enough memory remaining to satisfy the request, then NULL is returned.
886 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
887 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
889 @param PoolType The type of pool to allocate.
890 @param OldSize The size, in bytes, of OldBuffer.
891 @param NewSize The size, in bytes, of the buffer to reallocate.
892 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
893 parameter that may be NULL.
895 @return A pointer to the allocated buffer or NULL if allocation fails.
899 InternalReallocatePool (
900 IN EFI_MEMORY_TYPE PoolType
,
903 IN VOID
*OldBuffer OPTIONAL
908 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
909 if ((NewBuffer
!= NULL
) && (OldBuffer
!= NULL
)) {
910 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
911 FreePool (OldBuffer
);
918 Reallocates a buffer of type EfiBootServicesData.
920 Allocates and zeros the number bytes specified by NewSize from memory of type
921 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
922 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
923 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
924 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
925 enough memory remaining to satisfy the request, then NULL is returned.
927 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
928 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
930 @param OldSize The size, in bytes, of OldBuffer.
931 @param NewSize The size, in bytes, of the buffer to reallocate.
932 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
933 parameter that may be NULL.
935 @return A pointer to the allocated buffer or NULL if allocation fails.
943 IN VOID
*OldBuffer OPTIONAL
948 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
949 if (Buffer
!= NULL
) {
950 MemoryProfileLibRecord (
951 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
952 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
964 Reallocates a buffer of type EfiRuntimeServicesData.
966 Allocates and zeros the number bytes specified by NewSize from memory of type
967 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
968 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
969 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
970 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
971 enough memory remaining to satisfy the request, then NULL is returned.
973 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
974 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
976 @param OldSize The size, in bytes, of OldBuffer.
977 @param NewSize The size, in bytes, of the buffer to reallocate.
978 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
979 parameter that may be NULL.
981 @return A pointer to the allocated buffer or NULL if allocation fails.
986 ReallocateRuntimePool (
989 IN VOID
*OldBuffer OPTIONAL
994 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
995 if (Buffer
!= NULL
) {
996 MemoryProfileLibRecord (
997 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
998 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
999 EfiRuntimeServicesData
,
1010 Reallocates a buffer of type EfiReservedMemoryType.
1012 Allocates and zeros the number bytes specified by NewSize from memory of type
1013 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
1014 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
1015 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
1016 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
1017 enough memory remaining to satisfy the request, then NULL is returned.
1019 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
1020 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
1022 @param OldSize The size, in bytes, of OldBuffer.
1023 @param NewSize The size, in bytes, of the buffer to reallocate.
1024 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
1025 parameter that may be NULL.
1027 @return A pointer to the allocated buffer or NULL if allocation fails.
1032 ReallocateReservedPool (
1035 IN VOID
*OldBuffer OPTIONAL
1040 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1041 if (Buffer
!= NULL
) {
1042 MemoryProfileLibRecord (
1043 (PHYSICAL_ADDRESS
)(UINTN
)RETURN_ADDRESS (0),
1044 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1045 EfiReservedMemoryType
,
1056 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1057 Memory Allocation Library.
1059 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1060 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1061 resources, then this function will perform no actions.
1063 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1066 @param Buffer Pointer to the buffer to free.
1077 Status
= CoreFreePool (Buffer
);
1078 ASSERT_EFI_ERROR (Status
);