2 Support routines for memory allocation routines
3 based on PeiService for PEI phase drivers.
5 Copyright (c) 2006 - 2014, 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
;
48 EFI_MEMORY_TYPE RequestType
;
49 EFI_PEI_HOB_POINTERS Hob
;
55 RequestType
= MemoryType
;
56 if (MemoryType
== EfiReservedMemoryType
) {
58 // PEI AllocatePages() doesn't support EfiReservedMemoryType.
59 // Change RequestType to EfiBootServicesData for memory allocation.
61 RequestType
= EfiBootServicesData
;
64 Status
= PeiServicesAllocatePages (RequestType
, Pages
, &Memory
);
65 if (EFI_ERROR (Status
)) {
69 if (MemoryType
== EfiReservedMemoryType
) {
71 // Memory type needs to be updated to EfiReservedMemoryType. Per PI spec Volume 1,
72 // PEI AllocatePages() will automate the creation of the Memory Allocation HOB types.
73 // Search Memory Allocation HOB and find the matched memory region,
74 // then change its memory type to EfiReservedMemoryType.
76 Hob
.Raw
= GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
);
77 while (Hob
.Raw
!= NULL
&& Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
!= Memory
) {
78 Hob
.Raw
= GET_NEXT_HOB (Hob
);
79 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
81 ASSERT (Hob
.Raw
!= NULL
);
82 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
= EfiReservedMemoryType
;
85 return (VOID
*) (UINTN
) Memory
;
89 Allocates one or more 4KB pages of type EfiBootServicesData.
91 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
92 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
93 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
96 @param Pages The number of 4 KB pages to allocate.
98 @return A pointer to the allocated buffer or NULL if allocation fails.
107 return InternalAllocatePages (EfiBootServicesData
, Pages
);
111 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
113 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
114 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
115 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
118 @param Pages The number of 4 KB pages to allocate.
120 @return A pointer to the allocated buffer or NULL if allocation fails.
125 AllocateRuntimePages (
129 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
133 Allocates one or more 4KB pages of type EfiReservedMemoryType.
135 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
136 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
137 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
140 @param Pages The number of 4 KB pages to allocate.
142 @return A pointer to the allocated buffer or NULL if allocation fails.
147 AllocateReservedPages (
151 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
155 Frees one or more 4KB pages that were previously allocated with one of the page allocation
156 functions in the Memory Allocation Library.
158 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
159 must have been allocated on a previous call to the page allocation services of the Memory
160 Allocation Library. If it is not possible to free allocated pages, then this function will
163 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
165 If Pages is zero, then ASSERT().
167 @param Buffer The pointer to the buffer of pages to free.
168 @param Pages The number of 4 KB pages to free.
180 // PEI phase does not support to free pages, so leave it as NOP.
185 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
187 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
188 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
189 If there is not enough memory at the specified alignment remaining to satisfy the request, then
191 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
192 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
194 @param MemoryType The type of memory to allocate.
195 @param Pages The number of 4 KB pages to allocate.
196 @param Alignment The requested alignment of the allocation.
197 Must be a power of two.
198 If Alignment is zero, then byte alignment is used.
200 @return A pointer to the allocated buffer or NULL if allocation fails.
204 InternalAllocateAlignedPages (
205 IN EFI_MEMORY_TYPE MemoryType
,
210 EFI_PHYSICAL_ADDRESS Memory
;
211 EFI_PHYSICAL_ADDRESS AlignedMemory
;
212 EFI_PEI_HOB_POINTERS Hob
;
213 BOOLEAN SkipBeforeMemHob
= FALSE
;
214 BOOLEAN SkipAfterMemHob
= FALSE
;
215 EFI_PHYSICAL_ADDRESS HobBaseAddress
;
217 EFI_MEMORY_TYPE HobMemoryType
;
221 // Alignment must be a power of two or zero.
223 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
229 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
231 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
234 // We would rather waste some memory to save PEI code size.
235 // meaning in addition to the requested size for the aligned mem,
236 // we simply reserve an overhead memory equal to Alignmemt(page-aligned), no matter what.
237 // The overhead mem size could be reduced later with more involved malloc mechanisms
238 // (e.g., somthing that can detect the alignment boundary before allocating memory or
239 // can request that memory be allocated at a certain address that is aleady aligned).
241 TotalPages
= Pages
+ (Alignment
<= EFI_PAGE_SIZE
? 0 : EFI_SIZE_TO_PAGES(Alignment
));
242 Memory
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) InternalAllocatePages (MemoryType
, TotalPages
);
244 DEBUG((DEBUG_INFO
, "Out of memory resource! \n"));
247 DEBUG ((DEBUG_INFO
, "Allocated Memory unaligned: Address = 0x%LX, Pages = 0x%X, Type = %d \n", Memory
, TotalPages
, (UINTN
) MemoryType
));
250 // Alignment calculation
252 AlignedMemory
= Memory
;
253 if (Alignment
> EFI_PAGE_SIZE
) {
254 AlignedMemory
= ALIGN_VALUE (Memory
, Alignment
);
256 DEBUG ((DEBUG_INFO
, "After aligning to 0x%X bytes: Address = 0x%LX, Pages = 0x%X \n", Alignment
, AlignedMemory
, Pages
));
259 // In general three HOBs cover the total allocated space.
260 // The aligned portion is covered by the aligned mem HOB and
261 // the unaligned(to be freed) portions before and after the aligned portion are covered by newly created HOBs.
263 // Before mem HOB covers the region between "Memory" and "AlignedMemory"
264 // Aligned mem HOB covers the region between "AlignedMemory" and "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)"
265 // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)"
267 // The before or after mem HOBs need to be skipped under special cases where the aligned portion
268 // touches either the top or bottom of the original allocated space.
270 if (Memory
== AlignedMemory
) {
271 SkipBeforeMemHob
= TRUE
;
273 if ((Memory
+ EFI_PAGES_TO_SIZE(TotalPages
)) == (AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
))) {
275 // This condition is never met in the current implementation.
276 // There is always some after-mem since the overhead mem(used in TotalPages)
277 // is no less than Alignment.
279 SkipAfterMemHob
= TRUE
;
283 // Search for the mem HOB referring to the original(unaligned) allocation
284 // and update the size and type if needed.
286 Hob
.Raw
= GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
);
287 while (Hob
.Raw
!= NULL
) {
288 if (Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
== Memory
) {
291 Hob
.Raw
= GET_NEXT_HOB (Hob
);
292 Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
);
294 ASSERT (Hob
.Raw
!= NULL
);
295 if (SkipBeforeMemHob
) {
297 // Use this HOB as aligned mem HOB as there is no portion before it.
299 HobLength
= EFI_PAGES_TO_SIZE(Pages
);
300 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryLength
= HobLength
;
303 // Use this HOB as before mem HOB and create a new HOB for the aligned portion
305 HobLength
= (AlignedMemory
- Memory
);
306 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryLength
= HobLength
;
307 Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
= EfiConventionalMemory
;
310 HobBaseAddress
= Hob
.MemoryAllocation
->AllocDescriptor
.MemoryBaseAddress
;
311 HobMemoryType
= Hob
.MemoryAllocation
->AllocDescriptor
.MemoryType
;
314 // Build the aligned mem HOB if needed
316 if (!SkipBeforeMemHob
) {
317 DEBUG((DEBUG_INFO
, "Updated before-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
318 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
320 HobBaseAddress
= AlignedMemory
;
321 HobLength
= EFI_PAGES_TO_SIZE(Pages
);
322 HobMemoryType
= MemoryType
;
324 BuildMemoryAllocationHob (
330 DEBUG((DEBUG_INFO
, "Created aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
331 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
333 if (HobBaseAddress
!= 0) {
334 DEBUG((DEBUG_INFO
, "Updated aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
335 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
341 // Build the after mem HOB if needed
343 if (!SkipAfterMemHob
) {
344 HobBaseAddress
= AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
);
345 HobLength
= (Memory
+ EFI_PAGES_TO_SIZE(TotalPages
)) - (AlignedMemory
+ EFI_PAGES_TO_SIZE(Pages
));
346 HobMemoryType
= EfiConventionalMemory
;
348 BuildMemoryAllocationHob (
354 DEBUG((DEBUG_INFO
, "Created after-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",
355 HobBaseAddress
, HobLength
, (UINTN
) HobMemoryType
));
358 return (VOID
*) (UINTN
) AlignedMemory
;
362 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
364 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
365 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
366 returned. If there is not enough memory at the specified alignment remaining to satisfy the
367 request, then NULL is returned.
369 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
370 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
372 @param Pages The number of 4 KB pages to allocate.
373 @param Alignment The requested alignment of the allocation.
374 Must be a power of two.
375 If Alignment is zero, then byte alignment is used.
377 @return A pointer to the allocated buffer or NULL if allocation fails.
382 AllocateAlignedPages (
387 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
391 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
393 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
394 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
395 returned. If there is not enough memory at the specified alignment remaining to satisfy the
396 request, then NULL is returned.
398 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
399 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
401 @param Pages The number of 4 KB pages to allocate.
402 @param Alignment The requested alignment of the allocation.
403 Must be a power of two.
404 If Alignment is zero, then byte alignment is used.
406 @return A pointer to the allocated buffer or NULL if allocation fails.
411 AllocateAlignedRuntimePages (
416 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
420 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
422 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
423 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
424 returned. If there is not enough memory at the specified alignment remaining to satisfy the
425 request, then NULL is returned.
427 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
428 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
430 @param Pages The number of 4 KB pages to allocate.
431 @param Alignment The requested alignment of the allocation.
432 Must be a power of two.
433 If Alignment is zero, then byte alignment is used.
435 @return A pointer to the allocated buffer or NULL if allocation fails.
440 AllocateAlignedReservedPages (
445 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
449 Frees one or more 4KB pages that were previously allocated with one of the aligned page
450 allocation functions in the Memory Allocation Library.
452 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
453 must have been allocated on a previous call to the aligned page allocation services of the Memory
454 Allocation Library. If it is not possible to free allocated pages, then this function will
457 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
458 Library, then ASSERT().
459 If Pages is zero, then ASSERT().
461 @param Buffer The pointer to the buffer of pages to free.
462 @param Pages The number of 4 KB pages to free.
474 // PEI phase does not support to free pages, so leave it as NOP.
479 Allocates a buffer of a certain pool type.
481 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
482 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
483 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
485 @param MemoryType The type of memory to allocate.
486 @param AllocationSize The number of bytes to allocate.
488 @return A pointer to the allocated buffer or NULL if allocation fails.
492 InternalAllocatePool (
493 IN EFI_MEMORY_TYPE MemoryType
,
494 IN UINTN AllocationSize
498 // If we need lots of small runtime/reserved memory type from PEI in the future,
499 // we can consider providing a more complex algorithm that allocates runtime pages and
500 // provide pool allocations from those pages.
502 return InternalAllocatePages (MemoryType
, EFI_SIZE_TO_PAGES (AllocationSize
));
506 Allocates a buffer of type EfiBootServicesData.
508 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
509 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
510 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
512 @param AllocationSize The number of bytes to allocate.
514 @return A pointer to the allocated buffer or NULL if allocation fails.
520 IN UINTN AllocationSize
526 Status
= PeiServicesAllocatePool (AllocationSize
, &Buffer
);
527 if (EFI_ERROR (Status
)) {
534 Allocates a buffer of type EfiRuntimeServicesData.
536 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
537 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
538 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
540 @param AllocationSize The number of bytes to allocate.
542 @return A pointer to the allocated buffer or NULL if allocation fails.
547 AllocateRuntimePool (
548 IN UINTN AllocationSize
551 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
555 Allocates a buffer of type EfiReservedMemoryType.
557 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
558 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
559 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
561 @param AllocationSize The number of bytes to allocate.
563 @return A pointer to the allocated buffer or NULL if allocation fails.
568 AllocateReservedPool (
569 IN UINTN AllocationSize
572 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
576 Allocates and zeros a buffer of a certain pool type.
578 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
579 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
580 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
581 then NULL is returned.
583 @param PoolType The type of memory to allocate.
584 @param AllocationSize The number of bytes to allocate and zero.
586 @return A pointer to the allocated buffer or NULL if allocation fails.
590 InternalAllocateZeroPool (
591 IN EFI_MEMORY_TYPE PoolType
,
592 IN UINTN AllocationSize
597 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
598 if (Memory
!= NULL
) {
599 Memory
= ZeroMem (Memory
, AllocationSize
);
605 Allocates and zeros a buffer of type EfiBootServicesData.
607 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
608 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
609 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
610 request, then NULL is returned.
612 @param AllocationSize The number of bytes to allocate and zero.
614 @return A pointer to the allocated buffer or NULL if allocation fails.
620 IN UINTN AllocationSize
625 Memory
= AllocatePool (AllocationSize
);
626 if (Memory
!= NULL
) {
627 Memory
= ZeroMem (Memory
, AllocationSize
);
633 Allocates and zeros a buffer of type EfiRuntimeServicesData.
635 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
636 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
637 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
638 request, then NULL is returned.
640 @param AllocationSize The number of bytes to allocate and zero.
642 @return A pointer to the allocated buffer or NULL if allocation fails.
647 AllocateRuntimeZeroPool (
648 IN UINTN AllocationSize
651 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
655 Allocates and zeros a buffer of type EfiReservedMemoryType.
657 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
658 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
659 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
660 request, then NULL is returned.
662 @param AllocationSize The number of bytes to allocate and zero.
664 @return A pointer to the allocated buffer or NULL if allocation fails.
669 AllocateReservedZeroPool (
670 IN UINTN AllocationSize
673 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
677 Copies a buffer to an allocated buffer of a certain pool type.
679 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
680 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
681 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
682 is not enough memory remaining to satisfy the request, then NULL is returned.
683 If Buffer is NULL, then ASSERT().
684 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
686 @param PoolType The type of pool to allocate.
687 @param AllocationSize The number of bytes to allocate and zero.
688 @param Buffer The buffer to copy to the allocated buffer.
690 @return A pointer to the allocated buffer or NULL if allocation fails.
694 InternalAllocateCopyPool (
695 IN EFI_MEMORY_TYPE PoolType
,
696 IN UINTN AllocationSize
,
697 IN CONST VOID
*Buffer
702 ASSERT (Buffer
!= NULL
);
703 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
705 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
706 if (Memory
!= NULL
) {
707 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
713 Copies a buffer to an allocated buffer of type EfiBootServicesData.
715 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
716 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
717 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
718 is not enough memory remaining to satisfy the request, then NULL is returned.
720 If Buffer is NULL, then ASSERT().
721 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
723 @param AllocationSize The number of bytes to allocate and zero.
724 @param Buffer The buffer to copy to the allocated buffer.
726 @return A pointer to the allocated buffer or NULL if allocation fails.
732 IN UINTN AllocationSize
,
733 IN CONST VOID
*Buffer
738 ASSERT (Buffer
!= NULL
);
739 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
741 Memory
= AllocatePool (AllocationSize
);
742 if (Memory
!= NULL
) {
743 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
749 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
751 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
752 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
753 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
754 is not enough memory remaining to satisfy the request, then NULL is returned.
756 If Buffer is NULL, then ASSERT().
757 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
759 @param AllocationSize The number of bytes to allocate and zero.
760 @param Buffer The buffer to copy to the allocated buffer.
762 @return A pointer to the allocated buffer or NULL if allocation fails.
767 AllocateRuntimeCopyPool (
768 IN UINTN AllocationSize
,
769 IN CONST VOID
*Buffer
772 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
776 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
778 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
779 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
780 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
781 is not enough memory remaining to satisfy the request, then NULL is returned.
783 If Buffer is NULL, then ASSERT().
784 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
786 @param AllocationSize The number of bytes to allocate and zero.
787 @param Buffer The buffer to copy to the allocated buffer.
789 @return A pointer to the allocated buffer or NULL if allocation fails.
794 AllocateReservedCopyPool (
795 IN UINTN AllocationSize
,
796 IN CONST VOID
*Buffer
799 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
803 Reallocates a buffer of a specified memory type.
805 Allocates and zeros the number bytes specified by NewSize from memory of the type
806 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
807 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
808 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
809 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
810 enough memory remaining to satisfy the request, then NULL is returned.
812 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
813 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
815 @param PoolType The type of pool to allocate.
816 @param OldSize The size, in bytes, of OldBuffer.
817 @param NewSize The size, in bytes, of the buffer to reallocate.
818 @param OldBuffer The buffer to copy to the allocated buffer. This is an
819 optional parameter that may be NULL.
821 @return A pointer to the allocated buffer or NULL if allocation fails.
825 InternalReallocatePool (
826 IN EFI_MEMORY_TYPE PoolType
,
829 IN VOID
*OldBuffer OPTIONAL
834 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
835 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
836 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
837 FreePool (OldBuffer
);
843 Reallocates a buffer of type EfiBootServicesData.
845 Allocates and zeros the number bytes specified by NewSize from memory of type
846 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
847 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
848 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
849 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
850 enough memory remaining to satisfy the request, then NULL is returned.
852 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
853 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
855 @param OldSize The size, in bytes, of OldBuffer.
856 @param NewSize The size, in bytes, of the buffer to reallocate.
857 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
858 parameter that may be NULL.
860 @return A pointer to the allocated buffer or NULL if allocation fails.
868 IN VOID
*OldBuffer OPTIONAL
871 return InternalReallocatePool (EfiBootServicesData
, OldSize
, NewSize
, OldBuffer
);
875 Reallocates a buffer of type EfiRuntimeServicesData.
877 Allocates and zeros the number bytes specified by NewSize from memory of type
878 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
879 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
880 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
881 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
882 enough memory remaining to satisfy the request, then NULL is returned.
884 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
885 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
887 @param OldSize The size, in bytes, of OldBuffer.
888 @param NewSize The size, in bytes, of the buffer to reallocate.
889 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
890 parameter that may be NULL.
892 @return A pointer to the allocated buffer or NULL if allocation fails.
897 ReallocateRuntimePool (
900 IN VOID
*OldBuffer OPTIONAL
903 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
907 Reallocates a buffer of type EfiReservedMemoryType.
909 Allocates and zeros the number bytes specified by NewSize from memory of type
910 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
911 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
912 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
913 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
914 enough memory remaining to satisfy the request, then NULL is returned.
916 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
917 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
919 @param OldSize The size, in bytes, of OldBuffer.
920 @param NewSize The size, in bytes, of the buffer to reallocate.
921 @param OldBuffer The buffer to copy to the allocated buffer. This is an
922 optional parameter that may be NULL.
924 @return A pointer to the allocated buffer or NULL if allocation fails.
929 ReallocateReservedPool (
932 IN VOID
*OldBuffer OPTIONAL
935 return InternalReallocatePool (EfiReservedMemoryType
, OldSize
, NewSize
, OldBuffer
);
939 Frees a buffer that was previously allocated with one of the pool allocation functions in the
940 Memory Allocation Library.
942 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
943 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
944 resources, then this function will perform no actions.
946 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
949 @param Buffer The pointer to the buffer to free.
959 // PEI phase does not support to free pool, so leave it as NOP.