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/UefiBootServicesTableLib.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
;
60 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
61 if (EFI_ERROR (Status
)) {
64 return (VOID
*) (UINTN
) Memory
;
68 Allocates one or more 4KB pages of type EfiBootServicesData.
70 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
71 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
72 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
75 @param Pages The number of 4 KB pages to allocate.
77 @return A pointer to the allocated buffer or NULL if allocation fails.
86 return InternalAllocatePages (EfiBootServicesData
, Pages
);
90 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
92 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
93 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
94 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
97 @param Pages The number of 4 KB pages to allocate.
99 @return A pointer to the allocated buffer or NULL if allocation fails.
104 AllocateRuntimePages (
108 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
112 Allocates one or more 4KB pages of type EfiReservedMemoryType.
114 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
115 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
116 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
119 @param Pages The number of 4 KB pages to allocate.
121 @return A pointer to the allocated buffer or NULL if allocation fails.
126 AllocateReservedPages (
130 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
134 Frees one or more 4KB pages that were previously allocated with one of the page allocation
135 functions in the Memory Allocation Library.
137 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
138 must have been allocated on a previous call to the page allocation services of the Memory
140 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
142 If Pages is zero, then ASSERT().
144 @param Buffer Pointer to the buffer of pages to free.
145 @param Pages The number of 4 KB pages to free.
158 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
159 ASSERT_EFI_ERROR (Status
);
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
,
187 EFI_PHYSICAL_ADDRESS Memory
;
190 UINTN UnalignedPages
;
194 // Alignment must be a power of two or zero.
196 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
201 if (Alignment
> EFI_PAGE_SIZE
) {
203 // Caculate the total number of pages since alignment is larger than page size.
205 AlignmentMask
= Alignment
- 1;
206 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
208 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
210 ASSERT (RealPages
> Pages
);
212 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
213 if (EFI_ERROR (Status
)) {
216 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
217 UnalignedPages
= EFI_SIZE_TO_PAGES ((UINTN
) Memory
- AlignedMemory
);
218 if (UnalignedPages
> 0) {
220 // Free first unaligned page(s).
222 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
223 ASSERT_EFI_ERROR (Status
);
225 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
226 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
227 if (UnalignedPages
> 0) {
229 // Free last unaligned page(s).
231 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
232 ASSERT_EFI_ERROR (Status
);
236 // Do not over-allocate pages in this case.
238 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
239 if (EFI_ERROR (Status
)) {
242 AlignedMemory
= (UINTN
) Memory
;
244 return (VOID
*) AlignedMemory
;
248 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
250 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
251 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
252 returned. If there is not enough memory at the specified alignment remaining to satisfy the
253 request, then NULL is returned.
254 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
256 @param Pages The number of 4 KB pages to allocate.
257 @param Alignment The requested alignment of the allocation. Must be a power of two.
258 If Alignment is zero, then byte alignment is used.
260 @return A pointer to the allocated buffer or NULL if allocation fails.
265 AllocateAlignedPages (
270 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
274 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
276 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
277 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
278 returned. If there is not enough memory at the specified alignment remaining to satisfy the
279 request, then NULL is returned.
280 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
282 @param Pages The number of 4 KB pages to allocate.
283 @param Alignment The requested alignment of the allocation. Must be a power of two.
284 If Alignment is zero, then byte alignment is used.
286 @return A pointer to the allocated buffer or NULL if allocation fails.
291 AllocateAlignedRuntimePages (
296 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
300 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
302 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
303 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
304 returned. If there is not enough memory at the specified alignment remaining to satisfy the
305 request, then NULL is returned.
306 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
308 @param Pages The number of 4 KB pages to allocate.
309 @param Alignment The requested alignment of the allocation. Must be a power of two.
310 If Alignment is zero, then byte alignment is used.
312 @return A pointer to the allocated buffer or NULL if allocation fails.
317 AllocateAlignedReservedPages (
322 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
326 Frees one or more 4KB pages that were previously allocated with one of the aligned page
327 allocation functions in the Memory Allocation Library.
329 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
330 must have been allocated on a previous call to the aligned page allocation services of the Memory
332 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
333 Library, then ASSERT().
334 If Pages is zero, then ASSERT().
336 @param Buffer Pointer to the buffer of pages to free.
337 @param Pages The number of 4 KB pages to free.
350 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
351 ASSERT_EFI_ERROR (Status
);
355 Allocates a buffer of a certain pool type.
357 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
358 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
359 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
361 @param MemoryType The type of memory to allocate.
362 @param AllocationSize The number of bytes to allocate.
364 @return A pointer to the allocated buffer or NULL if allocation fails.
368 InternalAllocatePool (
369 IN EFI_MEMORY_TYPE MemoryType
,
370 IN UINTN AllocationSize
376 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
377 if (EFI_ERROR (Status
)) {
384 Allocates a buffer of type EfiBootServicesData.
386 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
387 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
388 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
390 @param AllocationSize The number of bytes to allocate.
392 @return A pointer to the allocated buffer or NULL if allocation fails.
398 IN UINTN AllocationSize
401 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
405 Allocates a buffer of type EfiRuntimeServicesData.
407 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
408 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
409 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
411 @param AllocationSize The number of bytes to allocate.
413 @return A pointer to the allocated buffer or NULL if allocation fails.
418 AllocateRuntimePool (
419 IN UINTN AllocationSize
422 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
426 Allocates a buffer of type EfieservedMemoryType.
428 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
429 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
430 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
432 @param AllocationSize The number of bytes to allocate.
434 @return A pointer to the allocated buffer or NULL if allocation fails.
439 AllocateReservedPool (
440 IN UINTN AllocationSize
443 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
447 Allocates and zeros a buffer of a certian pool type.
449 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
450 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
451 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
452 then NULL is returned.
454 @param PoolType The type of memory to allocate.
455 @param AllocationSize The number of bytes to allocate and zero.
457 @return A pointer to the allocated buffer or NULL if allocation fails.
461 InternalAllocateZeroPool (
462 IN EFI_MEMORY_TYPE PoolType
,
463 IN UINTN AllocationSize
468 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
469 if (Memory
!= NULL
) {
470 Memory
= ZeroMem (Memory
, AllocationSize
);
476 Allocates and zeros a buffer of type EfiBootServicesData.
478 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, 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.
491 IN UINTN AllocationSize
494 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
498 Allocates and zeros a buffer of type EfiRuntimeServicesData.
500 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, 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 AllocateRuntimeZeroPool (
513 IN UINTN AllocationSize
516 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
520 Allocates and zeros a buffer of type EfiReservedMemoryType.
522 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
523 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
524 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
525 request, then NULL is returned.
527 @param AllocationSize The number of bytes to allocate and zero.
529 @return A pointer to the allocated buffer or NULL if allocation fails.
534 AllocateReservedZeroPool (
535 IN UINTN AllocationSize
538 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
542 Copies a buffer to an allocated buffer of a certian pool type.
544 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
545 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
546 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
547 is not enough memory remaining to satisfy the request, then NULL is returned.
548 If Buffer is NULL, then ASSERT().
549 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
551 @param PoolType The type of pool to allocate.
552 @param AllocationSize The number of bytes to allocate and zero.
553 @param Buffer The buffer to copy to the allocated buffer.
555 @return A pointer to the allocated buffer or NULL if allocation fails.
559 InternalAllocateCopyPool (
560 IN EFI_MEMORY_TYPE PoolType
,
561 IN UINTN AllocationSize
,
562 IN CONST VOID
*Buffer
567 ASSERT (Buffer
!= NULL
);
568 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
570 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
571 if (Memory
!= NULL
) {
572 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
578 Copies a buffer to an allocated buffer of type EfiBootServicesData.
580 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
581 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
582 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
583 is not enough memory remaining to satisfy the request, then NULL is returned.
584 If Buffer is NULL, then ASSERT().
585 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
587 @param AllocationSize The number of bytes to allocate and zero.
588 @param Buffer The buffer to copy to the allocated buffer.
590 @return A pointer to the allocated buffer or NULL if allocation fails.
596 IN UINTN AllocationSize
,
597 IN CONST VOID
*Buffer
600 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
604 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
606 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
607 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
608 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
609 is not enough memory remaining to satisfy the request, then NULL is returned.
610 If Buffer is NULL, then ASSERT().
611 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
613 @param AllocationSize The number of bytes to allocate and zero.
614 @param Buffer The buffer to copy to the allocated buffer.
616 @return A pointer to the allocated buffer or NULL if allocation fails.
621 AllocateRuntimeCopyPool (
622 IN UINTN AllocationSize
,
623 IN CONST VOID
*Buffer
626 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
630 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
632 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
633 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
634 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
635 is not enough memory remaining to satisfy the request, then NULL is returned.
636 If Buffer is NULL, then ASSERT().
637 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
639 @param AllocationSize The number of bytes to allocate and zero.
640 @param Buffer The buffer to copy to the allocated buffer.
642 @return A pointer to the allocated buffer or NULL if allocation fails.
647 AllocateReservedCopyPool (
648 IN UINTN AllocationSize
,
649 IN CONST VOID
*Buffer
652 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
656 Frees a buffer that was previously allocated with one of the pool allocation functions in the
657 Memory Allocation Library.
659 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
660 pool allocation services of the Memory Allocation Library.
661 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
664 @param Buffer Pointer to the buffer to free.
675 Status
= gBS
->FreePool (Buffer
);
676 ASSERT_EFI_ERROR (Status
);
680 Allocates a buffer of a certain pool type at a specified alignment.
682 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
683 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
684 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
685 to satisfy the request, then NULL is returned.
686 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
688 @param PoolType The type of pool to allocate.
689 @param AllocationSize The number of bytes to allocate.
690 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
691 If Alignment is zero, then byte alignment is used.
693 @return A pointer to the allocated buffer or NULL if allocation fails.
697 InternalAllocateAlignedPool (
698 IN EFI_MEMORY_TYPE PoolType
,
699 IN UINTN AllocationSize
,
704 UINTN AlignedAddress
;
706 UINTN OverAllocationSize
;
707 UINTN RealAllocationSize
;
711 // Alignment must be a power of two or zero.
713 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
715 if (Alignment
== 0) {
716 AlignmentMask
= Alignment
;
718 AlignmentMask
= Alignment
- 1;
721 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
723 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
724 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
726 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
728 ASSERT (RealAllocationSize
> AllocationSize
);
730 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
731 if (RawAddress
== NULL
) {
734 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
736 // Save the original memory address just before the aligned address.
738 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
739 *FreePointer
= RawAddress
;
741 return (VOID
*) AlignedAddress
;
745 Allocates a buffer of type EfiBootServicesData at a specified alignment.
747 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
748 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
749 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
750 alignment remaining to satisfy the request, then NULL is returned.
751 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
753 @param AllocationSize The number of bytes to allocate.
754 @param Alignment The requested alignment of the allocation. Must be a power of two.
755 If Alignment is zero, then byte alignment is used.
757 @return A pointer to the allocated buffer or NULL if allocation fails.
762 AllocateAlignedPool (
763 IN UINTN AllocationSize
,
767 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
771 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
773 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
774 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
775 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
776 alignment remaining to satisfy the request, then NULL is returned.
777 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
779 @param AllocationSize The number of bytes to allocate.
780 @param Alignment The requested alignment of the allocation. Must be a power of two.
781 If Alignment is zero, then byte alignment is used.
783 @return A pointer to the allocated buffer or NULL if allocation fails.
788 AllocateAlignedRuntimePool (
789 IN UINTN AllocationSize
,
793 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
797 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
799 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
800 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
801 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
802 alignment remaining to satisfy the request, then NULL is returned.
803 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
805 @param AllocationSize The number of bytes to allocate.
806 @param Alignment The requested alignment of the allocation. Must be a power of two.
807 If Alignment is zero, then byte alignment is used.
809 @return A pointer to the allocated buffer or NULL if allocation fails.
814 AllocateAlignedReservedPool (
815 IN UINTN AllocationSize
,
819 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
823 Allocates and zeros a buffer of a certain pool type at a specified alignment.
825 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
826 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
827 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
828 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
829 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
831 @param PoolType The type of pool to allocate.
832 @param AllocationSize The number of bytes to allocate.
833 @param Alignment The requested alignment of the allocation. Must be a power of two.
834 If Alignment is zero, then byte alignment is used.
836 @return A pointer to the allocated buffer or NULL if allocation fails.
840 InternalAllocateAlignedZeroPool (
841 IN EFI_MEMORY_TYPE PoolType
,
842 IN UINTN AllocationSize
,
847 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
848 if (Memory
!= NULL
) {
849 Memory
= ZeroMem (Memory
, AllocationSize
);
855 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
857 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
858 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
859 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
860 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
862 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
864 @param AllocationSize The number of bytes to allocate.
865 @param Alignment The requested alignment of the allocation. Must be a power of two.
866 If Alignment is zero, then byte alignment is used.
868 @return A pointer to the allocated buffer or NULL if allocation fails.
873 AllocateAlignedZeroPool (
874 IN UINTN AllocationSize
,
878 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
882 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
884 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
885 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
886 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
887 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
889 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
891 @param AllocationSize The number of bytes to allocate.
892 @param Alignment The requested alignment of the allocation. Must be a power of two.
893 If Alignment is zero, then byte alignment is used.
895 @return A pointer to the allocated buffer or NULL if allocation fails.
900 AllocateAlignedRuntimeZeroPool (
901 IN UINTN AllocationSize
,
905 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
909 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
911 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
912 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
913 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
914 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
916 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
918 @param AllocationSize The number of bytes to allocate.
919 @param Alignment The requested alignment of the allocation. Must be a power of two.
920 If Alignment is zero, then byte alignment is used.
922 @return A pointer to the allocated buffer or NULL if allocation fails.
927 AllocateAlignedReservedZeroPool (
928 IN UINTN AllocationSize
,
932 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
936 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
938 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
939 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
940 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
941 to satisfy the request, then NULL is returned.
942 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
944 @param PoolType The type of pool to allocate.
945 @param AllocationSize The number of bytes to allocate.
946 @param Buffer The buffer to copy to the allocated buffer.
947 @param Alignment The requested alignment of the allocation. Must be a power of two.
948 If Alignment is zero, then byte alignment is used.
950 @return A pointer to the allocated buffer or NULL if allocation fails.
954 InternalAllocateAlignedCopyPool (
955 IN EFI_MEMORY_TYPE PoolType
,
956 IN UINTN AllocationSize
,
957 IN CONST VOID
*Buffer
,
963 ASSERT (Buffer
!= NULL
);
964 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
966 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
967 if (Memory
!= NULL
) {
968 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
974 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
976 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
977 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
978 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
979 alignment remaining to satisfy the request, then NULL is returned.
980 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
982 @param AllocationSize The number of bytes to allocate.
983 @param Buffer The buffer to copy to the allocated buffer.
984 @param Alignment The requested alignment of the allocation. Must be a power of two.
985 If Alignment is zero, then byte alignment is used.
987 @return A pointer to the allocated buffer or NULL if allocation fails.
992 AllocateAlignedCopyPool (
993 IN UINTN AllocationSize
,
994 IN CONST VOID
*Buffer
,
998 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
1002 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
1004 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
1005 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1006 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1007 alignment remaining to satisfy the request, then NULL is returned.
1008 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1010 @param AllocationSize The number of bytes to allocate.
1011 @param Buffer The buffer to copy to the allocated buffer.
1012 @param Alignment The requested alignment of the allocation. Must be a power of two.
1013 If Alignment is zero, then byte alignment is used.
1015 @return A pointer to the allocated buffer or NULL if allocation fails.
1020 AllocateAlignedRuntimeCopyPool (
1021 IN UINTN AllocationSize
,
1022 IN CONST VOID
*Buffer
,
1026 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1030 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1032 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1033 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1034 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1035 alignment remaining to satisfy the request, then NULL is returned.
1036 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1038 @param AllocationSize The number of bytes to allocate.
1039 @param Buffer The buffer to copy to the allocated buffer.
1040 @param Alignment The requested alignment of the allocation. Must be a power of two.
1041 If Alignment is zero, then byte alignment is used.
1043 @return A pointer to the allocated buffer or NULL if allocation fails.
1048 AllocateAlignedReservedCopyPool (
1049 IN UINTN AllocationSize
,
1050 IN CONST VOID
*Buffer
,
1054 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1058 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1059 in the Memory Allocation Library.
1061 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1062 aligned pool allocation services of the Memory Allocation Library.
1063 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1064 Library, then ASSERT().
1066 @param Buffer Pointer to the buffer to free.
1080 // Get the pre-saved original address in the over-allocate pool.
1082 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1083 RawAddress
= *FreePointer
;
1085 Status
= gBS
->FreePool (RawAddress
);
1086 ASSERT_EFI_ERROR (Status
);