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 // Include common header file for this module.
20 #include "CommonHeader.h"
22 #include "MemoryAllocationLibInternals.h"
25 Allocates one or more 4KB pages of a certain memory type.
27 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
28 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
29 If there is not enough memory remaining to satisfy the request, then NULL is returned.
31 @param MemoryType The type of memory to allocate.
32 @param Pages The number of 4 KB pages to allocate.
34 @return A pointer to the allocated buffer or NULL if allocation fails.
38 InternalAllocatePages (
39 IN EFI_MEMORY_TYPE MemoryType
,
44 EFI_PHYSICAL_ADDRESS Memory
;
45 EFI_PEI_SERVICES
**PeiServices
;
51 PeiServices
= GetPeiServicesTablePointer ();
52 Status
= (*PeiServices
)->AllocatePages (PeiServices
, MemoryType
, Pages
, &Memory
);
53 if (EFI_ERROR (Status
)) {
56 return (VOID
*) (UINTN
) Memory
;
60 Allocates one or more 4KB pages of type EfiBootServicesData.
62 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
63 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
64 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
67 @param Pages The number of 4 KB pages to allocate.
69 @return A pointer to the allocated buffer or NULL if allocation fails.
78 return InternalAllocatePages (EfiBootServicesData
, Pages
);
82 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
84 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
85 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
86 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
89 @param Pages The number of 4 KB pages to allocate.
91 @return A pointer to the allocated buffer or NULL if allocation fails.
96 AllocateRuntimePages (
100 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
104 Allocates one or more 4KB pages of type EfiReservedMemoryType.
106 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
107 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
108 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
111 @param Pages The number of 4 KB pages to allocate.
113 @return A pointer to the allocated buffer or NULL if allocation fails.
118 AllocateReservedPages (
122 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
126 Frees one or more 4KB pages that were previously allocated with one of the page allocation
127 functions in the Memory Allocation Library.
129 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
130 must have been allocated on a previous call to the page allocation services of the Memory
132 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
134 If Pages is zero, then ASSERT().
136 @param Buffer Pointer to the buffer of pages to free.
137 @param Pages The number of 4 KB pages to free.
148 // PEI phase does not support to free pages, so leave it as NOP.
153 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
155 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
156 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
157 If there is not enough memory at the specified alignment remaining to satisfy the request, then
159 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
161 @param MemoryType The type of memory to allocate.
162 @param Pages The number of 4 KB pages to allocate.
163 @param Alignment The requested alignment of the allocation. Must be a power of two.
164 If Alignment is zero, then byte alignment is used.
166 @return A pointer to the allocated buffer or NULL if allocation fails.
170 InternalAllocateAlignedPages (
171 IN EFI_MEMORY_TYPE MemoryType
,
180 // Alignment must be a power of two or zero.
182 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
188 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
190 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
192 // We would rather waste some memory to save PEI code size.
194 Memory
= InternalAllocatePages (MemoryType
, Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
195 if (Alignment
== 0) {
196 AlignmentMask
= Alignment
;
198 AlignmentMask
= Alignment
- 1;
200 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
204 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
206 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
207 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
208 returned. If there is not enough memory at the specified alignment remaining to satisfy the
209 request, then NULL is returned.
210 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
212 @param Pages The number of 4 KB pages to allocate.
213 @param Alignment The requested alignment of the allocation. Must be a power of two.
214 If Alignment is zero, then byte alignment is used.
216 @return A pointer to the allocated buffer or NULL if allocation fails.
221 AllocateAlignedPages (
226 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
230 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
232 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
233 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
234 returned. If there is not enough memory at the specified alignment remaining to satisfy the
235 request, then NULL is returned.
236 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
238 @param Pages The number of 4 KB pages to allocate.
239 @param Alignment The requested alignment of the allocation. Must be a power of two.
240 If Alignment is zero, then byte alignment is used.
242 @return A pointer to the allocated buffer or NULL if allocation fails.
247 AllocateAlignedRuntimePages (
252 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
256 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
258 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
259 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
260 returned. If there is not enough memory at the specified alignment remaining to satisfy the
261 request, then NULL is returned.
262 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
264 @param Pages The number of 4 KB pages to allocate.
265 @param Alignment The requested alignment of the allocation. Must be a power of two.
266 If Alignment is zero, then byte alignment is used.
268 @return A pointer to the allocated buffer or NULL if allocation fails.
273 AllocateAlignedReservedPages (
278 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
282 Frees one or more 4KB pages that were previously allocated with one of the aligned page
283 allocation functions in the Memory Allocation Library.
285 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
286 must have been allocated on a previous call to the aligned page allocation services of the Memory
288 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
289 Library, then ASSERT().
290 If Pages is zero, then ASSERT().
292 @param Buffer Pointer to the buffer of pages to free.
293 @param Pages The number of 4 KB pages to free.
304 // PEI phase does not support to free pages, so leave it as NOP.
309 Allocates a buffer of a certain pool type.
311 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
312 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
313 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
315 @param MemoryType The type of memory to allocate.
316 @param AllocationSize The number of bytes to allocate.
318 @return A pointer to the allocated buffer or NULL if allocation fails.
322 InternalAllocatePool (
323 IN EFI_MEMORY_TYPE MemoryType
,
324 IN UINTN AllocationSize
328 // If we need lots of small runtime/reserved memory type from PEI in the future,
329 // we can consider providing a more complex algorithm that allocates runtime pages and
330 // provide pool allocations from those pages.
332 return InternalAllocatePages (MemoryType
, EFI_SIZE_TO_PAGES (AllocationSize
));
336 Allocates a buffer of type EfiBootServicesData.
338 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
339 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
340 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
342 @param AllocationSize The number of bytes to allocate.
344 @return A pointer to the allocated buffer or NULL if allocation fails.
350 IN UINTN AllocationSize
354 EFI_PEI_SERVICES
**PeiServices
;
357 PeiServices
= GetPeiServicesTablePointer ();
359 Status
= (*PeiServices
)->AllocatePool (PeiServices
, AllocationSize
, &Buffer
);
360 if (EFI_ERROR (Status
)) {
367 Allocates a buffer of type EfiRuntimeServicesData.
369 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
370 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
371 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
373 @param AllocationSize The number of bytes to allocate.
375 @return A pointer to the allocated buffer or NULL if allocation fails.
380 AllocateRuntimePool (
381 IN UINTN AllocationSize
384 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
388 Allocates a buffer of type EfieservedMemoryType.
390 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
391 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
392 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
394 @param AllocationSize The number of bytes to allocate.
396 @return A pointer to the allocated buffer or NULL if allocation fails.
401 AllocateReservedPool (
402 IN UINTN AllocationSize
405 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
409 Allocates and zeros a buffer of a certian pool type.
411 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
412 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
413 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
414 then NULL is returned.
416 @param PoolType The type of memory to allocate.
417 @param AllocationSize The number of bytes to allocate and zero.
419 @return A pointer to the allocated buffer or NULL if allocation fails.
423 InternalAllocateZeroPool (
424 IN EFI_MEMORY_TYPE PoolType
,
425 IN UINTN AllocationSize
430 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
431 if (Memory
!= NULL
) {
432 Memory
= ZeroMem (Memory
, AllocationSize
);
438 Allocates and zeros a buffer of type EfiBootServicesData.
440 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
441 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
442 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
443 request, then NULL is returned.
445 @param AllocationSize The number of bytes to allocate and zero.
447 @return A pointer to the allocated buffer or NULL if allocation fails.
453 IN UINTN AllocationSize
458 Memory
= AllocatePool (AllocationSize
);
459 if (Memory
!= NULL
) {
460 Memory
= ZeroMem (Memory
, AllocationSize
);
466 Allocates and zeros a buffer of type EfiRuntimeServicesData.
468 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
469 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
470 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
471 request, then NULL is returned.
473 @param AllocationSize The number of bytes to allocate and zero.
475 @return A pointer to the allocated buffer or NULL if allocation fails.
480 AllocateRuntimeZeroPool (
481 IN UINTN AllocationSize
484 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
488 Allocates and zeros a buffer of type EfiReservedMemoryType.
490 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
491 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
492 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
493 request, then NULL is returned.
495 @param AllocationSize The number of bytes to allocate and zero.
497 @return A pointer to the allocated buffer or NULL if allocation fails.
502 AllocateReservedZeroPool (
503 IN UINTN AllocationSize
506 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
510 Copies a buffer to an allocated buffer of a certian pool type.
512 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
513 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
514 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
515 is not enough memory remaining to satisfy the request, then NULL is returned.
516 If Buffer is NULL, then ASSERT().
517 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
519 @param PoolType The type of pool to allocate.
520 @param AllocationSize The number of bytes to allocate and zero.
521 @param Buffer The buffer to copy to the allocated buffer.
523 @return A pointer to the allocated buffer or NULL if allocation fails.
527 InternalAllocateCopyPool (
528 IN EFI_MEMORY_TYPE PoolType
,
529 IN UINTN AllocationSize
,
530 IN CONST VOID
*Buffer
535 ASSERT (Buffer
!= NULL
);
536 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
538 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
539 if (Memory
!= NULL
) {
540 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
546 Copies a buffer to an allocated buffer of type EfiBootServicesData.
548 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
549 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
550 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
551 is not enough memory remaining to satisfy the request, then NULL is returned.
552 If Buffer is NULL, then ASSERT().
553 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
555 @param AllocationSize The number of bytes to allocate and zero.
556 @param Buffer The buffer to copy to the allocated buffer.
558 @return A pointer to the allocated buffer or NULL if allocation fails.
564 IN UINTN AllocationSize
,
565 IN CONST VOID
*Buffer
570 ASSERT (Buffer
!= NULL
);
571 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
573 Memory
= AllocatePool (AllocationSize
);
574 if (Memory
!= NULL
) {
575 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
581 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
583 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
584 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
585 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
586 is not enough memory remaining to satisfy the request, then NULL is returned.
587 If Buffer is NULL, then ASSERT().
588 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
590 @param AllocationSize The number of bytes to allocate and zero.
591 @param Buffer The buffer to copy to the allocated buffer.
593 @return A pointer to the allocated buffer or NULL if allocation fails.
598 AllocateRuntimeCopyPool (
599 IN UINTN AllocationSize
,
600 IN CONST VOID
*Buffer
603 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
607 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
609 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
610 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
611 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
612 is not enough memory remaining to satisfy the request, then NULL is returned.
613 If Buffer is NULL, then ASSERT().
614 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
616 @param AllocationSize The number of bytes to allocate and zero.
617 @param Buffer The buffer to copy to the allocated buffer.
619 @return A pointer to the allocated buffer or NULL if allocation fails.
624 AllocateReservedCopyPool (
625 IN UINTN AllocationSize
,
626 IN CONST VOID
*Buffer
629 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
633 Frees a buffer that was previously allocated with one of the pool allocation functions in the
634 Memory Allocation Library.
636 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
637 pool allocation services of the Memory Allocation Library.
638 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
641 @param Buffer Pointer to the buffer to free.
651 // PEI phase does not support to free pool, so leave it as NOP.
656 Allocates a buffer of a certain pool type at a specified alignment.
658 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
659 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
660 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
661 to satisfy the request, then NULL is returned.
662 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
664 @param PoolType The type of pool to allocate.
665 @param AllocationSize The number of bytes to allocate.
666 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
667 If Alignment is zero, then byte alignment is used.
669 @return A pointer to the allocated buffer or NULL if allocation fails.
673 InternalAllocateAlignedPool (
674 IN EFI_MEMORY_TYPE PoolType
,
675 IN UINTN AllocationSize
,
680 UINTN AlignedAddress
;
684 // Alignment must be a power of two or zero.
686 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
688 if (Alignment
== 0) {
689 AlignmentMask
= Alignment
;
691 AlignmentMask
= Alignment
- 1;
694 // Make sure that AllocationSize plus AlignmentMask does not overflow.
696 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
698 RawAddress
= InternalAllocatePool (PoolType
, AllocationSize
+ AlignmentMask
);
700 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
702 return (VOID
*) AlignedAddress
;
706 Allocates a buffer of type EfiBootServicesData at a specified alignment.
708 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
709 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
710 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
711 alignment remaining to satisfy the request, then NULL is returned.
712 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
714 @param AllocationSize The number of bytes to allocate.
715 @param Alignment The requested alignment of the allocation. Must be a power of two.
716 If Alignment is zero, then byte alignment is used.
718 @return A pointer to the allocated buffer or NULL if allocation fails.
723 AllocateAlignedPool (
724 IN UINTN AllocationSize
,
729 UINTN AlignedAddress
;
733 // Alignment must be a power of two or zero.
735 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
737 if (Alignment
== 0) {
738 AlignmentMask
= Alignment
;
740 AlignmentMask
= Alignment
- 1;
744 // Make sure that AllocationSize plus AlignmentMask does not overflow.
746 ASSERT (AllocationSize
<= (MAX_ADDRESS
- AlignmentMask
));
748 RawAddress
= AllocatePool (AllocationSize
+ AlignmentMask
);
750 AlignedAddress
= ((UINTN
) RawAddress
+ AlignmentMask
) & ~AlignmentMask
;
752 return (VOID
*) AlignedAddress
;
756 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
758 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
759 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
760 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
761 alignment remaining to satisfy the request, then NULL is returned.
762 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
764 @param AllocationSize The number of bytes to allocate.
765 @param Alignment The requested alignment of the allocation. Must be a power of two.
766 If Alignment is zero, then byte alignment is used.
768 @return A pointer to the allocated buffer or NULL if allocation fails.
773 AllocateAlignedRuntimePool (
774 IN UINTN AllocationSize
,
778 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
782 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
784 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
785 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
786 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
787 alignment remaining to satisfy the request, then NULL is returned.
788 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
790 @param AllocationSize The number of bytes to allocate.
791 @param Alignment The requested alignment of the allocation. Must be a power of two.
792 If Alignment is zero, then byte alignment is used.
794 @return A pointer to the allocated buffer or NULL if allocation fails.
799 AllocateAlignedReservedPool (
800 IN UINTN AllocationSize
,
804 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
808 Allocates and zeros a buffer of a certain pool type at a specified alignment.
810 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
811 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
812 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
813 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
814 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
816 @param PoolType The type of pool to allocate.
817 @param AllocationSize The number of bytes to allocate.
818 @param Alignment The requested alignment of the allocation. Must be a power of two.
819 If Alignment is zero, then byte alignment is used.
821 @return A pointer to the allocated buffer or NULL if allocation fails.
825 InternalAllocateAlignedZeroPool (
826 IN EFI_MEMORY_TYPE PoolType
,
827 IN UINTN AllocationSize
,
833 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
834 if (Memory
!= NULL
) {
835 Memory
= ZeroMem (Memory
, AllocationSize
);
841 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
843 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
844 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
845 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
846 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
848 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
850 @param AllocationSize The number of bytes to allocate.
851 @param Alignment The requested alignment of the allocation. Must be a power of two.
852 If Alignment is zero, then byte alignment is used.
854 @return A pointer to the allocated buffer or NULL if allocation fails.
859 AllocateAlignedZeroPool (
860 IN UINTN AllocationSize
,
866 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
867 if (Memory
!= NULL
) {
868 Memory
= ZeroMem (Memory
, AllocationSize
);
874 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
876 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
877 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
878 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
879 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
881 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
883 @param AllocationSize The number of bytes to allocate.
884 @param Alignment The requested alignment of the allocation. Must be a power of two.
885 If Alignment is zero, then byte alignment is used.
887 @return A pointer to the allocated buffer or NULL if allocation fails.
892 AllocateAlignedRuntimeZeroPool (
893 IN UINTN AllocationSize
,
897 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
901 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
903 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
904 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
905 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
906 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
908 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
910 @param AllocationSize The number of bytes to allocate.
911 @param Alignment The requested alignment of the allocation. Must be a power of two.
912 If Alignment is zero, then byte alignment is used.
914 @return A pointer to the allocated buffer or NULL if allocation fails.
919 AllocateAlignedReservedZeroPool (
920 IN UINTN AllocationSize
,
924 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
928 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
930 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
931 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
932 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
933 to satisfy the request, then NULL is returned.
934 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
936 @param PoolType The type of pool to allocate.
937 @param AllocationSize The number of bytes to allocate.
938 @param Buffer The buffer to copy to the allocated buffer.
939 @param Alignment The requested alignment of the allocation. Must be a power of two.
940 If Alignment is zero, then byte alignment is used.
942 @return A pointer to the allocated buffer or NULL if allocation fails.
946 InternalAllocateAlignedCopyPool (
947 IN EFI_MEMORY_TYPE PoolType
,
948 IN UINTN AllocationSize
,
949 IN CONST VOID
*Buffer
,
955 ASSERT (Buffer
!= NULL
);
956 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
958 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
959 if (Memory
!= NULL
) {
960 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
966 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
968 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
969 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
970 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
971 alignment remaining to satisfy the request, then NULL is returned.
972 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
974 @param AllocationSize The number of bytes to allocate.
975 @param Buffer The buffer to copy to the allocated buffer.
976 @param Alignment The requested alignment of the allocation. Must be a power of two.
977 If Alignment is zero, then byte alignment is used.
979 @return A pointer to the allocated buffer or NULL if allocation fails.
984 AllocateAlignedCopyPool (
985 IN UINTN AllocationSize
,
986 IN CONST VOID
*Buffer
,
992 ASSERT (Buffer
!= NULL
);
993 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
995 Memory
= AllocateAlignedPool (AllocationSize
, Alignment
);
996 if (Memory
!= NULL
) {
997 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
1003 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
1005 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
1006 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1007 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1008 alignment remaining to satisfy the request, then NULL is returned.
1009 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1011 @param AllocationSize The number of bytes to allocate.
1012 @param Buffer The buffer to copy to the allocated buffer.
1013 @param Alignment The requested alignment of the allocation. Must be a power of two.
1014 If Alignment is zero, then byte alignment is used.
1016 @return A pointer to the allocated buffer or NULL if allocation fails.
1021 AllocateAlignedRuntimeCopyPool (
1022 IN UINTN AllocationSize
,
1023 IN CONST VOID
*Buffer
,
1027 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1031 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1033 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1034 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1035 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1036 alignment remaining to satisfy the request, then NULL is returned.
1037 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1039 @param AllocationSize The number of bytes to allocate.
1040 @param Buffer The buffer to copy to the allocated buffer.
1041 @param Alignment The requested alignment of the allocation. Must be a power of two.
1042 If Alignment is zero, then byte alignment is used.
1044 @return A pointer to the allocated buffer or NULL if allocation fails.
1049 AllocateAlignedReservedCopyPool (
1050 IN UINTN AllocationSize
,
1051 IN CONST VOID
*Buffer
,
1055 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1059 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1060 in the Memory Allocation Library.
1062 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1063 aligned pool allocation services of the Memory Allocation Library.
1064 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1065 Library, then ASSERT().
1067 @param Buffer Pointer to the buffer to free.
1077 // PEI phase does not support to free pool, so leave it as NOP.