2 Support routines for memory allocation routines for use with drivers.
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. 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.
13 Module Name: MemoryAllocationLib.c
18 // The package level header files this module uses
22 // The protocols, PPI and GUID defintions for this module
25 // The Library classes this module consumes
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/PeiServicesTablePointerLib.h>
29 #include <Library/BaseMemoryLib.h>
30 #include <Library/DebugLib.h>
32 #include "MemoryAllocationLibInternals.h"
35 Allocates one or more 4KB pages of a certain memory type.
37 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
38 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
39 If there is not enough memory remaining to satisfy the request, then NULL is returned.
41 @param MemoryType The type of memory to allocate.
42 @param Pages The number of 4 KB pages to allocate.
44 @return A pointer to the allocated buffer or NULL if allocation fails.
48 InternalAllocatePages (
49 IN EFI_MEMORY_TYPE MemoryType
,
54 EFI_PHYSICAL_ADDRESS Memory
;
55 EFI_PEI_SERVICES
**PeiServices
;
61 PeiServices
= GetPeiServicesTablePointer ();
62 Status
= (*PeiServices
)->AllocatePages (PeiServices
, MemoryType
, Pages
, &Memory
);
63 if (EFI_ERROR (Status
)) {
66 return (VOID
*) (UINTN
) Memory
;
70 Allocates one or more 4KB pages of type EfiBootServicesData.
72 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
73 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
74 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
77 @param Pages The number of 4 KB pages to allocate.
79 @return A pointer to the allocated buffer or NULL if allocation fails.
88 return InternalAllocatePages (EfiBootServicesData
, Pages
);
92 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
94 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
95 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
96 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
99 @param Pages The number of 4 KB pages to allocate.
101 @return A pointer to the allocated buffer or NULL if allocation fails.
106 AllocateRuntimePages (
110 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
114 Allocates one or more 4KB pages of type EfiReservedMemoryType.
116 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
117 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
118 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
121 @param Pages The number of 4 KB pages to allocate.
123 @return A pointer to the allocated buffer or NULL if allocation fails.
128 AllocateReservedPages (
132 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
136 Frees one or more 4KB pages that were previously allocated with one of the page allocation
137 functions in the Memory Allocation Library.
139 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
140 must have been allocated on a previous call to the page allocation services of the Memory
142 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
144 If Pages is zero, then ASSERT().
146 @param Buffer Pointer to the buffer of pages to free.
147 @param Pages The number of 4 KB pages to free.
158 // PEI phase does not support to free pages, so leave it as NOP.
163 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
165 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
166 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
167 If there is not enough memory at the specified alignment remaining to satisfy the request, then
169 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
171 @param MemoryType The type of memory to allocate.
172 @param Pages The number of 4 KB pages to allocate.
173 @param Alignment The requested alignment of the allocation. Must be a power of two.
174 If Alignment is zero, then byte alignment is used.
176 @return A pointer to the allocated buffer or NULL if allocation fails.
180 InternalAllocateAlignedPages (
181 IN EFI_MEMORY_TYPE MemoryType
,
190 // Alignment must be a power of two or zero.
192 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
198 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
200 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
202 // We would rather waste some memory to save PEI code size.
204 Memory
= InternalAllocatePages (MemoryType
, Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
205 if (Alignment
== 0) {
206 AlignmentMask
= Alignment
;
208 AlignmentMask
= Alignment
- 1;
210 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
214 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
216 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
217 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
218 returned. If there is not enough memory at the specified alignment remaining to satisfy the
219 request, then NULL is returned.
220 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
222 @param Pages The number of 4 KB pages to allocate.
223 @param Alignment The requested alignment of the allocation. Must be a power of two.
224 If Alignment is zero, then byte alignment is used.
226 @return A pointer to the allocated buffer or NULL if allocation fails.
231 AllocateAlignedPages (
236 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
240 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
242 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
243 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
244 returned. If there is not enough memory at the specified alignment remaining to satisfy the
245 request, then NULL is returned.
246 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
248 @param Pages The number of 4 KB pages to allocate.
249 @param Alignment The requested alignment of the allocation. Must be a power of two.
250 If Alignment is zero, then byte alignment is used.
252 @return A pointer to the allocated buffer or NULL if allocation fails.
257 AllocateAlignedRuntimePages (
262 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
266 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
268 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
269 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
270 returned. If there is not enough memory at the specified alignment remaining to satisfy the
271 request, then NULL is returned.
272 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
274 @param Pages The number of 4 KB pages to allocate.
275 @param Alignment The requested alignment of the allocation. Must be a power of two.
276 If Alignment is zero, then byte alignment is used.
278 @return A pointer to the allocated buffer or NULL if allocation fails.
283 AllocateAlignedReservedPages (
288 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
292 Frees one or more 4KB pages that were previously allocated with one of the aligned page
293 allocation functions in the Memory Allocation Library.
295 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
296 must have been allocated on a previous call to the aligned page allocation services of the Memory
298 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
299 Library, then ASSERT().
300 If Pages is zero, then ASSERT().
302 @param Buffer Pointer to the buffer of pages to free.
303 @param Pages The number of 4 KB pages to free.
314 // PEI phase does not support to free pages, so leave it as NOP.
319 Allocates a buffer of a certain pool type.
321 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
322 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
323 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
325 @param MemoryType The type of memory to allocate.
326 @param AllocationSize The number of bytes to allocate.
328 @return A pointer to the allocated buffer or NULL if allocation fails.
332 InternalAllocatePool (
333 IN EFI_MEMORY_TYPE MemoryType
,
334 IN UINTN AllocationSize
338 // If we need lots of small runtime/reserved memory type from PEI in the future,
339 // we can consider providing a more complex algorithm that allocates runtime pages and
340 // provide pool allocations from those pages.
342 return InternalAllocatePages (MemoryType
, EFI_SIZE_TO_PAGES (AllocationSize
));
346 Allocates a buffer of type EfiBootServicesData.
348 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
349 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
350 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
352 @param AllocationSize The number of bytes to allocate.
354 @return A pointer to the allocated buffer or NULL if allocation fails.
360 IN UINTN AllocationSize
364 EFI_PEI_SERVICES
**PeiServices
;
367 PeiServices
= GetPeiServicesTablePointer ();
369 Status
= (*PeiServices
)->AllocatePool (PeiServices
, AllocationSize
, &Buffer
);
370 if (EFI_ERROR (Status
)) {
377 Allocates a buffer of type EfiRuntimeServicesData.
379 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
380 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
381 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
383 @param AllocationSize The number of bytes to allocate.
385 @return A pointer to the allocated buffer or NULL if allocation fails.
390 AllocateRuntimePool (
391 IN UINTN AllocationSize
394 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
398 Allocates a buffer of type EfieservedMemoryType.
400 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
401 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
402 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
404 @param AllocationSize The number of bytes to allocate.
406 @return A pointer to the allocated buffer or NULL if allocation fails.
411 AllocateReservedPool (
412 IN UINTN AllocationSize
415 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
419 Allocates and zeros a buffer of a certian pool type.
421 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
422 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
423 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
424 then NULL is returned.
426 @param PoolType The type of memory to allocate.
427 @param AllocationSize The number of bytes to allocate and zero.
429 @return A pointer to the allocated buffer or NULL if allocation fails.
433 InternalAllocateZeroPool (
434 IN EFI_MEMORY_TYPE PoolType
,
435 IN UINTN AllocationSize
440 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
441 if (Memory
!= NULL
) {
442 Memory
= ZeroMem (Memory
, AllocationSize
);
448 Allocates and zeros a buffer of type EfiBootServicesData.
450 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
451 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
452 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
453 request, then NULL is returned.
455 @param AllocationSize The number of bytes to allocate and zero.
457 @return A pointer to the allocated buffer or NULL if allocation fails.
463 IN UINTN AllocationSize
468 Memory
= AllocatePool (AllocationSize
);
469 if (Memory
!= NULL
) {
470 Memory
= ZeroMem (Memory
, AllocationSize
);
476 Allocates and zeros a buffer of type EfiRuntimeServicesData.
478 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
479 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
480 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
481 request, then NULL is returned.
483 @param AllocationSize The number of bytes to allocate and zero.
485 @return A pointer to the allocated buffer or NULL if allocation fails.
490 AllocateRuntimeZeroPool (
491 IN UINTN AllocationSize
494 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
498 Allocates and zeros a buffer of type EfiReservedMemoryType.
500 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
501 buffer with zeros, 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 satisfy the
503 request, then NULL is returned.
505 @param AllocationSize The number of bytes to allocate and zero.
507 @return A pointer to the allocated buffer or NULL if allocation fails.
512 AllocateReservedZeroPool (
513 IN UINTN AllocationSize
516 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
520 Copies a buffer to an allocated buffer of a certian pool type.
522 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
523 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
524 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
525 is not enough memory remaining to satisfy the request, then NULL is returned.
526 If Buffer is NULL, then ASSERT().
527 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
529 @param PoolType The type of pool to allocate.
530 @param AllocationSize The number of bytes to allocate and zero.
531 @param Buffer The buffer to copy to the allocated buffer.
533 @return A pointer to the allocated buffer or NULL if allocation fails.
537 InternalAllocateCopyPool (
538 IN EFI_MEMORY_TYPE PoolType
,
539 IN UINTN AllocationSize
,
540 IN CONST VOID
*Buffer
545 ASSERT (Buffer
!= NULL
);
546 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
548 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
549 if (Memory
!= NULL
) {
550 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
556 Copies a buffer to an allocated buffer of type EfiBootServicesData.
558 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
559 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
560 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
561 is not enough memory remaining to satisfy the request, then NULL is returned.
562 If Buffer is NULL, then ASSERT().
563 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
565 @param AllocationSize The number of bytes to allocate and zero.
566 @param Buffer The buffer to copy to the allocated buffer.
568 @return A pointer to the allocated buffer or NULL if allocation fails.
574 IN UINTN AllocationSize
,
575 IN CONST VOID
*Buffer
580 ASSERT (Buffer
!= NULL
);
581 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
583 Memory
= AllocatePool (AllocationSize
);
584 if (Memory
!= NULL
) {
585 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
591 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
593 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
594 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
595 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
596 is not enough memory remaining to satisfy the request, then NULL is returned.
597 If Buffer is NULL, then ASSERT().
598 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
600 @param AllocationSize The number of bytes to allocate and zero.
601 @param Buffer The buffer to copy to the allocated buffer.
603 @return A pointer to the allocated buffer or NULL if allocation fails.
608 AllocateRuntimeCopyPool (
609 IN UINTN AllocationSize
,
610 IN CONST VOID
*Buffer
613 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
617 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
619 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
620 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
621 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
622 is not enough memory remaining to satisfy the request, then NULL is returned.
623 If Buffer is NULL, then ASSERT().
624 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
626 @param AllocationSize The number of bytes to allocate and zero.
627 @param Buffer The buffer to copy to the allocated buffer.
629 @return A pointer to the allocated buffer or NULL if allocation fails.
634 AllocateReservedCopyPool (
635 IN UINTN AllocationSize
,
636 IN CONST VOID
*Buffer
639 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
643 Frees a buffer that was previously allocated with one of the pool allocation functions in the
644 Memory Allocation Library.
646 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
647 pool allocation services of the Memory Allocation Library.
648 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
651 @param Buffer Pointer to the buffer to free.
661 // PEI phase does not support to free pool, so leave it as NOP.
666 Allocates a buffer of a certain pool type at a specified alignment.
668 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
669 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
670 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
671 to satisfy the request, then NULL is returned.
672 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
674 @param PoolType The type of pool to allocate.
675 @param AllocationSize The number of bytes to allocate.
676 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
677 If Alignment is zero, then byte alignment is used.
679 @return A pointer to the allocated buffer or NULL if allocation fails.
683 InternalAllocateAlignedPool (
684 IN EFI_MEMORY_TYPE PoolType
,
685 IN UINTN AllocationSize
,
690 UINTN AlignedAddress
;
694 // Alignment must be a power of two or zero.
696 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
698 if (Alignment
== 0) {
699 AlignmentMask
= Alignment
;
701 AlignmentMask
= Alignment
- 1;
704 // Make sure that AllocationSize plus AlignmentMask does not overflow.
706 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
708 RawAddress
= InternalAllocatePool (PoolType
, AllocationSize
+ AlignmentMask
);
710 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
712 return (VOID
*) AlignedAddress
;
716 Allocates a buffer of type EfiBootServicesData at a specified alignment.
718 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
719 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
720 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
721 alignment remaining to satisfy the request, then NULL is returned.
722 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
724 @param AllocationSize The number of bytes to allocate.
725 @param Alignment The requested alignment of the allocation. Must be a power of two.
726 If Alignment is zero, then byte alignment is used.
728 @return A pointer to the allocated buffer or NULL if allocation fails.
733 AllocateAlignedPool (
734 IN UINTN AllocationSize
,
739 UINTN AlignedAddress
;
743 // Alignment must be a power of two or zero.
745 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
747 if (Alignment
== 0) {
748 AlignmentMask
= Alignment
;
750 AlignmentMask
= Alignment
- 1;
754 // Make sure that AllocationSize plus AlignmentMask does not overflow.
756 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
758 RawAddress
= AllocatePool (AllocationSize
+ AlignmentMask
);
760 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
762 return (VOID
*) AlignedAddress
;
766 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
768 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
769 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
770 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
771 alignment remaining to satisfy the request, then NULL is returned.
772 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
774 @param AllocationSize The number of bytes to allocate.
775 @param Alignment The requested alignment of the allocation. Must be a power of two.
776 If Alignment is zero, then byte alignment is used.
778 @return A pointer to the allocated buffer or NULL if allocation fails.
783 AllocateAlignedRuntimePool (
784 IN UINTN AllocationSize
,
788 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
792 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
794 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
795 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
796 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
797 alignment remaining to satisfy the request, then NULL is returned.
798 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
800 @param AllocationSize The number of bytes to allocate.
801 @param Alignment The requested alignment of the allocation. Must be a power of two.
802 If Alignment is zero, then byte alignment is used.
804 @return A pointer to the allocated buffer or NULL if allocation fails.
809 AllocateAlignedReservedPool (
810 IN UINTN AllocationSize
,
814 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
818 Allocates and zeros a buffer of a certain pool type at a specified alignment.
820 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
821 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
822 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
823 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
824 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
826 @param PoolType The type of pool to allocate.
827 @param AllocationSize The number of bytes to allocate.
828 @param Alignment The requested alignment of the allocation. Must be a power of two.
829 If Alignment is zero, then byte alignment is used.
831 @return A pointer to the allocated buffer or NULL if allocation fails.
835 InternalAllocateAlignedZeroPool (
836 IN EFI_MEMORY_TYPE PoolType
,
837 IN UINTN AllocationSize
,
843 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
844 if (Memory
!= NULL
) {
845 Memory
= ZeroMem (Memory
, AllocationSize
);
851 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
853 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
854 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
855 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
856 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
858 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
860 @param AllocationSize The number of bytes to allocate.
861 @param Alignment The requested alignment of the allocation. Must be a power of two.
862 If Alignment is zero, then byte alignment is used.
864 @return A pointer to the allocated buffer or NULL if allocation fails.
869 AllocateAlignedZeroPool (
870 IN UINTN AllocationSize
,
876 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
877 if (Memory
!= NULL
) {
878 Memory
= ZeroMem (Memory
, AllocationSize
);
884 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
886 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
887 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
888 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
889 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
891 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
893 @param AllocationSize The number of bytes to allocate.
894 @param Alignment The requested alignment of the allocation. Must be a power of two.
895 If Alignment is zero, then byte alignment is used.
897 @return A pointer to the allocated buffer or NULL if allocation fails.
902 AllocateAlignedRuntimeZeroPool (
903 IN UINTN AllocationSize
,
907 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
911 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
913 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
914 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
915 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
916 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
918 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
920 @param AllocationSize The number of bytes to allocate.
921 @param Alignment The requested alignment of the allocation. Must be a power of two.
922 If Alignment is zero, then byte alignment is used.
924 @return A pointer to the allocated buffer or NULL if allocation fails.
929 AllocateAlignedReservedZeroPool (
930 IN UINTN AllocationSize
,
934 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
938 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
940 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
941 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
942 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
943 to satisfy the request, then NULL is returned.
944 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
946 @param PoolType The type of pool to allocate.
947 @param AllocationSize The number of bytes to allocate.
948 @param Buffer The buffer to copy to the allocated buffer.
949 @param Alignment The requested alignment of the allocation. Must be a power of two.
950 If Alignment is zero, then byte alignment is used.
952 @return A pointer to the allocated buffer or NULL if allocation fails.
956 InternalAllocateAlignedCopyPool (
957 IN EFI_MEMORY_TYPE PoolType
,
958 IN UINTN AllocationSize
,
959 IN CONST VOID
*Buffer
,
965 ASSERT (Buffer
!= NULL
);
966 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
968 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
969 if (Memory
!= NULL
) {
970 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
976 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
978 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
979 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
980 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
981 alignment remaining to satisfy the request, then NULL is returned.
982 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
984 @param AllocationSize The number of bytes to allocate.
985 @param Buffer The buffer to copy to the allocated buffer.
986 @param Alignment The requested alignment of the allocation. Must be a power of two.
987 If Alignment is zero, then byte alignment is used.
989 @return A pointer to the allocated buffer or NULL if allocation fails.
994 AllocateAlignedCopyPool (
995 IN UINTN AllocationSize
,
996 IN CONST VOID
*Buffer
,
1002 ASSERT (Buffer
!= NULL
);
1003 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
1005 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
1006 if (Memory
!= NULL
) {
1007 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
1013 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
1015 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
1016 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1017 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1018 alignment remaining to satisfy the request, then NULL is returned.
1019 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1021 @param AllocationSize The number of bytes to allocate.
1022 @param Buffer The buffer to copy to the allocated buffer.
1023 @param Alignment The requested alignment of the allocation. Must be a power of two.
1024 If Alignment is zero, then byte alignment is used.
1026 @return A pointer to the allocated buffer or NULL if allocation fails.
1031 AllocateAlignedRuntimeCopyPool (
1032 IN UINTN AllocationSize
,
1033 IN CONST VOID
*Buffer
,
1037 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1041 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1043 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1044 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1045 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1046 alignment remaining to satisfy the request, then NULL is returned.
1047 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1049 @param AllocationSize The number of bytes to allocate.
1050 @param Buffer The buffer to copy to the allocated buffer.
1051 @param Alignment The requested alignment of the allocation. Must be a power of two.
1052 If Alignment is zero, then byte alignment is used.
1054 @return A pointer to the allocated buffer or NULL if allocation fails.
1059 AllocateAlignedReservedCopyPool (
1060 IN UINTN AllocationSize
,
1061 IN CONST VOID
*Buffer
,
1065 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1069 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1070 in the Memory Allocation Library.
1072 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1073 aligned pool allocation services of the Memory Allocation Library.
1074 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1075 Library, then ASSERT().
1077 @param Buffer Pointer to the buffer to free.
1087 // PEI phase does not support to free pool, so leave it as NOP.