2 Support routines for memory allocation routines based on SMM Core internal functions.
4 Copyright (c) 2006 - 2013, 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().
254 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
256 @param MemoryType The type of memory to allocate.
257 @param Pages The number of 4 KB pages to allocate.
258 @param Alignment The requested alignment of the allocation. Must be a power of two.
259 If Alignment is zero, then byte alignment is used.
261 @return A pointer to the allocated buffer or NULL if allocation fails.
265 InternalAllocateAlignedPages (
266 IN EFI_MEMORY_TYPE MemoryType
,
272 EFI_PHYSICAL_ADDRESS Memory
;
275 UINTN UnalignedPages
;
279 // Alignment must be a power of two or zero.
281 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
286 if (Alignment
> EFI_PAGE_SIZE
) {
288 // Caculate the total number of pages since alignment is larger than page size.
290 AlignmentMask
= Alignment
- 1;
291 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
293 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
295 ASSERT (RealPages
> Pages
);
297 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
298 if (EFI_ERROR (Status
)) {
301 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
302 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
303 if (UnalignedPages
> 0) {
305 // Free first unaligned page(s).
307 Status
= SmmFreePages (Memory
, UnalignedPages
);
308 ASSERT_EFI_ERROR (Status
);
310 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
311 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
312 if (UnalignedPages
> 0) {
314 // Free last unaligned page(s).
316 Status
= SmmFreePages (Memory
, UnalignedPages
);
317 ASSERT_EFI_ERROR (Status
);
321 // Do not over-allocate pages in this case.
323 Status
= SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
324 if (EFI_ERROR (Status
)) {
327 AlignedMemory
= (UINTN
) Memory
;
329 return (VOID
*) AlignedMemory
;
333 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
335 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
336 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
337 returned. If there is not enough memory at the specified alignment remaining to satisfy the
338 request, then NULL is returned.
340 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
341 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
343 @param Pages The number of 4 KB pages to allocate.
344 @param Alignment The requested alignment of the allocation. Must be a power of two.
345 If Alignment is zero, then byte alignment is used.
347 @return A pointer to the allocated buffer or NULL if allocation fails.
352 AllocateAlignedPages (
357 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
361 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
363 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
364 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
365 returned. If there is not enough memory at the specified alignment remaining to satisfy the
366 request, then NULL is returned.
368 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
369 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
371 @param Pages The number of 4 KB pages to allocate.
372 @param Alignment The requested alignment of the allocation. Must be a power of two.
373 If Alignment is zero, then byte alignment is used.
375 @return A pointer to the allocated buffer or NULL if allocation fails.
380 AllocateAlignedRuntimePages (
385 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
389 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
391 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
392 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
393 returned. If there is not enough memory at the specified alignment remaining to satisfy the
394 request, then NULL is returned.
396 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
397 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
399 @param Pages The number of 4 KB pages to allocate.
400 @param Alignment The requested alignment of the allocation. Must be a power of two.
401 If Alignment is zero, then byte alignment is used.
403 @return A pointer to the allocated buffer or NULL if allocation fails.
408 AllocateAlignedReservedPages (
417 Frees one or more 4KB pages that were previously allocated with one of the aligned page
418 allocation functions in the Memory Allocation Library.
420 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
421 must have been allocated on a previous call to the aligned page allocation services of the Memory
422 Allocation Library. If it is not possible to free allocated pages, then this function will
425 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
426 Library, then ASSERT().
427 If Pages is zero, then ASSERT().
429 @param Buffer Pointer to the buffer of pages to free.
430 @param Pages The number of 4 KB pages to free.
443 if (BufferInSmram (Buffer
)) {
445 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePages() service.
446 // So, SmmFreePages() service is used to free it.
448 Status
= SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
451 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
452 // So, gBS->FreePages() service is used to free it.
454 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
456 ASSERT_EFI_ERROR (Status
);
460 Allocates a buffer of a certain pool type.
462 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
463 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
464 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
466 @param MemoryType The type of memory to allocate.
467 @param AllocationSize The number of bytes to allocate.
469 @return A pointer to the allocated buffer or NULL if allocation fails.
473 InternalAllocatePool (
474 IN EFI_MEMORY_TYPE MemoryType
,
475 IN UINTN AllocationSize
481 Status
= SmmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
482 if (EFI_ERROR (Status
)) {
489 Allocates a buffer of type EfiBootServicesData.
491 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
492 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
493 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
495 @param AllocationSize The number of bytes to allocate.
497 @return A pointer to the allocated buffer or NULL if allocation fails.
503 IN UINTN AllocationSize
506 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
510 Allocates a buffer of type EfiRuntimeServicesData.
512 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
513 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
514 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
516 @param AllocationSize The number of bytes to allocate.
518 @return A pointer to the allocated buffer or NULL if allocation fails.
523 AllocateRuntimePool (
524 IN UINTN AllocationSize
527 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
531 Allocates a buffer of type EfiReservedMemoryType.
533 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns
534 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
535 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
537 @param AllocationSize The number of bytes to allocate.
539 @return A pointer to the allocated buffer or NULL if allocation fails.
544 AllocateReservedPool (
545 IN UINTN AllocationSize
552 Allocates and zeros a buffer of a certain pool type.
554 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer
555 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
556 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
557 then NULL is returned.
559 @param PoolType The type of memory to allocate.
560 @param AllocationSize The number of bytes to allocate and zero.
562 @return A pointer to the allocated buffer or NULL if allocation fails.
566 InternalAllocateZeroPool (
567 IN EFI_MEMORY_TYPE PoolType
,
568 IN UINTN AllocationSize
573 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
574 if (Memory
!= NULL
) {
575 Memory
= ZeroMem (Memory
, AllocationSize
);
581 Allocates and zeros a buffer of type EfiBootServicesData.
583 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
584 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
585 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
586 request, then NULL is returned.
588 @param AllocationSize The number of bytes to allocate and zero.
590 @return A pointer to the allocated buffer or NULL if allocation fails.
596 IN UINTN AllocationSize
599 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
603 Allocates and zeros a buffer of type EfiRuntimeServicesData.
605 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
606 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
607 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
608 request, then NULL is returned.
610 @param AllocationSize The number of bytes to allocate and zero.
612 @return A pointer to the allocated buffer or NULL if allocation fails.
617 AllocateRuntimeZeroPool (
618 IN UINTN AllocationSize
621 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
625 Allocates and zeros a buffer of type EfiReservedMemoryType.
627 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
628 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
629 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
630 request, then NULL is returned.
632 @param AllocationSize The number of bytes to allocate and zero.
634 @return A pointer to the allocated buffer or NULL if allocation fails.
639 AllocateReservedZeroPool (
640 IN UINTN AllocationSize
647 Copies a buffer to an allocated buffer of a certain pool type.
649 Allocates the number bytes specified by AllocationSize of a certain pool type, copies
650 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
651 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
652 is not enough memory remaining to satisfy the request, then NULL is returned.
653 If Buffer is NULL, then ASSERT().
654 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
656 @param PoolType The type of pool to allocate.
657 @param AllocationSize The number of bytes to allocate and zero.
658 @param Buffer The buffer to copy to the allocated buffer.
660 @return A pointer to the allocated buffer or NULL if allocation fails.
664 InternalAllocateCopyPool (
665 IN EFI_MEMORY_TYPE PoolType
,
666 IN UINTN AllocationSize
,
667 IN CONST VOID
*Buffer
672 ASSERT (Buffer
!= NULL
);
673 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
675 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
676 if (Memory
!= NULL
) {
677 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
683 Copies a buffer to an allocated buffer of type EfiBootServicesData.
685 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
686 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
687 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
688 is not enough memory remaining to satisfy the request, then NULL is returned.
690 If Buffer is NULL, then ASSERT().
691 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
693 @param AllocationSize The number of bytes to allocate and zero.
694 @param Buffer The buffer to copy to the allocated buffer.
696 @return A pointer to the allocated buffer or NULL if allocation fails.
702 IN UINTN AllocationSize
,
703 IN CONST VOID
*Buffer
706 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
710 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
712 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
713 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
714 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
715 is not enough memory remaining to satisfy the request, then NULL is returned.
717 If Buffer is NULL, then ASSERT().
718 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
720 @param AllocationSize The number of bytes to allocate and zero.
721 @param Buffer The buffer to copy to the allocated buffer.
723 @return A pointer to the allocated buffer or NULL if allocation fails.
728 AllocateRuntimeCopyPool (
729 IN UINTN AllocationSize
,
730 IN CONST VOID
*Buffer
733 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
737 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
739 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
740 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
741 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
742 is not enough memory remaining to satisfy the request, then NULL is returned.
744 If Buffer is NULL, then ASSERT().
745 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
747 @param AllocationSize The number of bytes to allocate and zero.
748 @param Buffer The buffer to copy to the allocated buffer.
750 @return A pointer to the allocated buffer or NULL if allocation fails.
755 AllocateReservedCopyPool (
756 IN UINTN AllocationSize
,
757 IN CONST VOID
*Buffer
764 Reallocates a buffer of a specified memory type.
766 Allocates and zeros the number bytes specified by NewSize from memory of the type
767 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
768 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
769 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
770 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
771 enough memory remaining to satisfy the request, then NULL is returned.
773 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
774 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
776 @param PoolType The type of pool to allocate.
777 @param OldSize The size, in bytes, of OldBuffer.
778 @param NewSize The size, in bytes, of the buffer to reallocate.
779 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
780 parameter that may be NULL.
782 @return A pointer to the allocated buffer or NULL if allocation fails.
786 InternalReallocatePool (
787 IN EFI_MEMORY_TYPE PoolType
,
790 IN VOID
*OldBuffer OPTIONAL
795 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
796 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
797 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
798 FreePool (OldBuffer
);
804 Reallocates a buffer of type EfiBootServicesData.
806 Allocates and zeros the number bytes specified by NewSize from memory of type
807 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
808 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
809 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
810 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
811 enough memory remaining to satisfy the request, then NULL is returned.
813 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
814 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
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 optional
819 parameter that may be NULL.
821 @return A pointer to the allocated buffer or NULL if allocation fails.
829 IN VOID
*OldBuffer OPTIONAL
832 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
836 Reallocates a buffer of type EfiRuntimeServicesData.
838 Allocates and zeros the number bytes specified by NewSize from memory of type
839 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
840 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
841 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
842 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
843 enough memory remaining to satisfy the request, then NULL is returned.
845 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
846 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
848 @param OldSize The size, in bytes, of OldBuffer.
849 @param NewSize The size, in bytes, of the buffer to reallocate.
850 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
851 parameter that may be NULL.
853 @return A pointer to the allocated buffer or NULL if allocation fails.
858 ReallocateRuntimePool (
861 IN VOID
*OldBuffer OPTIONAL
864 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
868 Reallocates a buffer of type EfiReservedMemoryType.
870 Allocates and zeros the number bytes specified by NewSize from memory of type
871 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and
872 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
873 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
874 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
875 enough memory remaining to satisfy the request, then NULL is returned.
877 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
878 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
880 @param OldSize The size, in bytes, of OldBuffer.
881 @param NewSize The size, in bytes, of the buffer to reallocate.
882 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
883 parameter that may be NULL.
885 @return A pointer to the allocated buffer or NULL if allocation fails.
890 ReallocateReservedPool (
893 IN VOID
*OldBuffer OPTIONAL
900 Frees a buffer that was previously allocated with one of the pool allocation functions in the
901 Memory Allocation Library.
903 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
904 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
905 resources, then this function will perform no actions.
907 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
910 @param Buffer Pointer to the buffer to free.
921 if (BufferInSmram (Buffer
)) {
923 // When Buffer is in SMRAM range, it should be allocated by SmmAllocatePool() service.
924 // So, SmmFreePool() service is used to free it.
926 Status
= SmmFreePool (Buffer
);
929 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.
930 // So, gBS->FreePool() service is used to free it.
932 Status
= gBS
->FreePool (Buffer
);
934 ASSERT_EFI_ERROR (Status
);