2 Support routines for memory allocation routines based on SMM Core internal functions.
4 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Protocol/SmmAccess2.h>
18 #include <Library/MemoryAllocationLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/DebugLib.h>
22 #include "PiSmmCoreMemoryAllocationServices.h"
24 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
= NULL
;
25 UINTN mSmramRangeCount
= 0;
28 This function gets and caches SMRAM ranges that are present in the system.
30 It will ASSERT() if SMM Access2 Protocol doesn't exist.
31 It will ASSERT() if SMRAM ranges can't be got.
32 It will ASSERT() if Resource can't be allocated for cache SMRAM range.
42 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
46 // Locate SMM Access2 Protocol
48 Status
= gBS
->LocateProtocol (
49 &gEfiSmmAccess2ProtocolGuid
,
53 ASSERT_EFI_ERROR (Status
);
56 // Get SMRAM range information
59 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
60 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
62 mSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
63 ASSERT (mSmramRanges
!= NULL
);
65 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
66 ASSERT_EFI_ERROR (Status
);
68 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
72 Check whether the start address of buffer is within any of the SMRAM ranges.
74 @param[in] Buffer The pointer to the buffer to be checked.
76 @retval TURE The buffer is in SMRAM ranges.
77 @retval FALSE The buffer is out of SMRAM ranges.
87 if (mSmramRanges
== NULL
) {
89 // SMRAM ranges is not got. Try to get them all.
94 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
95 if (((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
>= mSmramRanges
[Index
].CpuStart
) &&
96 ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
< (mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
))) {
105 Allocates one or more 4KB pages of a certain memory type.
107 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
108 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
109 If there is not enough memory remaining to satisfy the request, then NULL is returned.
111 @param MemoryType The type of memory to allocate.
112 @param Pages The number of 4 KB pages to allocate.
114 @return A pointer to the allocated buffer or NULL if allocation fails.
118 InternalAllocatePages (
119 IN EFI_MEMORY_TYPE MemoryType
,
124 EFI_PHYSICAL_ADDRESS Memory
;
130 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
131 if (EFI_ERROR (Status
)) {
134 return (VOID
*) (UINTN
) Memory
;
138 Allocates one or more 4KB pages of type EfiBootServicesData.
140 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
141 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
142 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
145 @param Pages The number of 4 KB pages to allocate.
147 @return A pointer to the allocated buffer or NULL if allocation fails.
156 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
160 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
162 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
163 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
164 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
167 @param Pages The number of 4 KB pages to allocate.
169 @return A pointer to the allocated buffer or NULL if allocation fails.
174 AllocateRuntimePages (
178 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
182 Allocates one or more 4KB pages of type EfiReservedMemoryType.
184 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
185 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
186 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
189 @param Pages The number of 4 KB pages to allocate.
191 @return A pointer to the allocated buffer or NULL if allocation fails.
196 AllocateReservedPages (
204 Frees one or more 4KB pages that were previously allocated with one of the page allocation
205 functions in the Memory Allocation Library.
207 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
208 must have been allocated on a previous call to the page allocation services of the Memory
209 Allocation Library. If it is not possible to free allocated pages, then this function will
212 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
214 If Pages is zero, then ASSERT().
216 @param Buffer Pointer to the buffer of pages to free.
217 @param Pages The number of 4 KB pages to free.
230 if (BufferInSmram (Buffer
)) {
232 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.
233 // So, SmmFreePages() service is used to free it.
235 Status
= SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
238 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
239 // So, gBS->FreePages() service is used to free it.
241 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
243 ASSERT_EFI_ERROR (Status
);
247 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
249 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
250 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
251 If there is not enough memory at the specified alignment remaining to satisfy the request, then
253 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
255 @param MemoryType The type of memory to allocate.
256 @param Pages The number of 4 KB pages to allocate.
257 @param Alignment The requested alignment of the allocation. Must be a power of two.
258 If Alignment is zero, then byte alignment is used.
260 @return A pointer to the allocated buffer or NULL if allocation fails.
264 InternalAllocateAlignedPages (
265 IN EFI_MEMORY_TYPE MemoryType
,
271 EFI_PHYSICAL_ADDRESS Memory
;
274 UINTN UnalignedPages
;
278 // Alignment must be a power of two or zero.
280 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
285 if (Alignment
> EFI_PAGE_SIZE
) {
287 // Caculate the total number of pages since alignment is larger than page size.
289 AlignmentMask
= Alignment
- 1;
290 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
292 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
294 ASSERT (RealPages
> Pages
);
296 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
297 if (EFI_ERROR (Status
)) {
300 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
301 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
302 if (UnalignedPages
> 0) {
304 // Free first unaligned page(s).
306 Status
= SmmFreePages (Memory
, UnalignedPages
);
307 ASSERT_EFI_ERROR (Status
);
309 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
310 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
311 if (UnalignedPages
> 0) {
313 // Free last unaligned page(s).
315 Status
= SmmFreePages (Memory
, UnalignedPages
);
316 ASSERT_EFI_ERROR (Status
);
320 // Do not over-allocate pages in this case.
322 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
323 if (EFI_ERROR (Status
)) {
326 AlignedMemory
= (UINTN
) Memory
;
328 return (VOID
*) AlignedMemory
;
332 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
334 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
335 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
336 returned. If there is not enough memory at the specified alignment remaining to satisfy the
337 request, then NULL is returned.
339 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
341 @param Pages The number of 4 KB pages to allocate.
342 @param Alignment The requested alignment of the allocation. Must be a power of two.
343 If Alignment is zero, then byte alignment is used.
345 @return A pointer to the allocated buffer or NULL if allocation fails.
350 AllocateAlignedPages (
355 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
359 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
361 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
362 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
363 returned. If there is not enough memory at the specified alignment remaining to satisfy the
364 request, then NULL is returned.
366 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
368 @param Pages The number of 4 KB pages to allocate.
369 @param Alignment The requested alignment of the allocation. Must be a power of two.
370 If Alignment is zero, then byte alignment is used.
372 @return A pointer to the allocated buffer or NULL if allocation fails.
377 AllocateAlignedRuntimePages (
382 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
386 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
388 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
389 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
390 returned. If there is not enough memory at the specified alignment remaining to satisfy the
391 request, then NULL is returned.
393 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
395 @param Pages The number of 4 KB pages to allocate.
396 @param Alignment The requested alignment of the allocation. Must be a power of two.
397 If Alignment is zero, then byte alignment is used.
399 @return A pointer to the allocated buffer or NULL if allocation fails.
404 AllocateAlignedReservedPages (
413 Frees one or more 4KB pages that were previously allocated with one of the aligned page
414 allocation functions in the Memory Allocation Library.
416 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
417 must have been allocated on a previous call to the aligned page allocation services of the Memory
418 Allocation Library. If it is not possible to free allocated pages, then this function will
421 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
422 Library, then ASSERT().
423 If Pages is zero, then ASSERT().
425 @param Buffer Pointer to the buffer of pages to free.
426 @param Pages The number of 4 KB pages to free.
439 if (BufferInSmram (Buffer
)) {
441 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.
442 // So, SmmFreePages() service is used to free it.
444 Status
= SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
447 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
448 // So, gBS->FreePages() service is used to free it.
450 Status
= gBS
->FreePages ((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
477 Status
= SmmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
478 if (EFI_ERROR (Status
)) {
485 Allocates a buffer of type EfiBootServicesData.
487 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
488 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
489 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
491 @param AllocationSize The number of bytes to allocate.
493 @return A pointer to the allocated buffer or NULL if allocation fails.
499 IN UINTN AllocationSize
502 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
506 Allocates a buffer of type EfiRuntimeServicesData.
508 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
509 a 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.
519 AllocateRuntimePool (
520 IN UINTN AllocationSize
523 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
527 Allocates a buffer of type EfiReservedMemoryType.
529 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
530 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
531 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
533 @param AllocationSize The number of bytes to allocate.
535 @return A pointer to the allocated buffer or NULL if allocation fails.
540 AllocateReservedPool (
541 IN UINTN AllocationSize
548 Allocates and zeros a buffer of a certain pool type.
550 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
551 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
552 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
553 then NULL is returned.
555 @param PoolType The type of memory to allocate.
556 @param AllocationSize The number of bytes to allocate and zero.
558 @return A pointer to the allocated buffer or NULL if allocation fails.
562 InternalAllocateZeroPool (
563 IN EFI_MEMORY_TYPE PoolType
,
564 IN UINTN AllocationSize
569 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
570 if (Memory
!= NULL
) {
571 Memory
= ZeroMem (Memory
, AllocationSize
);
577 Allocates and zeros a buffer of type EfiBootServicesData.
579 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
580 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
581 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
582 request, then NULL is returned.
584 @param AllocationSize The number of bytes to allocate and zero.
586 @return A pointer to the allocated buffer or NULL if allocation fails.
592 IN UINTN AllocationSize
595 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
599 Allocates and zeros a buffer of type EfiRuntimeServicesData.
601 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
602 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
603 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
604 request, then NULL is returned.
606 @param AllocationSize The number of bytes to allocate and zero.
608 @return A pointer to the allocated buffer or NULL if allocation fails.
613 AllocateRuntimeZeroPool (
614 IN UINTN AllocationSize
617 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
621 Allocates and zeros a buffer of type EfiReservedMemoryType.
623 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
624 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
625 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
626 request, then NULL is returned.
628 @param AllocationSize The number of bytes to allocate and zero.
630 @return A pointer to the allocated buffer or NULL if allocation fails.
635 AllocateReservedZeroPool (
636 IN UINTN AllocationSize
643 Copies a buffer to an allocated buffer of a certain pool type.
645 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
646 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
647 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
648 is not enough memory remaining to satisfy the request, then NULL is returned.
649 If Buffer is NULL, then ASSERT().
650 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
652 @param PoolType The type of pool to allocate.
653 @param AllocationSize The number of bytes to allocate and zero.
654 @param Buffer The buffer to copy to the allocated buffer.
656 @return A pointer to the allocated buffer or NULL if allocation fails.
660 InternalAllocateCopyPool (
661 IN EFI_MEMORY_TYPE PoolType
,
662 IN UINTN AllocationSize
,
663 IN CONST VOID
*Buffer
668 ASSERT (Buffer
!= NULL
);
669 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
671 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
672 if (Memory
!= NULL
) {
673 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
679 Copies a buffer to an allocated buffer of type EfiBootServicesData.
681 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
682 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
683 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
684 is not enough memory remaining to satisfy the request, then NULL is returned.
686 If Buffer is NULL, then ASSERT().
687 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
689 @param AllocationSize The number of bytes to allocate and zero.
690 @param Buffer The buffer to copy to the allocated buffer.
692 @return A pointer to the allocated buffer or NULL if allocation fails.
698 IN UINTN AllocationSize
,
699 IN CONST VOID
*Buffer
702 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
706 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
708 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
709 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
710 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
711 is not enough memory remaining to satisfy the request, then NULL is returned.
713 If Buffer is NULL, then ASSERT().
714 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
716 @param AllocationSize The number of bytes to allocate and zero.
717 @param Buffer The buffer to copy to the allocated buffer.
719 @return A pointer to the allocated buffer or NULL if allocation fails.
724 AllocateRuntimeCopyPool (
725 IN UINTN AllocationSize
,
726 IN CONST VOID
*Buffer
729 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
733 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
735 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
736 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
737 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
738 is not enough memory remaining to satisfy the request, then NULL is returned.
740 If Buffer is NULL, then ASSERT().
741 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
743 @param AllocationSize The number of bytes to allocate and zero.
744 @param Buffer The buffer to copy to the allocated buffer.
746 @return A pointer to the allocated buffer or NULL if allocation fails.
751 AllocateReservedCopyPool (
752 IN UINTN AllocationSize
,
753 IN CONST VOID
*Buffer
760 Reallocates a buffer of a specified memory type.
762 Allocates and zeros the number bytes specified by NewSize from memory of the type
763 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
764 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
765 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
766 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
767 enough memory remaining to satisfy the request, then NULL is returned.
769 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
770 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
772 @param PoolType The type of pool to allocate.
773 @param OldSize The size, in bytes, of OldBuffer.
774 @param NewSize The size, in bytes, of the buffer to reallocate.
775 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
776 parameter that may be NULL.
778 @return A pointer to the allocated buffer or NULL if allocation fails.
782 InternalReallocatePool (
783 IN EFI_MEMORY_TYPE PoolType
,
786 IN VOID
*OldBuffer OPTIONAL
791 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
792 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
793 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
794 FreePool (OldBuffer
);
800 Reallocates a buffer of type EfiBootServicesData.
802 Allocates and zeros the number bytes specified by NewSize from memory of type
803 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
804 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
805 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
806 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
807 enough memory remaining to satisfy the request, then NULL is returned.
809 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
810 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
812 @param OldSize The size, in bytes, of OldBuffer.
813 @param NewSize The size, in bytes, of the buffer to reallocate.
814 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
815 parameter that may be NULL.
817 @return A pointer to the allocated buffer or NULL if allocation fails.
825 IN VOID
*OldBuffer OPTIONAL
828 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
832 Reallocates a buffer of type EfiRuntimeServicesData.
834 Allocates and zeros the number bytes specified by NewSize from memory of type
835 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
836 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
837 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
838 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
839 enough memory remaining to satisfy the request, then NULL is returned.
841 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
842 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
844 @param OldSize The size, in bytes, of OldBuffer.
845 @param NewSize The size, in bytes, of the buffer to reallocate.
846 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
847 parameter that may be NULL.
849 @return A pointer to the allocated buffer or NULL if allocation fails.
854 ReallocateRuntimePool (
857 IN VOID
*OldBuffer OPTIONAL
860 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
864 Reallocates a buffer of type EfiReservedMemoryType.
866 Allocates and zeros the number bytes specified by NewSize from memory of type
867 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
868 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
869 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
870 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
871 enough memory remaining to satisfy the request, then NULL is returned.
873 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
874 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
876 @param OldSize The size, in bytes, of OldBuffer.
877 @param NewSize The size, in bytes, of the buffer to reallocate.
878 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
879 parameter that may be NULL.
881 @return A pointer to the allocated buffer or NULL if allocation fails.
886 ReallocateReservedPool (
889 IN VOID
*OldBuffer OPTIONAL
896 Frees a buffer that was previously allocated with one of the pool allocation functions in the
897 Memory Allocation Library.
899 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
900 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
901 resources, then this function will perform no actions.
903 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
906 @param Buffer Pointer to the buffer to free.
917 if (BufferInSmram (Buffer
)) {
919 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.
920 // So, SmmFreePool() service is used to free it.
922 Status
= SmmFreePool (Buffer
);
925 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.
926 // So, gBS->FreePool() service is used to free it.
928 Status
= gBS
->FreePool (Buffer
);
930 ASSERT_EFI_ERROR (Status
);