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
17 #include "MemoryAllocationLibInternals.h"
20 Allocates one or more 4KB pages of a certain memory type.
22 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
23 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
24 If there is not enough memory remaining to satisfy the request, then NULL is returned.
26 @param MemoryType The type of memory to allocate.
27 @param Pages The number of 4 KB pages to allocate.
29 @return A pointer to the allocated buffer or NULL if allocation fails.
33 InternalAllocatePages (
34 IN EFI_MEMORY_TYPE MemoryType
,
39 EFI_PHYSICAL_ADDRESS Memory
;
40 EFI_PEI_SERVICES
**PeiServices
;
46 PeiServices
= GetPeiServicesTablePointer ();
47 Status
= (*PeiServices
)->AllocatePages (PeiServices
, MemoryType
, Pages
, &Memory
);
48 if (EFI_ERROR (Status
)) {
51 return (VOID
*) (UINTN
) Memory
;
55 Allocates one or more 4KB pages of type EfiBootServicesData.
57 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
58 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
59 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
62 @param Pages The number of 4 KB pages to allocate.
64 @return A pointer to the allocated buffer or NULL if allocation fails.
73 return InternalAllocatePages (EfiBootServicesData
, Pages
);
77 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
79 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
80 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
81 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
84 @param Pages The number of 4 KB pages to allocate.
86 @return A pointer to the allocated buffer or NULL if allocation fails.
91 AllocateRuntimePages (
95 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
99 Allocates one or more 4KB pages of type EfiReservedMemoryType.
101 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
102 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
103 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
106 @param Pages The number of 4 KB pages to allocate.
108 @return A pointer to the allocated buffer or NULL if allocation fails.
113 AllocateReservedPages (
117 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
121 Frees one or more 4KB pages that were previously allocated with one of the page allocation
122 functions in the Memory Allocation Library.
124 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
125 must have been allocated on a previous call to the page allocation services of the Memory
127 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
129 If Pages is zero, then ASSERT().
131 @param Buffer Pointer to the buffer of pages to free.
132 @param Pages The number of 4 KB pages to free.
143 // PEI phase does not support to free pages, so leave it as NOP.
148 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
150 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
151 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
152 If there is not enough memory at the specified alignment remaining to satisfy the request, then
154 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
156 @param MemoryType The type of memory to allocate.
157 @param Pages The number of 4 KB pages to allocate.
158 @param Alignment The requested alignment of the allocation. Must be a power of two.
159 If Alignment is zero, then byte alignment is used.
161 @return A pointer to the allocated buffer or NULL if allocation fails.
165 InternalAllocateAlignedPages (
166 IN EFI_MEMORY_TYPE MemoryType
,
175 // Alignment must be a power of two or zero.
177 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
183 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
185 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
187 // We would rather waste some memory to save PEI code size.
189 Memory
= InternalAllocatePages (MemoryType
, Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
190 if (Alignment
== 0) {
191 AlignmentMask
= Alignment
;
193 AlignmentMask
= Alignment
- 1;
195 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
199 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
201 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
202 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
203 returned. If there is not enough memory at the specified alignment remaining to satisfy the
204 request, then NULL is returned.
205 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
207 @param Pages The number of 4 KB pages to allocate.
208 @param Alignment The requested alignment of the allocation. Must be a power of two.
209 If Alignment is zero, then byte alignment is used.
211 @return A pointer to the allocated buffer or NULL if allocation fails.
216 AllocateAlignedPages (
221 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
225 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
227 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
228 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
229 returned. If there is not enough memory at the specified alignment remaining to satisfy the
230 request, then NULL is returned.
231 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
233 @param Pages The number of 4 KB pages to allocate.
234 @param Alignment The requested alignment of the allocation. Must be a power of two.
235 If Alignment is zero, then byte alignment is used.
237 @return A pointer to the allocated buffer or NULL if allocation fails.
242 AllocateAlignedRuntimePages (
247 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
251 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
253 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
254 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
255 returned. If there is not enough memory at the specified alignment remaining to satisfy the
256 request, then NULL is returned.
257 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
259 @param Pages The number of 4 KB pages to allocate.
260 @param Alignment The requested alignment of the allocation. Must be a power of two.
261 If Alignment is zero, then byte alignment is used.
263 @return A pointer to the allocated buffer or NULL if allocation fails.
268 AllocateAlignedReservedPages (
273 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
277 Frees one or more 4KB pages that were previously allocated with one of the aligned page
278 allocation functions in the Memory Allocation Library.
280 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
281 must have been allocated on a previous call to the aligned page allocation services of the Memory
283 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
284 Library, then ASSERT().
285 If Pages is zero, then ASSERT().
287 @param Buffer Pointer to the buffer of pages to free.
288 @param Pages The number of 4 KB pages to free.
299 // PEI phase does not support to free pages, so leave it as NOP.
304 Allocates a buffer of a certain pool type.
306 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
307 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
308 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
310 @param MemoryType The type of memory to allocate.
311 @param AllocationSize The number of bytes to allocate.
313 @return A pointer to the allocated buffer or NULL if allocation fails.
317 InternalAllocatePool (
318 IN EFI_MEMORY_TYPE MemoryType
,
319 IN UINTN AllocationSize
323 // If we need lots of small runtime/reserved memory type from PEI in the future,
324 // we can consider providing a more complex algorithm that allocates runtime pages and
325 // provide pool allocations from those pages.
327 return InternalAllocatePages (MemoryType
, EFI_SIZE_TO_PAGES (AllocationSize
));
331 Allocates a buffer of type EfiBootServicesData.
333 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
334 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
335 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
337 @param AllocationSize The number of bytes to allocate.
339 @return A pointer to the allocated buffer or NULL if allocation fails.
345 IN UINTN AllocationSize
349 EFI_PEI_SERVICES
**PeiServices
;
352 PeiServices
= GetPeiServicesTablePointer ();
354 Status
= (*PeiServices
)->AllocatePool (PeiServices
, AllocationSize
, &Buffer
);
355 if (EFI_ERROR (Status
)) {
362 Allocates a buffer of type EfiRuntimeServicesData.
364 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
365 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
366 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
368 @param AllocationSize The number of bytes to allocate.
370 @return A pointer to the allocated buffer or NULL if allocation fails.
375 AllocateRuntimePool (
376 IN UINTN AllocationSize
379 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
383 Allocates a buffer of type EfieservedMemoryType.
385 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
386 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
387 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
389 @param AllocationSize The number of bytes to allocate.
391 @return A pointer to the allocated buffer or NULL if allocation fails.
396 AllocateReservedPool (
397 IN UINTN AllocationSize
400 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
404 Allocates and zeros a buffer of a certian pool type.
406 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
407 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
408 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
409 then NULL is returned.
411 @param PoolType The type of memory to allocate.
412 @param AllocationSize The number of bytes to allocate and zero.
414 @return A pointer to the allocated buffer or NULL if allocation fails.
418 InternalAllocateZeroPool (
419 IN EFI_MEMORY_TYPE PoolType
,
420 IN UINTN AllocationSize
425 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
426 if (Memory
!= NULL
) {
427 Memory
= ZeroMem (Memory
, AllocationSize
);
433 Allocates and zeros a buffer of type EfiBootServicesData.
435 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
436 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
437 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
438 request, then NULL is returned.
440 @param AllocationSize The number of bytes to allocate and zero.
442 @return A pointer to the allocated buffer or NULL if allocation fails.
448 IN UINTN AllocationSize
453 Memory
= AllocatePool (AllocationSize
);
454 if (Memory
!= NULL
) {
455 Memory
= ZeroMem (Memory
, AllocationSize
);
461 Allocates and zeros a buffer of type EfiRuntimeServicesData.
463 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
464 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
465 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
466 request, then NULL is returned.
468 @param AllocationSize The number of bytes to allocate and zero.
470 @return A pointer to the allocated buffer or NULL if allocation fails.
475 AllocateRuntimeZeroPool (
476 IN UINTN AllocationSize
479 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
483 Allocates and zeros a buffer of type EfiReservedMemoryType.
485 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
486 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
487 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
488 request, then NULL is returned.
490 @param AllocationSize The number of bytes to allocate and zero.
492 @return A pointer to the allocated buffer or NULL if allocation fails.
497 AllocateReservedZeroPool (
498 IN UINTN AllocationSize
501 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
505 Copies a buffer to an allocated buffer of a certian pool type.
507 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
508 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
509 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
510 is not enough memory remaining to satisfy the request, then NULL is returned.
511 If Buffer is NULL, then ASSERT().
512 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
514 @param PoolType The type of pool to allocate.
515 @param AllocationSize The number of bytes to allocate and zero.
516 @param Buffer The buffer to copy to the allocated buffer.
518 @return A pointer to the allocated buffer or NULL if allocation fails.
522 InternalAllocateCopyPool (
523 IN EFI_MEMORY_TYPE PoolType
,
524 IN UINTN AllocationSize
,
525 IN CONST VOID
*Buffer
530 ASSERT (Buffer
!= NULL
);
531 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
533 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
534 if (Memory
!= NULL
) {
535 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
541 Copies a buffer to an allocated buffer of type EfiBootServicesData.
543 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
544 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
545 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
546 is not enough memory remaining to satisfy the request, then NULL is returned.
547 If Buffer is NULL, then ASSERT().
548 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
550 @param AllocationSize The number of bytes to allocate and zero.
551 @param Buffer The buffer to copy to the allocated buffer.
553 @return A pointer to the allocated buffer or NULL if allocation fails.
559 IN UINTN AllocationSize
,
560 IN CONST VOID
*Buffer
565 ASSERT (Buffer
!= NULL
);
566 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
568 Memory
= AllocatePool (AllocationSize
);
569 if (Memory
!= NULL
) {
570 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
576 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
578 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
579 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
580 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
581 is not enough memory remaining to satisfy the request, then NULL is returned.
582 If Buffer is NULL, then ASSERT().
583 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
585 @param AllocationSize The number of bytes to allocate and zero.
586 @param Buffer The buffer to copy to the allocated buffer.
588 @return A pointer to the allocated buffer or NULL if allocation fails.
593 AllocateRuntimeCopyPool (
594 IN UINTN AllocationSize
,
595 IN CONST VOID
*Buffer
598 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
602 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
604 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
605 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
606 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
607 is not enough memory remaining to satisfy the request, then NULL is returned.
608 If Buffer is NULL, then ASSERT().
609 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
611 @param AllocationSize The number of bytes to allocate and zero.
612 @param Buffer The buffer to copy to the allocated buffer.
614 @return A pointer to the allocated buffer or NULL if allocation fails.
619 AllocateReservedCopyPool (
620 IN UINTN AllocationSize
,
621 IN CONST VOID
*Buffer
624 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
628 Frees a buffer that was previously allocated with one of the pool allocation functions in the
629 Memory Allocation Library.
631 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
632 pool allocation services of the Memory Allocation Library.
633 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
636 @param Buffer Pointer to the buffer to free.
646 // PEI phase does not support to free pool, so leave it as NOP.
651 Allocates a buffer of a certain pool type at a specified alignment.
653 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
654 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
655 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
656 to satisfy the request, then NULL is returned.
657 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
659 @param PoolType The type of pool to allocate.
660 @param AllocationSize The number of bytes to allocate.
661 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
662 If Alignment is zero, then byte alignment is used.
664 @return A pointer to the allocated buffer or NULL if allocation fails.
668 InternalAllocateAlignedPool (
669 IN EFI_MEMORY_TYPE PoolType
,
670 IN UINTN AllocationSize
,
675 UINTN AlignedAddress
;
679 // Alignment must be a power of two or zero.
681 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
683 if (Alignment
== 0) {
684 AlignmentMask
= Alignment
;
686 AlignmentMask
= Alignment
- 1;
689 // Make sure that AllocationSize plus AlignmentMask does not overflow.
691 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
693 RawAddress
= InternalAllocatePool (PoolType
, AllocationSize
+ AlignmentMask
);
695 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
697 return (VOID
*) AlignedAddress
;
701 Allocates a buffer of type EfiBootServicesData at a specified alignment.
703 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
704 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
705 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
706 alignment remaining to satisfy the request, then NULL is returned.
707 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
709 @param AllocationSize The number of bytes to allocate.
710 @param Alignment The requested alignment of the allocation. Must be a power of two.
711 If Alignment is zero, then byte alignment is used.
713 @return A pointer to the allocated buffer or NULL if allocation fails.
718 AllocateAlignedPool (
719 IN UINTN AllocationSize
,
724 UINTN AlignedAddress
;
728 // Alignment must be a power of two or zero.
730 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
732 if (Alignment
== 0) {
733 AlignmentMask
= Alignment
;
735 AlignmentMask
= Alignment
- 1;
739 // Make sure that AllocationSize plus AlignmentMask does not overflow.
741 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
743 RawAddress
= AllocatePool (AllocationSize
+ AlignmentMask
);
745 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
747 return (VOID
*) AlignedAddress
;
751 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
753 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
754 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
755 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
756 alignment remaining to satisfy the request, then NULL is returned.
757 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
759 @param AllocationSize The number of bytes to allocate.
760 @param Alignment The requested alignment of the allocation. Must be a power of two.
761 If Alignment is zero, then byte alignment is used.
763 @return A pointer to the allocated buffer or NULL if allocation fails.
768 AllocateAlignedRuntimePool (
769 IN UINTN AllocationSize
,
773 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
777 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
779 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
780 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
781 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
782 alignment remaining to satisfy the request, then NULL is returned.
783 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
785 @param AllocationSize The number of bytes to allocate.
786 @param Alignment The requested alignment of the allocation. Must be a power of two.
787 If Alignment is zero, then byte alignment is used.
789 @return A pointer to the allocated buffer or NULL if allocation fails.
794 AllocateAlignedReservedPool (
795 IN UINTN AllocationSize
,
799 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
803 Allocates and zeros a buffer of a certain pool type at a specified alignment.
805 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
806 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
807 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
808 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
809 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
811 @param PoolType The type of pool to allocate.
812 @param AllocationSize The number of bytes to allocate.
813 @param Alignment The requested alignment of the allocation. Must be a power of two.
814 If Alignment is zero, then byte alignment is used.
816 @return A pointer to the allocated buffer or NULL if allocation fails.
820 InternalAllocateAlignedZeroPool (
821 IN EFI_MEMORY_TYPE PoolType
,
822 IN UINTN AllocationSize
,
828 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
829 if (Memory
!= NULL
) {
830 Memory
= ZeroMem (Memory
, AllocationSize
);
836 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
838 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
839 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
840 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
841 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
843 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
845 @param AllocationSize The number of bytes to allocate.
846 @param Alignment The requested alignment of the allocation. Must be a power of two.
847 If Alignment is zero, then byte alignment is used.
849 @return A pointer to the allocated buffer or NULL if allocation fails.
854 AllocateAlignedZeroPool (
855 IN UINTN AllocationSize
,
861 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
862 if (Memory
!= NULL
) {
863 Memory
= ZeroMem (Memory
, AllocationSize
);
869 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
871 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
872 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
873 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
874 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
876 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
878 @param AllocationSize The number of bytes to allocate.
879 @param Alignment The requested alignment of the allocation. Must be a power of two.
880 If Alignment is zero, then byte alignment is used.
882 @return A pointer to the allocated buffer or NULL if allocation fails.
887 AllocateAlignedRuntimeZeroPool (
888 IN UINTN AllocationSize
,
892 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
896 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
898 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
899 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
900 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
901 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
903 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
905 @param AllocationSize The number of bytes to allocate.
906 @param Alignment The requested alignment of the allocation. Must be a power of two.
907 If Alignment is zero, then byte alignment is used.
909 @return A pointer to the allocated buffer or NULL if allocation fails.
914 AllocateAlignedReservedZeroPool (
915 IN UINTN AllocationSize
,
919 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
923 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
925 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
926 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
927 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
928 to satisfy the request, then NULL is returned.
929 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
931 @param PoolType The type of pool to allocate.
932 @param AllocationSize The number of bytes to allocate.
933 @param Buffer The buffer to copy to the allocated buffer.
934 @param Alignment The requested alignment of the allocation. Must be a power of two.
935 If Alignment is zero, then byte alignment is used.
937 @return A pointer to the allocated buffer or NULL if allocation fails.
941 InternalAllocateAlignedCopyPool (
942 IN EFI_MEMORY_TYPE PoolType
,
943 IN UINTN AllocationSize
,
944 IN CONST VOID
*Buffer
,
950 ASSERT (Buffer
!= NULL
);
951 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
953 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
954 if (Memory
!= NULL
) {
955 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
961 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
963 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
964 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
965 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
966 alignment remaining to satisfy the request, then NULL is returned.
967 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
969 @param AllocationSize The number of bytes to allocate.
970 @param Buffer The buffer to copy to the allocated buffer.
971 @param Alignment The requested alignment of the allocation. Must be a power of two.
972 If Alignment is zero, then byte alignment is used.
974 @return A pointer to the allocated buffer or NULL if allocation fails.
979 AllocateAlignedCopyPool (
980 IN UINTN AllocationSize
,
981 IN CONST VOID
*Buffer
,
987 ASSERT (Buffer
!= NULL
);
988 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
990 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
991 if (Memory
!= NULL
) {
992 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
998 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
1000 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
1001 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1002 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1003 alignment remaining to satisfy the request, then NULL is returned.
1004 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1006 @param AllocationSize The number of bytes to allocate.
1007 @param Buffer The buffer to copy to the allocated buffer.
1008 @param Alignment The requested alignment of the allocation. Must be a power of two.
1009 If Alignment is zero, then byte alignment is used.
1011 @return A pointer to the allocated buffer or NULL if allocation fails.
1016 AllocateAlignedRuntimeCopyPool (
1017 IN UINTN AllocationSize
,
1018 IN CONST VOID
*Buffer
,
1022 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1026 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1028 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1029 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1030 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1031 alignment remaining to satisfy the request, then NULL is returned.
1032 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1034 @param AllocationSize The number of bytes to allocate.
1035 @param Buffer The buffer to copy to the allocated buffer.
1036 @param Alignment The requested alignment of the allocation. Must be a power of two.
1037 If Alignment is zero, then byte alignment is used.
1039 @return A pointer to the allocated buffer or NULL if allocation fails.
1044 AllocateAlignedReservedCopyPool (
1045 IN UINTN AllocationSize
,
1046 IN CONST VOID
*Buffer
,
1050 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1054 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1055 in the Memory Allocation Library.
1057 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1058 aligned pool allocation services of the Memory Allocation Library.
1059 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1060 Library, then ASSERT().
1062 @param Buffer Pointer to the buffer to free.
1072 // PEI phase does not support to free pool, so leave it as NOP.