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 - 2010, 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().
289 @param MemoryType The type of memory to allocate.
290 @param Pages The number of 4 KB pages to allocate.
291 @param Alignment The requested alignment of the allocation.
292 Must be a power of two.
293 If Alignment is zero, then byte alignment is used.
295 @return A pointer to the allocated buffer or NULL if allocation fails.
299 InternalAllocateAlignedPages (
300 IN EFI_MEMORY_TYPE MemoryType
,
306 EFI_PHYSICAL_ADDRESS Memory
;
309 UINTN UnalignedPages
;
313 // Alignment must be a power of two or zero.
315 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
320 if (Alignment
> EFI_PAGE_SIZE
) {
322 // Caculate the total number of pages since alignment is larger than page size.
324 AlignmentMask
= Alignment
- 1;
325 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
327 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
329 ASSERT (RealPages
> Pages
);
331 Status
= gSmst
->SmmAllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
332 if (EFI_ERROR (Status
)) {
335 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
336 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
337 if (UnalignedPages
> 0) {
339 // Free first unaligned page(s).
341 Status
= gSmst
->SmmFreePages (Memory
, UnalignedPages
);
342 ASSERT_EFI_ERROR (Status
);
344 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
345 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
346 if (UnalignedPages
> 0) {
348 // Free last unaligned page(s).
350 Status
= gSmst
->SmmFreePages (Memory
, UnalignedPages
);
351 ASSERT_EFI_ERROR (Status
);
355 // Do not over-allocate pages in this case.
357 Status
= gSmst
->SmmAllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
358 if (EFI_ERROR (Status
)) {
361 AlignedMemory
= (UINTN
) Memory
;
363 return (VOID
*) AlignedMemory
;
367 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
369 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData
370 with an alignment specified by Alignment. The allocated buffer is returned.
371 If Pages is 0, then NULL is returned. If there is not enough memory at the
372 specified alignment remaining to satisfy the request, then NULL is returned.
374 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
376 @param Pages The number of 4 KB pages to allocate.
377 @param Alignment The requested alignment of the allocation.
378 Must be a power of two.
379 If Alignment is zero, then byte alignment is used.
381 @return A pointer to the allocated buffer or NULL if allocation fails.
386 AllocateAlignedPages (
391 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
395 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
397 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData
398 with an alignment specified by Alignment. The allocated buffer is returned.
399 If Pages is 0, then NULL is returned. If there is not enough memory at the
400 specified alignment remaining to satisfy the request, then NULL is returned.
402 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
404 @param Pages The number of 4 KB pages to allocate.
405 @param Alignment The requested alignment of the allocation.
406 Must be a power of two.
407 If Alignment is zero, then byte alignment is used.
409 @return A pointer to the allocated buffer or NULL if allocation fails.
414 AllocateAlignedRuntimePages (
419 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
423 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
425 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType
426 with an alignment specified by Alignment. The allocated buffer is returned.
427 If Pages is 0, then NULL is returned. If there is not enough memory at the
428 specified alignment remaining to satisfy the request, then NULL is returned.
430 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
432 @param Pages The number of 4 KB pages to allocate.
433 @param Alignment The requested alignment of the allocation.
434 Must be a power of two.
435 If Alignment is zero, then byte alignment is used.
437 @return A pointer to the allocated buffer or NULL if allocation fails.
442 AllocateAlignedReservedPages (
451 Frees one or more 4KB pages that were previously allocated with one of the aligned page
452 allocation functions in the Memory Allocation Library.
454 Frees the number of 4KB pages specified by Pages from the buffer specified by
455 Buffer. Buffer must have been allocated on a previous call to the aligned page
456 allocation services of the Memory Allocation Library. If it is not possible to
457 free allocated pages, then this function will perform no actions.
459 If Buffer was not allocated with an aligned page allocation function in the
460 Memory Allocation Library, then ASSERT().
461 If Pages is zero, then ASSERT().
463 @param Buffer The pointer to the buffer of pages to free.
464 @param Pages The number of 4 KB pages to free.
477 if (BufferInSmram (Buffer
)) {
479 // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePages() service.
480 // So, gSmst->SmmFreePages() service is used to free it.
482 Status
= gSmst
->SmmFreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
485 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePages() service.
486 // So, gBS->FreePages() service is used to free it.
488 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
490 ASSERT_EFI_ERROR (Status
);
494 Allocates a buffer of a certain pool type.
496 Allocates the number bytes specified by AllocationSize of a certain pool type
497 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
498 valid buffer of 0 size is returned. If there is not enough memory remaining to
499 satisfy the request, then NULL is returned.
501 @param MemoryType The type of memory to allocate.
502 @param AllocationSize The number of bytes to allocate.
504 @return A pointer to the allocated buffer or NULL if allocation fails.
508 InternalAllocatePool (
509 IN EFI_MEMORY_TYPE MemoryType
,
510 IN UINTN AllocationSize
516 Status
= gSmst
->SmmAllocatePool (MemoryType
, AllocationSize
, &Memory
);
517 if (EFI_ERROR (Status
)) {
524 Allocates a buffer of type EfiBootServicesData.
526 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData
527 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
528 valid buffer of 0 size is returned. If there is not enough memory remaining to
529 satisfy the request, then NULL is returned.
531 @param AllocationSize The number of bytes to allocate.
533 @return A pointer to the allocated buffer or NULL if allocation fails.
539 IN UINTN AllocationSize
542 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
546 Allocates a buffer of type EfiRuntimeServicesData.
548 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData
549 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
550 valid buffer of 0 size is returned. If there is not enough memory remaining to
551 satisfy the request, then NULL is returned.
553 @param AllocationSize The number of bytes to allocate.
555 @return A pointer to the allocated buffer or NULL if allocation fails.
560 AllocateRuntimePool (
561 IN UINTN AllocationSize
564 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
568 Allocates a buffer of type EfiReservedMemoryType.
570 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType
571 and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
572 valid buffer of 0 size is returned. If there is not enough memory remaining to
573 satisfy the request, then NULL is returned.
575 @param AllocationSize The number of bytes to allocate.
577 @return A pointer to the allocated buffer or NULL if allocation fails.
582 AllocateReservedPool (
583 IN UINTN AllocationSize
590 Allocates and zeros a buffer of a certain pool type.
592 Allocates the number bytes specified by AllocationSize of a certain pool type,
593 clears the buffer with zeros, and returns a pointer to the allocated buffer.
594 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
595 not enough memory remaining to satisfy the request, then NULL is returned.
597 @param PoolType The type of memory to allocate.
598 @param AllocationSize The number of bytes to allocate and zero.
600 @return A pointer to the allocated buffer or NULL if allocation fails.
604 InternalAllocateZeroPool (
605 IN EFI_MEMORY_TYPE PoolType
,
606 IN UINTN AllocationSize
611 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
612 if (Memory
!= NULL
) {
613 Memory
= ZeroMem (Memory
, AllocationSize
);
619 Allocates and zeros a buffer of type EfiBootServicesData.
621 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData,
622 clears the buffer with zeros, and returns a pointer to the allocated buffer.
623 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
624 not enough memory remaining to satisfy the request, then NULL is returned.
626 @param AllocationSize The number of bytes to allocate and zero.
628 @return A pointer to the allocated buffer or NULL if allocation fails.
634 IN UINTN AllocationSize
637 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
641 Allocates and zeros a buffer of type EfiRuntimeServicesData.
643 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,
644 clears the buffer with zeros, and returns a pointer to the allocated buffer.
645 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
646 not enough memory remaining to satisfy the request, then NULL is returned.
648 @param AllocationSize The number of bytes to allocate and zero.
650 @return A pointer to the allocated buffer or NULL if allocation fails.
655 AllocateRuntimeZeroPool (
656 IN UINTN AllocationSize
659 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
663 Allocates and zeros a buffer of type EfiReservedMemoryType.
665 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,
666 clears the buffer with zeros, and returns a pointer to the allocated buffer.
667 If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is
668 not enough memory remaining to satisfy the request, then NULL is returned.
670 @param AllocationSize The number of bytes to allocate and zero.
672 @return A pointer to the allocated buffer or NULL if allocation fails.
677 AllocateReservedZeroPool (
678 IN UINTN AllocationSize
685 Copies a buffer to an allocated buffer of a certain pool type.
687 Allocates the number bytes specified by AllocationSize of a certain pool type,
688 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
689 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
690 of 0 size is returned. If there is not enough memory remaining to satisfy the
691 request, then NULL is returned. If Buffer is NULL, then ASSERT().
692 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
694 @param PoolType The type of pool to allocate.
695 @param AllocationSize The number of bytes to allocate and zero.
696 @param Buffer The buffer to copy to the allocated buffer.
698 @return A pointer to the allocated buffer or NULL if allocation fails.
702 InternalAllocateCopyPool (
703 IN EFI_MEMORY_TYPE PoolType
,
704 IN UINTN AllocationSize
,
705 IN CONST VOID
*Buffer
710 ASSERT (Buffer
!= NULL
);
711 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
713 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
714 if (Memory
!= NULL
) {
715 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
721 Copies a buffer to an allocated buffer of type EfiBootServicesData.
723 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData,
724 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
725 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
726 of 0 size is returned. If there is not enough memory remaining to satisfy the
727 request, then NULL is returned.
729 If Buffer is NULL, then ASSERT().
730 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
732 @param AllocationSize The number of bytes to allocate and zero.
733 @param Buffer The buffer to copy to the allocated buffer.
735 @return A pointer to the allocated buffer or NULL if allocation fails.
741 IN UINTN AllocationSize
,
742 IN CONST VOID
*Buffer
745 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
749 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
751 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData,
752 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
753 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
754 of 0 size is returned. If there is not enough memory remaining to satisfy the
755 request, then NULL is returned.
757 If Buffer is NULL, then ASSERT().
758 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
760 @param AllocationSize The number of bytes to allocate and zero.
761 @param Buffer The buffer to copy to the allocated buffer.
763 @return A pointer to the allocated buffer or NULL if allocation fails.
768 AllocateRuntimeCopyPool (
769 IN UINTN AllocationSize
,
770 IN CONST VOID
*Buffer
773 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
777 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
779 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType,
780 copies AllocationSize bytes from Buffer to the newly allocated buffer, and returns
781 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer
782 of 0 size is returned. If there is not enough memory remaining to satisfy the
783 request, then NULL is returned.
785 If Buffer is NULL, then ASSERT().
786 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT().
788 @param AllocationSize The number of bytes to allocate and zero.
789 @param Buffer The buffer to copy to the allocated buffer.
791 @return A pointer to the allocated buffer or NULL if allocation fails.
796 AllocateReservedCopyPool (
797 IN UINTN AllocationSize
,
798 IN CONST VOID
*Buffer
805 Reallocates a buffer of a specified memory type.
807 Allocates and zeros the number bytes specified by NewSize from memory of the type
808 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and
809 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
810 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
811 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
812 enough memory remaining to satisfy the request, then NULL is returned.
814 If the allocation of the new buffer is successful and the smaller of NewSize
815 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
817 @param PoolType The type of pool to allocate.
818 @param OldSize The size, in bytes, of OldBuffer.
819 @param NewSize The size, in bytes, of the buffer to reallocate.
820 @param OldBuffer The buffer to copy to the allocated buffer. This is an
821 optional parameter that may be NULL.
823 @return A pointer to the allocated buffer or NULL if allocation fails.
827 InternalReallocatePool (
828 IN EFI_MEMORY_TYPE PoolType
,
831 IN VOID
*OldBuffer OPTIONAL
836 NewBuffer
= InternalAllocateZeroPool (PoolType
, NewSize
);
837 if (NewBuffer
!= NULL
&& OldBuffer
!= NULL
) {
838 CopyMem (NewBuffer
, OldBuffer
, MIN (OldSize
, NewSize
));
839 FreePool (OldBuffer
);
845 Reallocates a buffer of type EfiBootServicesData.
847 Allocates and zeros the number bytes specified by NewSize from memory of type
848 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
849 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
850 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
851 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
852 enough memory remaining to satisfy the request, then NULL is returned.
854 If the allocation of the new buffer is successful and the smaller of NewSize
855 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
857 @param OldSize The size, in bytes, of OldBuffer.
858 @param NewSize The size, in bytes, of the buffer to reallocate.
859 @param OldBuffer The buffer to copy to the allocated buffer. This is an
860 optional parameter that may be NULL.
862 @return A pointer to the allocated buffer or NULL if allocation fails.
870 IN VOID
*OldBuffer OPTIONAL
873 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
877 Reallocates a buffer of type EfiRuntimeServicesData.
879 Allocates and zeros the number bytes specified by NewSize from memory of type
880 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize
881 and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
882 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
883 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
884 enough memory remaining to satisfy the request, then NULL is returned.
886 If the allocation of the new buffer is successful and the smaller of NewSize
887 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
889 @param OldSize The size, in bytes, of OldBuffer.
890 @param NewSize The size, in bytes, of the buffer to reallocate.
891 @param OldBuffer The buffer to copy to the allocated buffer. This is an
892 optional parameter that may be NULL.
894 @return A pointer to the allocated buffer or NULL if allocation fails.
899 ReallocateRuntimePool (
902 IN VOID
*OldBuffer OPTIONAL
905 return InternalReallocatePool (EfiRuntimeServicesData
, OldSize
, NewSize
, OldBuffer
);
909 Reallocates a buffer of type EfiReservedMemoryType.
911 Allocates and zeros the number bytes specified by NewSize from memory of type
912 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize
913 and NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
914 OldBuffer is freed. A pointer to the newly allocated buffer is returned.
915 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
916 enough memory remaining to satisfy the request, then NULL is returned.
918 If the allocation of the new buffer is successful and the smaller of NewSize
919 and OldSize is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
921 @param OldSize The size, in bytes, of OldBuffer.
922 @param NewSize The size, in bytes, of the buffer to reallocate.
923 @param OldBuffer The buffer to copy to the allocated buffer. This is an
924 optional parameter that may be NULL.
926 @return A pointer to the allocated buffer or NULL if allocation fails.
931 ReallocateReservedPool (
934 IN VOID
*OldBuffer OPTIONAL
941 Frees a buffer that was previously allocated with one of the pool allocation
942 functions in the Memory Allocation Library.
944 Frees the buffer specified by Buffer. Buffer must have been allocated on a
945 previous call to the pool allocation services of the Memory Allocation Library.
946 If it is not possible to free pool resources, then this function will perform
949 If Buffer was not allocated with a pool allocation function in the Memory
950 Allocation Library, then ASSERT().
952 @param Buffer The pointer to the buffer to free.
963 if (BufferInSmram (Buffer
)) {
965 // When Buffer is in SMRAM range, it should be allocated by gSmst->SmmAllocatePool() service.
966 // So, gSmst->SmmFreePool() service is used to free it.
968 Status
= gSmst
->SmmFreePool (Buffer
);
971 // When Buffer is out of SMRAM range, it should be allocated by gBS->AllocatePool() service.
972 // So, gBS->FreePool() service is used to free it.
974 Status
= gBS
->FreePool (Buffer
);
976 ASSERT_EFI_ERROR (Status
);