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
;
50 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
51 if (EFI_ERROR (Status
)) {
54 return (VOID
*) (UINTN
) Memory
;
58 Allocates one or more 4KB pages of type EfiBootServicesData.
60 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
61 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
62 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
65 @param Pages The number of 4 KB pages to allocate.
67 @return A pointer to the allocated buffer or NULL if allocation fails.
76 return InternalAllocatePages (EfiBootServicesData
, Pages
);
80 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
82 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
83 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
84 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
87 @param Pages The number of 4 KB pages to allocate.
89 @return A pointer to the allocated buffer or NULL if allocation fails.
94 AllocateRuntimePages (
98 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
102 Allocates one or more 4KB pages of type EfiReservedMemoryType.
104 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
105 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
106 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
109 @param Pages The number of 4 KB pages to allocate.
111 @return A pointer to the allocated buffer or NULL if allocation fails.
116 AllocateReservedPages (
120 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
124 Frees one or more 4KB pages that were previously allocated with one of the page allocation
125 functions in the Memory Allocation Library.
127 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
128 must have been allocated on a previous call to the page allocation services of the Memory
130 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
132 If Pages is zero, then ASSERT().
134 @param Buffer Pointer to the buffer of pages to free.
135 @param Pages The number of 4 KB pages to free.
148 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
149 ASSERT_EFI_ERROR (Status
);
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
,
177 EFI_PHYSICAL_ADDRESS Memory
;
180 UINTN UnalignedPages
;
184 // Alignment must be a power of two or zero.
186 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
191 if (Alignment
> EFI_PAGE_SIZE
) {
193 // Caculate the total number of pages since alignment is larger than page size.
195 AlignmentMask
= Alignment
- 1;
196 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
198 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
200 ASSERT (RealPages
> Pages
);
202 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
203 if (EFI_ERROR (Status
)) {
206 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
207 UnalignedPages
= EFI_SIZE_TO_PAGES ((UINTN
) Memory
- AlignedMemory
);
208 if (UnalignedPages
> 0) {
210 // Free first unaligned page(s).
212 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
213 ASSERT_EFI_ERROR (Status
);
215 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
216 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
217 if (UnalignedPages
> 0) {
219 // Free last unaligned page(s).
221 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
222 ASSERT_EFI_ERROR (Status
);
226 // Do not over-allocate pages in this case.
228 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
229 if (EFI_ERROR (Status
)) {
232 AlignedMemory
= (UINTN
) Memory
;
234 return (VOID
*) AlignedMemory
;
238 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
240 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
241 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
242 returned. If there is not enough memory at the specified alignment remaining to satisfy the
243 request, then NULL is returned.
244 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
246 @param Pages The number of 4 KB pages to allocate.
247 @param Alignment The requested alignment of the allocation. Must be a power of two.
248 If Alignment is zero, then byte alignment is used.
250 @return A pointer to the allocated buffer or NULL if allocation fails.
255 AllocateAlignedPages (
260 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
264 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
266 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
267 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
268 returned. If there is not enough memory at the specified alignment remaining to satisfy the
269 request, then NULL is returned.
270 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
272 @param Pages The number of 4 KB pages to allocate.
273 @param Alignment The requested alignment of the allocation. Must be a power of two.
274 If Alignment is zero, then byte alignment is used.
276 @return A pointer to the allocated buffer or NULL if allocation fails.
281 AllocateAlignedRuntimePages (
286 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
290 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
292 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
293 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
294 returned. If there is not enough memory at the specified alignment remaining to satisfy the
295 request, then NULL is returned.
296 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
298 @param Pages The number of 4 KB pages to allocate.
299 @param Alignment The requested alignment of the allocation. Must be a power of two.
300 If Alignment is zero, then byte alignment is used.
302 @return A pointer to the allocated buffer or NULL if allocation fails.
307 AllocateAlignedReservedPages (
312 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
316 Frees one or more 4KB pages that were previously allocated with one of the aligned page
317 allocation functions in the Memory Allocation Library.
319 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
320 must have been allocated on a previous call to the aligned page allocation services of the Memory
322 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
323 Library, then ASSERT().
324 If Pages is zero, then ASSERT().
326 @param Buffer Pointer to the buffer of pages to free.
327 @param Pages The number of 4 KB pages to free.
340 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
341 ASSERT_EFI_ERROR (Status
);
345 Allocates a buffer of a certain pool type.
347 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
348 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
349 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
351 @param MemoryType The type of memory to allocate.
352 @param AllocationSize The number of bytes to allocate.
354 @return A pointer to the allocated buffer or NULL if allocation fails.
358 InternalAllocatePool (
359 IN EFI_MEMORY_TYPE MemoryType
,
360 IN UINTN AllocationSize
366 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
367 if (EFI_ERROR (Status
)) {
374 Allocates a buffer of type EfiBootServicesData.
376 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
377 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
378 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
380 @param AllocationSize The number of bytes to allocate.
382 @return A pointer to the allocated buffer or NULL if allocation fails.
388 IN UINTN AllocationSize
391 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
395 Allocates a buffer of type EfiRuntimeServicesData.
397 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
398 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
399 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
401 @param AllocationSize The number of bytes to allocate.
403 @return A pointer to the allocated buffer or NULL if allocation fails.
408 AllocateRuntimePool (
409 IN UINTN AllocationSize
412 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
416 Allocates a buffer of type EfieservedMemoryType.
418 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
419 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
420 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
422 @param AllocationSize The number of bytes to allocate.
424 @return A pointer to the allocated buffer or NULL if allocation fails.
429 AllocateReservedPool (
430 IN UINTN AllocationSize
433 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
437 Allocates and zeros a buffer of a certian pool type.
439 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
440 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
441 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
442 then NULL is returned.
444 @param PoolType The type of memory to allocate.
445 @param AllocationSize The number of bytes to allocate and zero.
447 @return A pointer to the allocated buffer or NULL if allocation fails.
451 InternalAllocateZeroPool (
452 IN EFI_MEMORY_TYPE PoolType
,
453 IN UINTN AllocationSize
458 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
459 if (Memory
!= NULL
) {
460 Memory
= ZeroMem (Memory
, AllocationSize
);
466 Allocates and zeros a buffer of type EfiBootServicesData.
468 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, 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.
481 IN UINTN AllocationSize
484 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
488 Allocates and zeros a buffer of type EfiRuntimeServicesData.
490 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, 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 AllocateRuntimeZeroPool (
503 IN UINTN AllocationSize
506 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
510 Allocates and zeros a buffer of type EfiReservedMemoryType.
512 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
513 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
514 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
515 request, then NULL is returned.
517 @param AllocationSize The number of bytes to allocate and zero.
519 @return A pointer to the allocated buffer or NULL if allocation fails.
524 AllocateReservedZeroPool (
525 IN UINTN AllocationSize
528 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
532 Copies a buffer to an allocated buffer of a certian pool type.
534 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
535 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
536 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
537 is not enough memory remaining to satisfy the request, then NULL is returned.
538 If Buffer is NULL, then ASSERT().
539 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
541 @param PoolType The type of pool to allocate.
542 @param AllocationSize The number of bytes to allocate and zero.
543 @param Buffer The buffer to copy to the allocated buffer.
545 @return A pointer to the allocated buffer or NULL if allocation fails.
549 InternalAllocateCopyPool (
550 IN EFI_MEMORY_TYPE PoolType
,
551 IN UINTN AllocationSize
,
552 IN CONST VOID
*Buffer
557 ASSERT (Buffer
!= NULL
);
558 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
560 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
561 if (Memory
!= NULL
) {
562 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
568 Copies a buffer to an allocated buffer of type EfiBootServicesData.
570 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
571 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
572 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
573 is not enough memory remaining to satisfy the request, then NULL is returned.
574 If Buffer is NULL, then ASSERT().
575 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
577 @param AllocationSize The number of bytes to allocate and zero.
578 @param Buffer The buffer to copy to the allocated buffer.
580 @return A pointer to the allocated buffer or NULL if allocation fails.
586 IN UINTN AllocationSize
,
587 IN CONST VOID
*Buffer
590 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
594 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
596 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
597 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
598 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
599 is not enough memory remaining to satisfy the request, then NULL is returned.
600 If Buffer is NULL, then ASSERT().
601 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
603 @param AllocationSize The number of bytes to allocate and zero.
604 @param Buffer The buffer to copy to the allocated buffer.
606 @return A pointer to the allocated buffer or NULL if allocation fails.
611 AllocateRuntimeCopyPool (
612 IN UINTN AllocationSize
,
613 IN CONST VOID
*Buffer
616 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
620 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
622 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
623 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
624 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
625 is not enough memory remaining to satisfy the request, then NULL is returned.
626 If Buffer is NULL, then ASSERT().
627 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
629 @param AllocationSize The number of bytes to allocate and zero.
630 @param Buffer The buffer to copy to the allocated buffer.
632 @return A pointer to the allocated buffer or NULL if allocation fails.
637 AllocateReservedCopyPool (
638 IN UINTN AllocationSize
,
639 IN CONST VOID
*Buffer
642 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
646 Frees a buffer that was previously allocated with one of the pool allocation functions in the
647 Memory Allocation Library.
649 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
650 pool allocation services of the Memory Allocation Library.
651 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
654 @param Buffer Pointer to the buffer to free.
665 Status
= gBS
->FreePool (Buffer
);
666 ASSERT_EFI_ERROR (Status
);
670 Allocates a buffer of a certain pool type at a specified alignment.
672 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
673 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
674 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
675 to satisfy the request, then NULL is returned.
676 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
678 @param PoolType The type of pool to allocate.
679 @param AllocationSize The number of bytes to allocate.
680 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
681 If Alignment is zero, then byte alignment is used.
683 @return A pointer to the allocated buffer or NULL if allocation fails.
687 InternalAllocateAlignedPool (
688 IN EFI_MEMORY_TYPE PoolType
,
689 IN UINTN AllocationSize
,
694 UINTN AlignedAddress
;
696 UINTN OverAllocationSize
;
697 UINTN RealAllocationSize
;
701 // Alignment must be a power of two or zero.
703 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
705 if (Alignment
== 0) {
706 AlignmentMask
= Alignment
;
708 AlignmentMask
= Alignment
- 1;
711 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
713 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
714 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
716 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
718 ASSERT (RealAllocationSize
> AllocationSize
);
720 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
721 if (RawAddress
== NULL
) {
724 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
726 // Save the original memory address just before the aligned address.
728 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
729 *FreePointer
= RawAddress
;
731 return (VOID
*) AlignedAddress
;
735 Allocates a buffer of type EfiBootServicesData at a specified alignment.
737 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
738 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
739 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
740 alignment remaining to satisfy the request, then NULL is returned.
741 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
743 @param AllocationSize The number of bytes to allocate.
744 @param Alignment The requested alignment of the allocation. Must be a power of two.
745 If Alignment is zero, then byte alignment is used.
747 @return A pointer to the allocated buffer or NULL if allocation fails.
752 AllocateAlignedPool (
753 IN UINTN AllocationSize
,
757 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
761 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
763 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
764 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
765 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
766 alignment remaining to satisfy the request, then NULL is returned.
767 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
769 @param AllocationSize The number of bytes to allocate.
770 @param Alignment The requested alignment of the allocation. Must be a power of two.
771 If Alignment is zero, then byte alignment is used.
773 @return A pointer to the allocated buffer or NULL if allocation fails.
778 AllocateAlignedRuntimePool (
779 IN UINTN AllocationSize
,
783 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
787 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
789 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
790 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
791 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
792 alignment remaining to satisfy the request, then NULL is returned.
793 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
795 @param AllocationSize The number of bytes to allocate.
796 @param Alignment The requested alignment of the allocation. Must be a power of two.
797 If Alignment is zero, then byte alignment is used.
799 @return A pointer to the allocated buffer or NULL if allocation fails.
804 AllocateAlignedReservedPool (
805 IN UINTN AllocationSize
,
809 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
813 Allocates and zeros a buffer of a certain pool type at a specified alignment.
815 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
816 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
817 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
818 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
819 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
821 @param PoolType The type of pool to allocate.
822 @param AllocationSize The number of bytes to allocate.
823 @param Alignment The requested alignment of the allocation. Must be a power of two.
824 If Alignment is zero, then byte alignment is used.
826 @return A pointer to the allocated buffer or NULL if allocation fails.
830 InternalAllocateAlignedZeroPool (
831 IN EFI_MEMORY_TYPE PoolType
,
832 IN UINTN AllocationSize
,
837 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
838 if (Memory
!= NULL
) {
839 Memory
= ZeroMem (Memory
, AllocationSize
);
845 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
847 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
848 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
849 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
850 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
852 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
854 @param AllocationSize The number of bytes to allocate.
855 @param Alignment The requested alignment of the allocation. Must be a power of two.
856 If Alignment is zero, then byte alignment is used.
858 @return A pointer to the allocated buffer or NULL if allocation fails.
863 AllocateAlignedZeroPool (
864 IN UINTN AllocationSize
,
868 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
872 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
874 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
875 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
876 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
877 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
879 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
881 @param AllocationSize The number of bytes to allocate.
882 @param Alignment The requested alignment of the allocation. Must be a power of two.
883 If Alignment is zero, then byte alignment is used.
885 @return A pointer to the allocated buffer or NULL if allocation fails.
890 AllocateAlignedRuntimeZeroPool (
891 IN UINTN AllocationSize
,
895 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
899 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
901 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
902 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
903 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
904 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
906 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
908 @param AllocationSize The number of bytes to allocate.
909 @param Alignment The requested alignment of the allocation. Must be a power of two.
910 If Alignment is zero, then byte alignment is used.
912 @return A pointer to the allocated buffer or NULL if allocation fails.
917 AllocateAlignedReservedZeroPool (
918 IN UINTN AllocationSize
,
922 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
926 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
928 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
929 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
930 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
931 to satisfy the request, then NULL is returned.
932 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
934 @param PoolType The type of pool to allocate.
935 @param AllocationSize The number of bytes to allocate.
936 @param Buffer The buffer to copy to the allocated buffer.
937 @param Alignment The requested alignment of the allocation. Must be a power of two.
938 If Alignment is zero, then byte alignment is used.
940 @return A pointer to the allocated buffer or NULL if allocation fails.
944 InternalAllocateAlignedCopyPool (
945 IN EFI_MEMORY_TYPE PoolType
,
946 IN UINTN AllocationSize
,
947 IN CONST VOID
*Buffer
,
953 ASSERT (Buffer
!= NULL
);
954 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
956 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
957 if (Memory
!= NULL
) {
958 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
964 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
966 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
967 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
968 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
969 alignment remaining to satisfy the request, then NULL is returned.
970 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
972 @param AllocationSize The number of bytes to allocate.
973 @param Buffer The buffer to copy to the allocated buffer.
974 @param Alignment The requested alignment of the allocation. Must be a power of two.
975 If Alignment is zero, then byte alignment is used.
977 @return A pointer to the allocated buffer or NULL if allocation fails.
982 AllocateAlignedCopyPool (
983 IN UINTN AllocationSize
,
984 IN CONST VOID
*Buffer
,
988 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
992 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
994 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
995 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
996 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
997 alignment remaining to satisfy the request, then NULL is returned.
998 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1000 @param AllocationSize The number of bytes to allocate.
1001 @param Buffer The buffer to copy to the allocated buffer.
1002 @param Alignment The requested alignment of the allocation. Must be a power of two.
1003 If Alignment is zero, then byte alignment is used.
1005 @return A pointer to the allocated buffer or NULL if allocation fails.
1010 AllocateAlignedRuntimeCopyPool (
1011 IN UINTN AllocationSize
,
1012 IN CONST VOID
*Buffer
,
1016 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1020 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1022 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1023 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1024 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1025 alignment remaining to satisfy the request, then NULL is returned.
1026 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1028 @param AllocationSize The number of bytes to allocate.
1029 @param Buffer The buffer to copy to the allocated buffer.
1030 @param Alignment The requested alignment of the allocation. Must be a power of two.
1031 If Alignment is zero, then byte alignment is used.
1033 @return A pointer to the allocated buffer or NULL if allocation fails.
1038 AllocateAlignedReservedCopyPool (
1039 IN UINTN AllocationSize
,
1040 IN CONST VOID
*Buffer
,
1044 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1048 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1049 in the Memory Allocation Library.
1051 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1052 aligned pool allocation services of the Memory Allocation Library.
1053 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1054 Library, then ASSERT().
1056 @param Buffer Pointer to the buffer to free.
1070 // Get the pre-saved original address in the over-allocate pool.
1072 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1073 RawAddress
= *FreePointer
;
1075 Status
= gBS
->FreePool (RawAddress
);
1076 ASSERT_EFI_ERROR (Status
);