2 Support routines for memory allocation routines
3 based on PeiService for PEI phase drivers.
5 Copyright (c) 2006 - 2017, 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/PeiServicesLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/DebugLib.h>
24 #include <Library/HobLib.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
= PeiServicesAllocatePages (MemoryType
, Pages
, &Memory
);
54 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.
80 return InternalAllocatePages (EfiBootServicesData
, Pages
);
84 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
86 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
87 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
88 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
91 @param Pages The number of 4 KB pages to allocate.
93 @return A pointer to the allocated buffer or NULL if allocation fails.
98 AllocateRuntimePages (
102 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
106 Allocates one or more 4KB pages of type EfiReservedMemoryType.
108 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
109 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
110 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
113 @param Pages The number of 4 KB pages to allocate.
115 @return A pointer to the allocated buffer or NULL if allocation fails.
120 AllocateReservedPages (
124 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
128 Frees one or more 4KB pages that were previously allocated with one of the page allocation
129 functions in the Memory Allocation Library.
131 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
132 must have been allocated on a previous call to the page allocation services of the Memory
133 Allocation Library. If it is not possible to free allocated pages, then this function will
136 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
138 If Pages is zero, then ASSERT().
140 @param Buffer The pointer to the buffer of pages to free.
141 @param Pages The number of 4 KB pages to free.
154 Status
= PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
155 ASSERT_EFI_ERROR (Status
);
159 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
161 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
162 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
163 If there is not enough memory at the specified alignment remaining to satisfy the request, then
165 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
166 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
168 @param MemoryType The type of memory to allocate.
169 @param Pages The number of 4 KB pages to allocate.
170 @param Alignment The requested alignment of the allocation.
171 Must be a power of two.
172 If Alignment is zero, then byte alignment is used.
174 @return A pointer to the allocated buffer or NULL if allocation fails.
178 InternalAllocateAlignedPages (
179 IN EFI_MEMORY_TYPE MemoryType
,
184 EFI_PHYSICAL_ADDRESS Memory
;
185 EFI_PHYSICAL_ADDRESS AlignedMemory
;
186 EFI_PEI_HOB_POINTERS Hob
;
187 BOOLEAN SkipBeforeMemHob
;
188 BOOLEAN SkipAfterMemHob
;
189 EFI_PHYSICAL_ADDRESS HobBaseAddress
;
191 EFI_MEMORY_TYPE HobMemoryType
;
195 // Alignment must be a power of two or zero.
197 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
203 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
205 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
208 // We would rather waste some memory to save PEI code size.
209 // meaning in addition to the requested size for the aligned mem,
210 // we simply reserve an overhead memory equal to Alignmemt(page-aligned), no matter what.
211 // The overhead mem size could be reduced later with more involved malloc mechanisms
212 // (e.g., somthing that can detect the alignment boundary before allocating memory or
213 // can request that memory be allocated at a certain address that is aleady aligned).
215 TotalPages
= Pages
+ (Alignment
<= EFI_PAGE_SIZE
? 0 : EFI_SIZE_TO_PAGES(Alignment
));
216 Memory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) InternalAllocatePages (MemoryType
, TotalPages
);
218 DEBUG((DEBUG_INFO
, "Out of memory resource! \n"));
221 DEBUG ((DEBUG_INFO
, "Allocated Memory unaligned: Address = 0x%LX, Pages = 0x%X, Type = %d \n", Memory
, TotalPages
, (UINTN
) MemoryType
));
224 // Alignment calculation
226 AlignedMemory
= Memory
;
227 if (Alignment
> EFI_PAGE_SIZE
) {
228 AlignedMemory
= ALIGN_VALUE (Memory
, Alignment
);
230 DEBUG ((DEBUG_INFO
, "After aligning to 0x%X bytes: Address = 0x%LX, Pages = 0x%X \n", Alignment
, AlignedMemory
, Pages
));
233 // In general three HOBs cover the total allocated space.
234 // The aligned portion is covered by the aligned mem HOB and
235 // the unaligned(to be freed) portions before and after the aligned portion are covered by newly created HOBs.
237 // Before mem HOB covers the region between "Memory" and "AlignedMemory"
238 // Aligned mem HOB covers the region between "AlignedMemory" and "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)"
239 // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)"
241 // The before or after mem HOBs need to be skipped under special cases where the aligned portion
242 // touches either the top or bottom of the original allocated space.
244 SkipBeforeMemHob
= FALSE
;
245 SkipAfterMemHob
= FALSE
;
246 if (Memory
== AlignedMemory
) {
247 SkipBeforeMemHob
= TRUE
;
249 if ((Memory
+ EFI_PAGES_TO_SIZE(TotalPages
)) == (AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
))) {
251 // This condition is never met in the current implementation.
252 // There is always some after-mem since the overhead mem(used in TotalPages)
253 // is no less than Alignment.
255 SkipAfterMemHob
= TRUE
;
259 // Search for the mem HOB referring to the original(unaligned) allocation
260 // and update the size and type if needed.
262 Hob
.Raw
= GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
);
263 while (Hob
.Raw
!= NULL
) {
264 if (Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
== Memory
) {
267 Hob
.Raw
= GET_NEXT_HOB (Hob
);
268 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
270 ASSERT (Hob
.Raw
!= NULL
);
271 if (SkipBeforeMemHob
) {
273 // Use this HOB as aligned mem HOB as there is no portion before it.
275 HobLength
= EFI_PAGES_TO_SIZE(Pages
);
276 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryLength
= HobLength
;
279 // Use this HOB as before mem HOB and create a new HOB for the aligned portion
281 HobLength
= (AlignedMemory
- Memory
);
282 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryLength
= HobLength
;
283 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
= EfiConventionalMemory
;
286 HobBaseAddress
= Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
;
287 HobMemoryType
= Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
;
290 // Build the aligned mem HOB if needed
292 if (!SkipBeforeMemHob
) {
293 DEBUG((DEBUG_INFO
, "Updated before-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
294 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
296 HobBaseAddress
= AlignedMemory
;
297 HobLength
= EFI_PAGES_TO_SIZE(Pages
);
298 HobMemoryType
= MemoryType
;
300 BuildMemoryAllocationHob (
306 DEBUG((DEBUG_INFO
, "Created aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
307 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
309 if (HobBaseAddress
!= 0) {
310 DEBUG((DEBUG_INFO
, "Updated aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
311 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
317 // Build the after mem HOB if needed
319 if (!SkipAfterMemHob
) {
320 HobBaseAddress
= AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
);
321 HobLength
= (Memory
+ EFI_PAGES_TO_SIZE(TotalPages
)) - (AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
));
322 HobMemoryType
= EfiConventionalMemory
;
324 BuildMemoryAllocationHob (
330 DEBUG((DEBUG_INFO
, "Created after-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
331 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
334 return (VOID
*) (UINTN
) AlignedMemory
;
338 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
340 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
341 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
342 returned. If there is not enough memory at the specified alignment remaining to satisfy the
343 request, then NULL is returned.
345 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
346 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
348 @param Pages The number of 4 KB pages to allocate.
349 @param Alignment The requested alignment of the allocation.
350 Must be a power of two.
351 If Alignment is zero, then byte alignment is used.
353 @return A pointer to the allocated buffer or NULL if allocation fails.
358 AllocateAlignedPages (
363 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
367 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
369 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
370 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
371 returned. If there is not enough memory at the specified alignment remaining to satisfy the
372 request, then NULL is returned.
374 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
375 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
377 @param Pages The number of 4 KB pages to allocate.
378 @param Alignment The requested alignment of the allocation.
379 Must be a power of two.
380 If Alignment is zero, then byte alignment is used.
382 @return A pointer to the allocated buffer or NULL if allocation fails.
387 AllocateAlignedRuntimePages (
392 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
396 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
398 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
399 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
400 returned. If there is not enough memory at the specified alignment remaining to satisfy the
401 request, then NULL is returned.
403 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
404 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
406 @param Pages The number of 4 KB pages to allocate.
407 @param Alignment The requested alignment of the allocation.
408 Must be a power of two.
409 If Alignment is zero, then byte alignment is used.
411 @return A pointer to the allocated buffer or NULL if allocation fails.
416 AllocateAlignedReservedPages (
421 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
425 Frees one or more 4KB pages that were previously allocated with one of the aligned page
426 allocation functions in the Memory Allocation Library.
428 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
429 must have been allocated on a previous call to the aligned page allocation services of the Memory
430 Allocation Library. If it is not possible to free allocated pages, then this function will
433 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
434 Library, then ASSERT().
435 If Pages is zero, then ASSERT().
437 @param Buffer The pointer to the buffer of pages to free.
438 @param Pages The number of 4 KB pages to free.
451 Status
= PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
452 ASSERT_EFI_ERROR (Status
);
456 Allocates a buffer of a certain pool type.
458 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
459 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
460 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
462 @param MemoryType The type of memory to allocate.
463 @param AllocationSize The number of bytes to allocate.
465 @return A pointer to the allocated buffer or NULL if allocation fails.
469 InternalAllocatePool (
470 IN EFI_MEMORY_TYPE MemoryType
,
471 IN UINTN AllocationSize
475 // If we need lots of small runtime/reserved memory type from PEI in the future,
476 // we can consider providing a more complex algorithm that allocates runtime pages and
477 // provide pool allocations from those pages.
479 return InternalAllocatePages (MemoryType
, EFI_SIZE_TO_PAGES (AllocationSize
));
483 Allocates a buffer of type EfiBootServicesData.
485 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
486 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
487 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
489 @param AllocationSize The number of bytes to allocate.
491 @return A pointer to the allocated buffer or NULL if allocation fails.
497 IN UINTN AllocationSize
503 Status
= PeiServicesAllocatePool (AllocationSize
, &Buffer
);
504 if (EFI_ERROR (Status
)) {
511 Allocates a buffer of type EfiRuntimeServicesData.
513 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
514 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
515 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
517 @param AllocationSize The number of bytes to allocate.
519 @return A pointer to the allocated buffer or NULL if allocation fails.
524 AllocateRuntimePool (
525 IN UINTN AllocationSize
528 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
532 Allocates a buffer of type EfiReservedMemoryType.
534 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
535 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
536 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
538 @param AllocationSize The number of bytes to allocate.
540 @return A pointer to the allocated buffer or NULL if allocation fails.
545 AllocateReservedPool (
546 IN UINTN AllocationSize
549 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
553 Allocates and zeros a buffer of a certain pool type.
555 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
556 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
557 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
558 then NULL is returned.
560 @param PoolType The type of memory to allocate.
561 @param AllocationSize The number of bytes to allocate and zero.
563 @return A pointer to the allocated buffer or NULL if allocation fails.
567 InternalAllocateZeroPool (
568 IN EFI_MEMORY_TYPE PoolType
,
569 IN UINTN AllocationSize
574 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
575 if (Memory
!= NULL
) {
576 Memory
= ZeroMem (Memory
, AllocationSize
);
582 Allocates and zeros a buffer of type EfiBootServicesData.
584 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
585 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
586 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
587 request, then NULL is returned.
589 @param AllocationSize The number of bytes to allocate and zero.
591 @return A pointer to the allocated buffer or NULL if allocation fails.
597 IN UINTN AllocationSize
602 Memory
= AllocatePool (AllocationSize
);
603 if (Memory
!= NULL
) {
604 Memory
= ZeroMem (Memory
, AllocationSize
);
610 Allocates and zeros a buffer of type EfiRuntimeServicesData.
612 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
613 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
614 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
615 request, then NULL is returned.
617 @param AllocationSize The number of bytes to allocate and zero.
619 @return A pointer to the allocated buffer or NULL if allocation fails.
624 AllocateRuntimeZeroPool (
625 IN UINTN AllocationSize
628 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
632 Allocates and zeros a buffer of type EfiReservedMemoryType.
634 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, 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 AllocateReservedZeroPool (
647 IN UINTN AllocationSize
650 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
654 Copies a buffer to an allocated buffer of a certain pool type.
656 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
657 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
658 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
659 is not enough memory remaining to satisfy the request, then NULL is returned.
660 If Buffer is NULL, then ASSERT().
661 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
663 @param PoolType The type of pool to allocate.
664 @param AllocationSize The number of bytes to allocate and zero.
665 @param Buffer The buffer to copy to the allocated buffer.
667 @return A pointer to the allocated buffer or NULL if allocation fails.
671 InternalAllocateCopyPool (
672 IN EFI_MEMORY_TYPE PoolType
,
673 IN UINTN AllocationSize
,
674 IN CONST VOID
*Buffer
679 ASSERT (Buffer
!= NULL
);
680 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
682 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
683 if (Memory
!= NULL
) {
684 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
690 Copies a buffer to an allocated buffer of type EfiBootServicesData.
692 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
693 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
694 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
695 is not enough memory remaining to satisfy the request, then NULL is returned.
697 If Buffer is NULL, then ASSERT().
698 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
700 @param AllocationSize The number of bytes to allocate and zero.
701 @param Buffer The buffer to copy to the allocated buffer.
703 @return A pointer to the allocated buffer or NULL if allocation fails.
709 IN UINTN AllocationSize
,
710 IN CONST VOID
*Buffer
715 ASSERT (Buffer
!= NULL
);
716 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
718 Memory
= AllocatePool (AllocationSize
);
719 if (Memory
!= NULL
) {
720 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
726 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
728 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
729 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
730 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
731 is not enough memory remaining to satisfy the request, then NULL is returned.
733 If Buffer is NULL, then ASSERT().
734 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
736 @param AllocationSize The number of bytes to allocate and zero.
737 @param Buffer The buffer to copy to the allocated buffer.
739 @return A pointer to the allocated buffer or NULL if allocation fails.
744 AllocateRuntimeCopyPool (
745 IN UINTN AllocationSize
,
746 IN CONST VOID
*Buffer
749 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
753 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
755 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
756 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
757 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
758 is not enough memory remaining to satisfy the request, then NULL is returned.
760 If Buffer is NULL, then ASSERT().
761 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
763 @param AllocationSize The number of bytes to allocate and zero.
764 @param Buffer The buffer to copy to the allocated buffer.
766 @return A pointer to the allocated buffer or NULL if allocation fails.
771 AllocateReservedCopyPool (
772 IN UINTN AllocationSize
,
773 IN CONST VOID
*Buffer
776 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
780 Reallocates a buffer of a specified memory type.
782 Allocates and zeros the number bytes specified by NewSize from memory of the type
783 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
784 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
785 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
786 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
787 enough memory remaining to satisfy the request, then NULL is returned.
789 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
790 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
792 @param PoolType The type of pool to allocate.
793 @param OldSize The size, in bytes, of OldBuffer.
794 @param NewSize The size, in bytes, of the buffer to reallocate.
795 @param OldBuffer The buffer to copy to the allocated buffer. This is an
796 optional parameter that may be NULL.
798 @return A pointer to the allocated buffer or NULL if allocation fails.
802 InternalReallocatePool (
803 IN EFI_MEMORY_TYPE PoolType
,
806 IN VOID
*OldBuffer OPTIONAL
811 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
812 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
813 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
814 FreePool (OldBuffer
);
820 Reallocates a buffer of type EfiBootServicesData.
822 Allocates and zeros the number bytes specified by NewSize from memory of type
823 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
824 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
825 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
826 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
827 enough memory remaining to satisfy the request, then NULL is returned.
829 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
830 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
832 @param OldSize The size, in bytes, of OldBuffer.
833 @param NewSize The size, in bytes, of the buffer to reallocate.
834 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
835 parameter that may be NULL.
837 @return A pointer to the allocated buffer or NULL if allocation fails.
845 IN VOID
*OldBuffer OPTIONAL
848 return InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
852 Reallocates a buffer of type EfiRuntimeServicesData.
854 Allocates and zeros the number bytes specified by NewSize from memory of type
855 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
856 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
857 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
858 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
859 enough memory remaining to satisfy the request, then NULL is returned.
861 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
862 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
864 @param OldSize The size, in bytes, of OldBuffer.
865 @param NewSize The size, in bytes, of the buffer to reallocate.
866 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
867 parameter that may be NULL.
869 @return A pointer to the allocated buffer or NULL if allocation fails.
874 ReallocateRuntimePool (
877 IN VOID
*OldBuffer OPTIONAL
880 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
884 Reallocates a buffer of type EfiReservedMemoryType.
886 Allocates and zeros the number bytes specified by NewSize from memory of type
887 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
888 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
889 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
890 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
891 enough memory remaining to satisfy the request, then NULL is returned.
893 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
894 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
896 @param OldSize The size, in bytes, of OldBuffer.
897 @param NewSize The size, in bytes, of the buffer to reallocate.
898 @param OldBuffer The buffer to copy to the allocated buffer. This is an
899 optional parameter that may be NULL.
901 @return A pointer to the allocated buffer or NULL if allocation fails.
906 ReallocateReservedPool (
909 IN VOID
*OldBuffer OPTIONAL
912 return InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
916 Frees a buffer that was previously allocated with one of the pool allocation functions in the
917 Memory Allocation Library.
919 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
920 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
921 resources, then this function will perform no actions.
923 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
926 @param Buffer The pointer to the buffer to free.
936 // PEI phase does not support to free pool, so leave it as NOP.