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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include "DxeCoreMemoryAllocationServices.h"
26 #include <Library/MemoryProfileLib.h>
29 Allocates one or more 4KB pages of a certain memory type.
31 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
32 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
33 If there is not enough memory remaining to satisfy the request, then NULL is returned.
35 @param MemoryType The type of memory to allocate.
36 @param Pages The number of 4 KB pages to allocate.
38 @return A pointer to the allocated buffer or NULL if allocation fails.
42 InternalAllocatePages (
43 IN EFI_MEMORY_TYPE MemoryType
,
48 EFI_PHYSICAL_ADDRESS Memory
;
54 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
55 if (EFI_ERROR (Status
)) {
58 return (VOID
*) (UINTN
) Memory
;
62 Allocates one or more 4KB pages of type EfiBootServicesData.
64 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
65 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
66 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
69 @param Pages The number of 4 KB pages to allocate.
71 @return A pointer to the allocated buffer or NULL if allocation fails.
82 Buffer
= InternalAllocatePages (EfiBootServicesData
, Pages
);
84 MemoryProfileLibRecord (
85 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
86 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_PAGES
,
89 EFI_PAGES_TO_SIZE (Pages
),
97 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
99 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
100 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
101 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
104 @param Pages The number of 4 KB pages to allocate.
106 @return A pointer to the allocated buffer or NULL if allocation fails.
111 AllocateRuntimePages (
117 Buffer
= InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
118 if (Buffer
!= NULL
) {
119 MemoryProfileLibRecord (
120 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
121 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_PAGES
,
122 EfiRuntimeServicesData
,
124 EFI_PAGES_TO_SIZE (Pages
),
132 Allocates one or more 4KB pages of type EfiReservedMemoryType.
134 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
135 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
136 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
139 @param Pages The number of 4 KB pages to allocate.
141 @return A pointer to the allocated buffer or NULL if allocation fails.
146 AllocateReservedPages (
152 Buffer
= InternalAllocatePages (EfiReservedMemoryType
, Pages
);
153 if (Buffer
!= NULL
) {
154 MemoryProfileLibRecord (
155 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
156 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_PAGES
,
157 EfiReservedMemoryType
,
159 EFI_PAGES_TO_SIZE (Pages
),
167 Frees one or more 4KB pages that were previously allocated with one of the page allocation
168 functions in the Memory Allocation Library.
170 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
171 must have been allocated on a previous call to the page allocation services of the Memory
172 Allocation Library. If it is not possible to free allocated pages, then this function will
175 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
177 If Pages is zero, then ASSERT().
179 @param Buffer Pointer to the buffer of pages to free.
180 @param Pages The number of 4 KB pages to free.
193 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
194 ASSERT_EFI_ERROR (Status
);
198 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
200 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
201 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
202 If there is not enough memory at the specified alignment remaining to satisfy the request, then
204 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
205 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
207 @param MemoryType The type of memory to allocate.
208 @param Pages The number of 4 KB pages to allocate.
209 @param Alignment The requested alignment of the allocation. Must be a power of two.
210 If Alignment is zero, then byte alignment is used.
212 @return A pointer to the allocated buffer or NULL if allocation fails.
216 InternalAllocateAlignedPages (
217 IN EFI_MEMORY_TYPE MemoryType
,
223 EFI_PHYSICAL_ADDRESS Memory
;
226 UINTN UnalignedPages
;
230 // Alignment must be a power of two or zero.
232 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
237 if (Alignment
> EFI_PAGE_SIZE
) {
239 // Calculate the total number of pages since alignment is larger than page size.
241 AlignmentMask
= Alignment
- 1;
242 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
244 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
246 ASSERT (RealPages
> Pages
);
248 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
249 if (EFI_ERROR (Status
)) {
252 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
253 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
254 if (UnalignedPages
> 0) {
256 // Free first unaligned page(s).
258 Status
= CoreFreePages (Memory
, UnalignedPages
);
259 ASSERT_EFI_ERROR (Status
);
261 Memory
= AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
);
262 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
263 if (UnalignedPages
> 0) {
265 // Free last unaligned page(s).
267 Status
= CoreFreePages (Memory
, UnalignedPages
);
268 ASSERT_EFI_ERROR (Status
);
272 // Do not over-allocate pages in this case.
274 Status
= CoreAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
275 if (EFI_ERROR (Status
)) {
278 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
),
325 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
327 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
328 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
329 returned. If there is not enough memory at the specified alignment remaining to satisfy the
330 request, then NULL is returned.
332 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
333 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
335 @param Pages The number of 4 KB pages to allocate.
336 @param Alignment The requested alignment of the allocation. Must be a power of two.
337 If Alignment is zero, then byte alignment is used.
339 @return A pointer to the allocated buffer or NULL if allocation fails.
344 AllocateAlignedRuntimePages (
351 Buffer
= InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
352 if (Buffer
!= NULL
) {
353 MemoryProfileLibRecord (
354 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
355 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RUNTIME_PAGES
,
356 EfiRuntimeServicesData
,
358 EFI_PAGES_TO_SIZE (Pages
),
366 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
368 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
369 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
370 returned. If there is not enough memory at the specified alignment remaining to satisfy the
371 request, then NULL is returned.
373 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
374 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
376 @param Pages The number of 4 KB pages to allocate.
377 @param Alignment The requested alignment of the allocation. Must be a power of two.
378 If Alignment is zero, then byte alignment is used.
380 @return A pointer to the allocated buffer or NULL if allocation fails.
385 AllocateAlignedReservedPages (
392 Buffer
= InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
393 if (Buffer
!= NULL
) {
394 MemoryProfileLibRecord (
395 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
396 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ALIGNED_RESERVED_PAGES
,
397 EfiReservedMemoryType
,
399 EFI_PAGES_TO_SIZE (Pages
),
407 Frees one or more 4KB pages that were previously allocated with one of the aligned page
408 allocation functions in the Memory Allocation Library.
410 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
411 must have been allocated on a previous call to the aligned page allocation services of the Memory
412 Allocation Library. If it is not possible to free allocated pages, then this function will
415 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
416 Library, then ASSERT().
417 If Pages is zero, then ASSERT().
419 @param Buffer Pointer to the buffer of pages to free.
420 @param Pages The number of 4 KB pages to free.
433 Status
= CoreFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
434 ASSERT_EFI_ERROR (Status
);
438 Allocates a buffer of a certain pool type.
440 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
441 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
442 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
444 @param MemoryType The type of memory to allocate.
445 @param AllocationSize The number of bytes to allocate.
447 @return A pointer to the allocated buffer or NULL if allocation fails.
451 InternalAllocatePool (
452 IN EFI_MEMORY_TYPE MemoryType
,
453 IN UINTN AllocationSize
461 Status
= CoreAllocatePool (MemoryType
, AllocationSize
, &Memory
);
462 if (EFI_ERROR (Status
)) {
469 Allocates a buffer of type EfiBootServicesData.
471 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
472 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
473 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
475 @param AllocationSize The number of bytes to allocate.
477 @return A pointer to the allocated buffer or NULL if allocation fails.
483 IN UINTN AllocationSize
488 Buffer
= InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
489 if (Buffer
!= NULL
) {
490 MemoryProfileLibRecord (
491 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
492 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_POOL
,
503 Allocates a buffer of type EfiRuntimeServicesData.
505 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
506 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
507 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
509 @param AllocationSize The number of bytes to allocate.
511 @return A pointer to the allocated buffer or NULL if allocation fails.
516 AllocateRuntimePool (
517 IN UINTN AllocationSize
522 Buffer
= InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
523 if (Buffer
!= NULL
) {
524 MemoryProfileLibRecord (
525 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
526 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_POOL
,
527 EfiRuntimeServicesData
,
537 Allocates a buffer of type EfiReservedMemoryType.
539 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
540 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
541 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
543 @param AllocationSize The number of bytes to allocate.
545 @return A pointer to the allocated buffer or NULL if allocation fails.
550 AllocateReservedPool (
551 IN UINTN AllocationSize
556 Buffer
= InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
557 if (Buffer
!= NULL
) {
558 MemoryProfileLibRecord (
559 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
560 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_POOL
,
561 EfiReservedMemoryType
,
571 Allocates and zeros a buffer of a certain pool type.
573 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
574 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
575 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
576 then NULL is returned.
578 @param PoolType The type of memory to allocate.
579 @param AllocationSize The number of bytes to allocate and zero.
581 @return A pointer to the allocated buffer or NULL if allocation fails.
585 InternalAllocateZeroPool (
586 IN EFI_MEMORY_TYPE PoolType
,
587 IN UINTN AllocationSize
592 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
593 if (Memory
!= NULL
) {
594 Memory
= ZeroMem (Memory
, AllocationSize
);
600 Allocates and zeros a buffer of type EfiBootServicesData.
602 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
603 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
604 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
605 request, then NULL is returned.
607 @param AllocationSize The number of bytes to allocate and zero.
609 @return A pointer to the allocated buffer or NULL if allocation fails.
615 IN UINTN AllocationSize
620 Buffer
= InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
621 if (Buffer
!= NULL
) {
622 MemoryProfileLibRecord (
623 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
624 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_ZERO_POOL
,
635 Allocates and zeros a buffer of type EfiRuntimeServicesData.
637 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
638 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
639 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
640 request, then NULL is returned.
642 @param AllocationSize The number of bytes to allocate and zero.
644 @return A pointer to the allocated buffer or NULL if allocation fails.
649 AllocateRuntimeZeroPool (
650 IN UINTN AllocationSize
655 Buffer
= InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
656 if (Buffer
!= NULL
) {
657 MemoryProfileLibRecord (
658 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
659 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_ZERO_POOL
,
660 EfiRuntimeServicesData
,
670 Allocates and zeros a buffer of type EfiReservedMemoryType.
672 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
673 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
674 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
675 request, then NULL is returned.
677 @param AllocationSize The number of bytes to allocate and zero.
679 @return A pointer to the allocated buffer or NULL if allocation fails.
684 AllocateReservedZeroPool (
685 IN UINTN AllocationSize
690 Buffer
= InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
691 if (Buffer
!= NULL
) {
692 MemoryProfileLibRecord (
693 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
694 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_ZERO_POOL
,
695 EfiReservedMemoryType
,
705 Copies a buffer to an allocated buffer of a certain pool type.
707 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
708 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
709 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
710 is not enough memory remaining to satisfy the request, then NULL is returned.
711 If Buffer is NULL, then ASSERT().
712 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
714 @param PoolType The type of pool to allocate.
715 @param AllocationSize The number of bytes to allocate and zero.
716 @param Buffer The buffer to copy to the allocated buffer.
718 @return A pointer to the allocated buffer or NULL if allocation fails.
722 InternalAllocateCopyPool (
723 IN EFI_MEMORY_TYPE PoolType
,
724 IN UINTN AllocationSize
,
725 IN CONST VOID
*Buffer
730 ASSERT (Buffer
!= NULL
);
731 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
733 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
734 if (Memory
!= NULL
) {
735 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
741 Copies a buffer to an allocated buffer of type EfiBootServicesData.
743 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
744 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
745 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
746 is not enough memory remaining to satisfy the request, then NULL is returned.
748 If Buffer is NULL, then ASSERT().
749 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
751 @param AllocationSize The number of bytes to allocate and zero.
752 @param Buffer The buffer to copy to the allocated buffer.
754 @return A pointer to the allocated buffer or NULL if allocation fails.
760 IN UINTN AllocationSize
,
761 IN CONST VOID
*Buffer
766 NewBuffer
= InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
767 if (NewBuffer
!= NULL
) {
768 MemoryProfileLibRecord (
769 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
770 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_COPY_POOL
,
781 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
783 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
784 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
785 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
786 is not enough memory remaining to satisfy the request, then NULL is returned.
788 If Buffer is NULL, then ASSERT().
789 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
791 @param AllocationSize The number of bytes to allocate and zero.
792 @param Buffer The buffer to copy to the allocated buffer.
794 @return A pointer to the allocated buffer or NULL if allocation fails.
799 AllocateRuntimeCopyPool (
800 IN UINTN AllocationSize
,
801 IN CONST VOID
*Buffer
806 NewBuffer
= InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
807 if (NewBuffer
!= NULL
) {
808 MemoryProfileLibRecord (
809 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
810 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RUNTIME_COPY_POOL
,
811 EfiRuntimeServicesData
,
821 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
823 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
824 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
825 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
826 is not enough memory remaining to satisfy the request, then NULL is returned.
828 If Buffer is NULL, then ASSERT().
829 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
831 @param AllocationSize The number of bytes to allocate and zero.
832 @param Buffer The buffer to copy to the allocated buffer.
834 @return A pointer to the allocated buffer or NULL if allocation fails.
839 AllocateReservedCopyPool (
840 IN UINTN AllocationSize
,
841 IN CONST VOID
*Buffer
846 NewBuffer
= InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
847 if (NewBuffer
!= NULL
) {
848 MemoryProfileLibRecord (
849 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
850 MEMORY_PROFILE_ACTION_LIB_ALLOCATE_RESERVED_COPY_POOL
,
851 EfiRuntimeServicesData
,
861 Reallocates a buffer of a specified memory type.
863 Allocates and zeros the number bytes specified by NewSize from memory of the type
864 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
865 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
866 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
867 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
868 enough memory remaining to satisfy the request, then NULL is returned.
870 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
871 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
873 @param PoolType The type of pool to allocate.
874 @param OldSize The size, in bytes, of OldBuffer.
875 @param NewSize The size, in bytes, of the buffer to reallocate.
876 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
877 parameter that may be NULL.
879 @return A pointer to the allocated buffer or NULL if allocation fails.
883 InternalReallocatePool (
884 IN EFI_MEMORY_TYPE PoolType
,
887 IN VOID
*OldBuffer OPTIONAL
892 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
893 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
894 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
895 FreePool (OldBuffer
);
901 Reallocates a buffer of type EfiBootServicesData.
903 Allocates and zeros the number bytes specified by NewSize from memory of type
904 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
905 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
906 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
907 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
908 enough memory remaining to satisfy the request, then NULL is returned.
910 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
911 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
913 @param OldSize The size, in bytes, of OldBuffer.
914 @param NewSize The size, in bytes, of the buffer to reallocate.
915 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
916 parameter that may be NULL.
918 @return A pointer to the allocated buffer or NULL if allocation fails.
926 IN VOID
*OldBuffer OPTIONAL
931 Buffer
= InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
932 if (Buffer
!= NULL
) {
933 MemoryProfileLibRecord (
934 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
935 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_POOL
,
946 Reallocates a buffer of type EfiRuntimeServicesData.
948 Allocates and zeros the number bytes specified by NewSize from memory of type
949 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
950 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
951 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
952 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
953 enough memory remaining to satisfy the request, then NULL is returned.
955 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
956 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
958 @param OldSize The size, in bytes, of OldBuffer.
959 @param NewSize The size, in bytes, of the buffer to reallocate.
960 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
961 parameter that may be NULL.
963 @return A pointer to the allocated buffer or NULL if allocation fails.
968 ReallocateRuntimePool (
971 IN VOID
*OldBuffer OPTIONAL
976 Buffer
= InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
977 if (Buffer
!= NULL
) {
978 MemoryProfileLibRecord (
979 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
980 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RUNTIME_POOL
,
981 EfiRuntimeServicesData
,
991 Reallocates a buffer of type EfiReservedMemoryType.
993 Allocates and zeros the number bytes specified by NewSize from memory of type
994 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
995 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
996 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
997 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
998 enough memory remaining to satisfy the request, then NULL is returned.
1000 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
1001 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
1003 @param OldSize The size, in bytes, of OldBuffer.
1004 @param NewSize The size, in bytes, of the buffer to reallocate.
1005 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
1006 parameter that may be NULL.
1008 @return A pointer to the allocated buffer or NULL if allocation fails.
1013 ReallocateReservedPool (
1016 IN VOID
*OldBuffer OPTIONAL
1021 Buffer
= InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
1022 if (Buffer
!= NULL
) {
1023 MemoryProfileLibRecord (
1024 (PHYSICAL_ADDRESS
) (UINTN
) RETURN_ADDRESS(0),
1025 MEMORY_PROFILE_ACTION_LIB_REALLOCATE_RESERVED_POOL
,
1026 EfiReservedMemoryType
,
1036 Frees a buffer that was previously allocated with one of the pool allocation functions in the
1037 Memory Allocation Library.
1039 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1040 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
1041 resources, then this function will perform no actions.
1043 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
1046 @param Buffer Pointer to the buffer to free.
1057 Status
= CoreFreePool (Buffer
);
1058 ASSERT_EFI_ERROR (Status
);