2 Support routines for memory allocation routines based
3 on SMM Services Table services for SMM phase drivers.
5 The PI System Management Mode Core Interface Specification only allows the use
6 of EfiRuntimeServicesCode and EfiRuntimeServicesData memory types for memory
7 allocations through the SMM Services Table. The functions in the Memory
8 Allocation Library use EfiBootServicesData as the default memory allocation
9 type. For this SMM specific instance of the Memory Allocation Library,
10 EfiRuntimeServicesData is used as the default memory type for all allocations.
11 In addition, allocation for the Reserved memory types are not supported and
12 will always return NULL.
14 Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
15 This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php.
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
27 #include <Protocol/SmmAccess2.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiBootServicesTableLib.h>
30 #include <Library/SmmServicesTableLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/DebugLib.h>
34 EFI_SMRAM_DESCRIPTOR
*mSmramRanges
;
35 UINTN mSmramRangeCount
;
38 The constructor function caches SMRAM ranges that are present in the system.
40 It will ASSERT() if SMM Access2 Protocol doesn't exist.
41 It will ASSERT() if SMRAM ranges can't be got.
42 It will ASSERT() if Resource can't be allocated for cache SMRAM range.
43 It will always return EFI_SUCCESS.
45 @param ImageHandle The firmware allocated handle for the EFI image.
46 @param SystemTable A pointer to the EFI System Table.
48 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
53 SmmMemoryAllocationLibConstructor (
54 IN EFI_HANDLE ImageHandle
,
55 IN EFI_SYSTEM_TABLE
*SystemTable
59 EFI_SMM_ACCESS2_PROTOCOL
*SmmAccess
;
63 // Locate SMM Access2 Protocol
65 Status
= gBS
->LocateProtocol (
66 &gEfiSmmAccess2ProtocolGuid
,
70 ASSERT_EFI_ERROR (Status
);
73 // Get SMRAM range information
76 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, NULL
);
77 ASSERT (Status
== EFI_BUFFER_TOO_SMALL
);
79 mSmramRanges
= (EFI_SMRAM_DESCRIPTOR
*) AllocatePool (Size
);
80 ASSERT (mSmramRanges
!= NULL
);
82 Status
= SmmAccess
->GetCapabilities (SmmAccess
, &Size
, mSmramRanges
);
83 ASSERT_EFI_ERROR (Status
);
85 mSmramRangeCount
= Size
/ sizeof (EFI_SMRAM_DESCRIPTOR
);
91 If SMM driver exits with an error, it must call this routine
92 to free the allocated resource before the exiting.
94 @param[in] ImageHandle The firmware allocated handle for the EFI image.
95 @param[in] SystemTable A pointer to the EFI System Table.
97 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
101 SmmMemoryAllocationLibDestructor (
102 IN EFI_HANDLE ImageHandle
,
103 IN EFI_SYSTEM_TABLE
*SystemTable
106 FreePool (mSmramRanges
);
112 Check whether the start address of buffer is within any of the SMRAM ranges.
114 @param[in] Buffer The pointer to the buffer to be checked.
116 @retval TURE The buffer is in SMRAM ranges.
117 @retval FALSE The buffer is out of SMRAM ranges.
127 for (Index
= 0; Index
< mSmramRangeCount
; Index
++) {
128 if (((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
>= mSmramRanges
[Index
].CpuStart
) &&
129 ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
< (mSmramRanges
[Index
].CpuStart
+ mSmramRanges
[Index
].PhysicalSize
))) {
138 Allocates one or more 4KB pages of a certain memory type.
140 Allocates the number of 4KB pages of a certain memory type and returns a pointer
141 to the allocated buffer. The buffer returned is aligned on a 4KB boundary. If
142 Pages is 0, then NULL is returned. If there is not enough memory remaining to
143 satisfy the request, then NULL is returned.
145 @param MemoryType The type of memory to allocate.
146 @param Pages The number of 4 KB pages to allocate.
148 @return A pointer to the allocated buffer or NULL if allocation fails.
152 InternalAllocatePages (
153 IN EFI_MEMORY_TYPE MemoryType
,
158 EFI_PHYSICAL_ADDRESS Memory
;
164 Status
= gSmst
->SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
165 if (EFI_ERROR (Status
)) {
168 return (VOID
*) (UINTN
) Memory
;
172 Allocates one or more 4KB pages of type EfiBootServicesData.
174 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer
175 to the allocated buffer. The buffer returned is aligned on a 4KB boundary. If
176 Pages is 0, then NULL is returned. If there is not enough memory remaining to
177 satisfy the request, then NULL is returned.
179 @param Pages The number of 4 KB pages to allocate.
181 @return A pointer to the allocated buffer or NULL if allocation fails.
190 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
194 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
196 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a
197 pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
198 If Pages is 0, then NULL is returned. If there is not enough memory remaining
199 to satisfy the request, then NULL is returned.
201 @param Pages The number of 4 KB pages to allocate.
203 @return A pointer to the allocated buffer or NULL if allocation fails.
208 AllocateRuntimePages (
212 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
216 Allocates one or more 4KB pages of type EfiReservedMemoryType.
218 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a
219 pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
220 If Pages is 0, then NULL is returned. If there is not enough memory remaining
221 to satisfy the request, then NULL is returned.
223 @param Pages The number of 4 KB pages to allocate.
225 @return A pointer to the allocated buffer or NULL if allocation fails.
230 AllocateReservedPages (
238 Frees one or more 4KB pages that were previously allocated with one of the page allocation
239 functions in the Memory Allocation Library.
241 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer.
242 Buffer must have been allocated on a previous call to the page allocation services
243 of the Memory Allocation Library. If it is not possible to free allocated pages,
244 then this function will perform no actions.
246 If Buffer was not allocated with a page allocation function in the Memory Allocation
247 Library, then ASSERT().
248 If Pages is zero, then ASSERT().
250 @param Buffer The pointer to the buffer of pages to free.
251 @param Pages The number of 4 KB pages to free.
264 if (BufferInSmram (Buffer
)) {
266 // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service.
267 // So, gSmst->SmmFreePages() service is used to free it.
269 Status
= gSmst
->SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
272 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
273 // So, gBS->FreePages() service is used to free it.
275 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
277 ASSERT_EFI_ERROR (Status
);
281 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
283 Allocates the number of 4KB pages specified by Pages of a certain memory type
284 with an alignment specified by Alignment. The allocated buffer is returned.
285 If Pages is 0, then NULL is returned. If there is not enough memory at the
286 specified alignment remaining to satisfy the request, then NULL is returned.
287 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
288 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
290 @param MemoryType The type of memory to allocate.
291 @param Pages The number of 4 KB pages to allocate.
292 @param Alignment The requested alignment of the allocation.
293 Must be a power of two.
294 If Alignment is zero, then byte alignment is used.
296 @return A pointer to the allocated buffer or NULL if allocation fails.
300 InternalAllocateAlignedPages (
301 IN EFI_MEMORY_TYPE MemoryType
,
307 EFI_PHYSICAL_ADDRESS Memory
;
310 UINTN UnalignedPages
;
314 // Alignment must be a power of two or zero.
316 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
321 if (Alignment
> EFI_PAGE_SIZE
) {
323 // Caculate the total number of pages since alignment is larger than page size.
325 AlignmentMask
= Alignment
- 1;
326 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
328 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
330 ASSERT (RealPages
> Pages
);
332 Status
= gSmst
->SmmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
333 if (EFI_ERROR (Status
)) {
336 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
337 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
338 if (UnalignedPages
> 0) {
340 // Free first unaligned page(s).
342 Status
= gSmst
->SmmFreePages (Memory
, UnalignedPages
);
343 ASSERT_EFI_ERROR (Status
);
345 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
346 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
347 if (UnalignedPages
> 0) {
349 // Free last unaligned page(s).
351 Status
= gSmst
->SmmFreePages (Memory
, UnalignedPages
);
352 ASSERT_EFI_ERROR (Status
);
356 // Do not over-allocate pages in this case.
358 Status
= gSmst
->SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
359 if (EFI_ERROR (Status
)) {
362 AlignedMemory
= (UINTN
) Memory
;
364 return (VOID
*) AlignedMemory
;
368 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
370 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData
371 with an alignment specified by Alignment. The allocated buffer is returned.
372 If Pages is 0, then NULL is returned. If there is not enough memory at the
373 specified alignment remaining to satisfy the request, then NULL is returned.
375 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
376 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
378 @param Pages The number of 4 KB pages to allocate.
379 @param Alignment The requested alignment of the allocation.
380 Must be a power of two.
381 If Alignment is zero, then byte alignment is used.
383 @return A pointer to the allocated buffer or NULL if allocation fails.
388 AllocateAlignedPages (
393 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
397 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
399 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData
400 with an alignment specified by Alignment. The allocated buffer is returned.
401 If Pages is 0, then NULL is returned. If there is not enough memory at the
402 specified alignment remaining to satisfy the request, then NULL is returned.
404 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
405 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
407 @param Pages The number of 4 KB pages to allocate.
408 @param Alignment The requested alignment of the allocation.
409 Must be a power of two.
410 If Alignment is zero, then byte alignment is used.
412 @return A pointer to the allocated buffer or NULL if allocation fails.
417 AllocateAlignedRuntimePages (
422 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
426 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
428 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType
429 with an alignment specified by Alignment. The allocated buffer is returned.
430 If Pages is 0, then NULL is returned. If there is not enough memory at the
431 specified alignment remaining to satisfy the request, then NULL is returned.
433 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
434 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().
436 @param Pages The number of 4 KB pages to allocate.
437 @param Alignment The requested alignment of the allocation.
438 Must be a power of two.
439 If Alignment is zero, then byte alignment is used.
441 @return A pointer to the allocated buffer or NULL if allocation fails.
446 AllocateAlignedReservedPages (
455 Frees one or more 4KB pages that were previously allocated with one of the aligned page
456 allocation functions in the Memory Allocation Library.
458 Frees the number of 4KB pages specified by Pages from the buffer specified by
459 Buffer. Buffer must have been allocated on a previous call to the aligned page
460 allocation services of the Memory Allocation Library. If it is not possible to
461 free allocated pages, then this function will perform no actions.
463 If Buffer was not allocated with an aligned page allocation function in the
464 Memory Allocation Library, then ASSERT().
465 If Pages is zero, then ASSERT().
467 @param Buffer The pointer to the buffer of pages to free.
468 @param Pages The number of 4 KB pages to free.
481 if (BufferInSmram (Buffer
)) {
483 // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service.
484 // So, gSmst->SmmFreePages() service is used to free it.
486 Status
= gSmst
->SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
489 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
490 // So, gBS->FreePages() service is used to free it.
492 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
494 ASSERT_EFI_ERROR (Status
);
498 Allocates a buffer of a certain pool type.
500 Allocates the number bytes specified by AllocationSize of a certain pool type
501 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
502 valid buffer of 0 size is returned. If there is not enough memory remaining to
503 satisfy the request, then NULL is returned.
505 @param MemoryType The type of memory to allocate.
506 @param AllocationSize The number of bytes to allocate.
508 @return A pointer to the allocated buffer or NULL if allocation fails.
512 InternalAllocatePool (
513 IN EFI_MEMORY_TYPE MemoryType
,
514 IN UINTN AllocationSize
520 Status
= gSmst
->SmmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
521 if (EFI_ERROR (Status
)) {
528 Allocates a buffer of type EfiBootServicesData.
530 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData
531 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
532 valid buffer of 0 size is returned. If there is not enough memory remaining to
533 satisfy the request, then NULL is returned.
535 @param AllocationSize The number of bytes to allocate.
537 @return A pointer to the allocated buffer or NULL if allocation fails.
543 IN UINTN AllocationSize
546 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
550 Allocates a buffer of type EfiRuntimeServicesData.
552 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData
553 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
554 valid buffer of 0 size is returned. If there is not enough memory remaining to
555 satisfy the request, then NULL is returned.
557 @param AllocationSize The number of bytes to allocate.
559 @return A pointer to the allocated buffer or NULL if allocation fails.
564 AllocateRuntimePool (
565 IN UINTN AllocationSize
568 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
572 Allocates a buffer of type EfiReservedMemoryType.
574 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType
575 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
576 valid buffer of 0 size is returned. If there is not enough memory remaining to
577 satisfy the request, then NULL is returned.
579 @param AllocationSize The number of bytes to allocate.
581 @return A pointer to the allocated buffer or NULL if allocation fails.
586 AllocateReservedPool (
587 IN UINTN AllocationSize
594 Allocates and zeros a buffer of a certain pool type.
596 Allocates the number bytes specified by AllocationSize of a certain pool type,
597 clears the buffer with zeros, and returns a pointer to the allocated buffer.
598 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
599 not enough memory remaining to satisfy the request, then NULL is returned.
601 @param PoolType The type of memory to allocate.
602 @param AllocationSize The number of bytes to allocate and zero.
604 @return A pointer to the allocated buffer or NULL if allocation fails.
608 InternalAllocateZeroPool (
609 IN EFI_MEMORY_TYPE PoolType
,
610 IN UINTN AllocationSize
615 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
616 if (Memory
!= NULL
) {
617 Memory
= ZeroMem (Memory
, AllocationSize
);
623 Allocates and zeros a buffer of type EfiBootServicesData.
625 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData,
626 clears the buffer with zeros, and returns a pointer to the allocated buffer.
627 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
628 not enough memory remaining to satisfy the request, then NULL is returned.
630 @param AllocationSize The number of bytes to allocate and zero.
632 @return A pointer to the allocated buffer or NULL if allocation fails.
638 IN UINTN AllocationSize
641 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
645 Allocates and zeros a buffer of type EfiRuntimeServicesData.
647 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,
648 clears the buffer with zeros, and returns a pointer to the allocated buffer.
649 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
650 not enough memory remaining to satisfy the request, then NULL is returned.
652 @param AllocationSize The number of bytes to allocate and zero.
654 @return A pointer to the allocated buffer or NULL if allocation fails.
659 AllocateRuntimeZeroPool (
660 IN UINTN AllocationSize
663 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
667 Allocates and zeros a buffer of type EfiReservedMemoryType.
669 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,
670 clears the buffer with zeros, and returns a pointer to the allocated buffer.
671 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
672 not enough memory remaining to satisfy the request, then NULL is returned.
674 @param AllocationSize The number of bytes to allocate and zero.
676 @return A pointer to the allocated buffer or NULL if allocation fails.
681 AllocateReservedZeroPool (
682 IN UINTN AllocationSize
689 Copies a buffer to an allocated buffer of a certain pool type.
691 Allocates the number bytes specified by AllocationSize of a certain pool type,
692 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
693 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
694 of 0 size is returned. If there is not enough memory remaining to satisfy the
695 request, then NULL is returned. If Buffer is NULL, then ASSERT().
696 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
698 @param PoolType The type of pool to allocate.
699 @param AllocationSize The number of bytes to allocate and zero.
700 @param Buffer The buffer to copy to the allocated buffer.
702 @return A pointer to the allocated buffer or NULL if allocation fails.
706 InternalAllocateCopyPool (
707 IN EFI_MEMORY_TYPE PoolType
,
708 IN UINTN AllocationSize
,
709 IN CONST VOID
*Buffer
714 ASSERT (Buffer
!= NULL
);
715 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
717 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
718 if (Memory
!= NULL
) {
719 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
725 Copies a buffer to an allocated buffer of type EfiBootServicesData.
727 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData,
728 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
729 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
730 of 0 size is returned. If there is not enough memory remaining to satisfy the
731 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.
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 EfiRuntimeServicesData.
755 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,
756 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
757 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
758 of 0 size is returned. If there is not enough memory remaining to satisfy the
759 request, then NULL is returned.
761 If Buffer is NULL, then ASSERT().
762 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
764 @param AllocationSize The number of bytes to allocate and zero.
765 @param Buffer The buffer to copy to the allocated buffer.
767 @return A pointer to the allocated buffer or NULL if allocation fails.
772 AllocateRuntimeCopyPool (
773 IN UINTN AllocationSize
,
774 IN CONST VOID
*Buffer
777 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
781 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
783 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,
784 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
785 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
786 of 0 size is returned. If there is not enough memory remaining to satisfy the
787 request, then NULL is returned.
789 If Buffer is NULL, then ASSERT().
790 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
792 @param AllocationSize The number of bytes to allocate and zero.
793 @param Buffer The buffer to copy to the allocated buffer.
795 @return A pointer to the allocated buffer or NULL if allocation fails.
800 AllocateReservedCopyPool (
801 IN UINTN AllocationSize
,
802 IN CONST VOID
*Buffer
809 Reallocates a buffer of a specified memory type.
811 Allocates and zeros the number bytes specified by NewSize from memory of the type
812 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
813 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
814 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
815 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
816 enough memory remaining to satisfy the request, then NULL is returned.
818 If the allocation of the new buffer is successful and the smaller of NewSize
819 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
821 @param PoolType The type of pool to allocate.
822 @param OldSize The size, in bytes, of OldBuffer.
823 @param NewSize The size, in bytes, of the buffer to reallocate.
824 @param OldBuffer The buffer to copy to the allocated buffer. This is an
825 optional parameter that may be NULL.
827 @return A pointer to the allocated buffer or NULL if allocation fails.
831 InternalReallocatePool (
832 IN EFI_MEMORY_TYPE PoolType
,
835 IN VOID
*OldBuffer OPTIONAL
840 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
841 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
842 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
843 FreePool (OldBuffer
);
849 Reallocates a buffer of type EfiBootServicesData.
851 Allocates and zeros the number bytes specified by NewSize from memory of type
852 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
853 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
854 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
855 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
856 enough memory remaining to satisfy the request, then NULL is returned.
858 If the allocation of the new buffer is successful and the smaller of NewSize
859 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
861 @param OldSize The size, in bytes, of OldBuffer.
862 @param NewSize The size, in bytes, of the buffer to reallocate.
863 @param OldBuffer The buffer to copy to the allocated buffer. This is an
864 optional parameter that may be NULL.
866 @return A pointer to the allocated buffer or NULL if allocation fails.
874 IN VOID
*OldBuffer OPTIONAL
877 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
881 Reallocates a buffer of type EfiRuntimeServicesData.
883 Allocates and zeros the number bytes specified by NewSize from memory of type
884 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize
885 and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
886 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
887 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
888 enough memory remaining to satisfy the request, then NULL is returned.
890 If the allocation of the new buffer is successful and the smaller of NewSize
891 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
893 @param OldSize The size, in bytes, of OldBuffer.
894 @param NewSize The size, in bytes, of the buffer to reallocate.
895 @param OldBuffer The buffer to copy to the allocated buffer. This is an
896 optional parameter that may be NULL.
898 @return A pointer to the allocated buffer or NULL if allocation fails.
903 ReallocateRuntimePool (
906 IN VOID
*OldBuffer OPTIONAL
909 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
913 Reallocates a buffer of type EfiReservedMemoryType.
915 Allocates and zeros the number bytes specified by NewSize from memory of type
916 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize
917 and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
918 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
919 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
920 enough memory remaining to satisfy the request, then NULL is returned.
922 If the allocation of the new buffer is successful and the smaller of NewSize
923 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
925 @param OldSize The size, in bytes, of OldBuffer.
926 @param NewSize The size, in bytes, of the buffer to reallocate.
927 @param OldBuffer The buffer to copy to the allocated buffer. This is an
928 optional parameter that may be NULL.
930 @return A pointer to the allocated buffer or NULL if allocation fails.
935 ReallocateReservedPool (
938 IN VOID
*OldBuffer OPTIONAL
945 Frees a buffer that was previously allocated with one of the pool allocation
946 functions in the Memory Allocation Library.
948 Frees the buffer specified by Buffer. Buffer must have been allocated on a
949 previous call to the pool allocation services of the Memory Allocation Library.
950 If it is not possible to free pool resources, then this function will perform
953 If Buffer was not allocated with a pool allocation function in the Memory
954 Allocation Library, then ASSERT().
956 @param Buffer The pointer to the buffer to free.
967 if (BufferInSmram (Buffer
)) {
969 // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePool() service.
970 // So, gSmst->SmmFreePool() service is used to free it.
972 Status
= gSmst
->SmmFreePool (Buffer
);
975 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.
976 // So, gBS->FreePool() service is used to free it.
978 Status
= gBS
->FreePool (Buffer
);
980 ASSERT_EFI_ERROR (Status
);