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.
16 // The package level header files this module uses
20 // The protocols, PPI and GUID defintions for this module
23 // The Library classes this module consumes
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include <Library/BaseMemoryLib.h>
28 #include <Library/DebugLib.h>
30 #include "MemoryAllocationLibInternals.h"
33 Allocates one or more 4KB pages of a certain memory type.
35 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
36 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
37 If there is not enough memory remaining to satisfy the request, then NULL is returned.
39 @param MemoryType The type of memory to allocate.
40 @param Pages The number of 4 KB pages to allocate.
42 @return A pointer to the allocated buffer or NULL if allocation fails.
46 InternalAllocatePages (
47 IN EFI_MEMORY_TYPE MemoryType
,
52 EFI_PHYSICAL_ADDRESS Memory
;
58 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
59 if (EFI_ERROR (Status
)) {
62 return (VOID
*) (UINTN
) Memory
;
66 Allocates one or more 4KB pages of type EfiBootServicesData.
68 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
69 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
70 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
73 @param Pages The number of 4 KB pages to allocate.
75 @return A pointer to the allocated buffer or NULL if allocation fails.
84 return InternalAllocatePages (EfiBootServicesData
, Pages
);
88 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
90 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
91 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
92 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
95 @param Pages The number of 4 KB pages to allocate.
97 @return A pointer to the allocated buffer or NULL if allocation fails.
102 AllocateRuntimePages (
106 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
110 Allocates one or more 4KB pages of type EfiReservedMemoryType.
112 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
113 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
114 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
117 @param Pages The number of 4 KB pages to allocate.
119 @return A pointer to the allocated buffer or NULL if allocation fails.
124 AllocateReservedPages (
128 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
132 Frees one or more 4KB pages that were previously allocated with one of the page allocation
133 functions in the Memory Allocation Library.
135 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
136 must have been allocated on a previous call to the page allocation services of the Memory
138 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
140 If Pages is zero, then ASSERT().
142 @param Buffer Pointer to the buffer of pages to free.
143 @param Pages The number of 4 KB pages to free.
156 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
157 ASSERT_EFI_ERROR (Status
);
161 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
163 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
164 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
165 If there is not enough memory at the specified alignment remaining to satisfy the request, then
167 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
169 @param MemoryType The type of memory to allocate.
170 @param Pages The number of 4 KB pages to allocate.
171 @param Alignment The requested alignment of the allocation. Must be a power of two.
172 If Alignment is zero, then byte alignment is used.
174 @return A pointer to the allocated buffer or NULL if allocation fails.
178 InternalAllocateAlignedPages (
179 IN EFI_MEMORY_TYPE MemoryType
,
185 EFI_PHYSICAL_ADDRESS Memory
;
188 UINTN UnalignedPages
;
192 // Alignment must be a power of two or zero.
194 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
199 if (Alignment
> EFI_PAGE_SIZE
) {
201 // Caculate the total number of pages since alignment is larger than page size.
203 AlignmentMask
= Alignment
- 1;
204 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
206 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
208 ASSERT (RealPages
> Pages
);
210 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
211 if (EFI_ERROR (Status
)) {
214 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
215 UnalignedPages
= EFI_SIZE_TO_PAGES ((UINTN
) Memory
- AlignedMemory
);
216 if (UnalignedPages
> 0) {
218 // Free first unaligned page(s).
220 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
221 ASSERT_EFI_ERROR (Status
);
223 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
224 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
225 if (UnalignedPages
> 0) {
227 // Free last unaligned page(s).
229 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
230 ASSERT_EFI_ERROR (Status
);
234 // Do not over-allocate pages in this case.
236 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
237 if (EFI_ERROR (Status
)) {
240 AlignedMemory
= (UINTN
) Memory
;
242 return (VOID
*) AlignedMemory
;
246 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
248 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
249 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
250 returned. If there is not enough memory at the specified alignment remaining to satisfy the
251 request, then NULL is returned.
252 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
254 @param Pages The number of 4 KB pages to allocate.
255 @param Alignment The requested alignment of the allocation. Must be a power of two.
256 If Alignment is zero, then byte alignment is used.
258 @return A pointer to the allocated buffer or NULL if allocation fails.
263 AllocateAlignedPages (
268 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
272 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
274 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
275 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
276 returned. If there is not enough memory at the specified alignment remaining to satisfy the
277 request, then NULL is returned.
278 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
280 @param Pages The number of 4 KB pages to allocate.
281 @param Alignment The requested alignment of the allocation. Must be a power of two.
282 If Alignment is zero, then byte alignment is used.
284 @return A pointer to the allocated buffer or NULL if allocation fails.
289 AllocateAlignedRuntimePages (
294 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
298 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
300 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
301 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
302 returned. If there is not enough memory at the specified alignment remaining to satisfy the
303 request, then NULL is returned.
304 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
306 @param Pages The number of 4 KB pages to allocate.
307 @param Alignment The requested alignment of the allocation. Must be a power of two.
308 If Alignment is zero, then byte alignment is used.
310 @return A pointer to the allocated buffer or NULL if allocation fails.
315 AllocateAlignedReservedPages (
320 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
324 Frees one or more 4KB pages that were previously allocated with one of the aligned page
325 allocation functions in the Memory Allocation Library.
327 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
328 must have been allocated on a previous call to the aligned page allocation services of the Memory
330 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
331 Library, then ASSERT().
332 If Pages is zero, then ASSERT().
334 @param Buffer Pointer to the buffer of pages to free.
335 @param Pages The number of 4 KB pages to free.
348 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
349 ASSERT_EFI_ERROR (Status
);
353 Allocates a buffer of a certain pool type.
355 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
356 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
357 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
359 @param MemoryType The type of memory to allocate.
360 @param AllocationSize The number of bytes to allocate.
362 @return A pointer to the allocated buffer or NULL if allocation fails.
366 InternalAllocatePool (
367 IN EFI_MEMORY_TYPE MemoryType
,
368 IN UINTN AllocationSize
374 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
375 if (EFI_ERROR (Status
)) {
382 Allocates a buffer of type EfiBootServicesData.
384 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
385 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
386 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
388 @param AllocationSize The number of bytes to allocate.
390 @return A pointer to the allocated buffer or NULL if allocation fails.
396 IN UINTN AllocationSize
399 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
403 Allocates a buffer of type EfiRuntimeServicesData.
405 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
406 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
407 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
409 @param AllocationSize The number of bytes to allocate.
411 @return A pointer to the allocated buffer or NULL if allocation fails.
416 AllocateRuntimePool (
417 IN UINTN AllocationSize
420 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
424 Allocates a buffer of type EfieservedMemoryType.
426 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
427 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
428 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
430 @param AllocationSize The number of bytes to allocate.
432 @return A pointer to the allocated buffer or NULL if allocation fails.
437 AllocateReservedPool (
438 IN UINTN AllocationSize
441 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
445 Allocates and zeros a buffer of a certian pool type.
447 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
448 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
449 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
450 then NULL is returned.
452 @param PoolType The type of memory to allocate.
453 @param AllocationSize The number of bytes to allocate and zero.
455 @return A pointer to the allocated buffer or NULL if allocation fails.
459 InternalAllocateZeroPool (
460 IN EFI_MEMORY_TYPE PoolType
,
461 IN UINTN AllocationSize
466 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
467 if (Memory
!= NULL
) {
468 Memory
= ZeroMem (Memory
, AllocationSize
);
474 Allocates and zeros a buffer of type EfiBootServicesData.
476 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
477 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
478 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
479 request, then NULL is returned.
481 @param AllocationSize The number of bytes to allocate and zero.
483 @return A pointer to the allocated buffer or NULL if allocation fails.
489 IN UINTN AllocationSize
492 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
496 Allocates and zeros a buffer of type EfiRuntimeServicesData.
498 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
499 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
500 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
501 request, then NULL is returned.
503 @param AllocationSize The number of bytes to allocate and zero.
505 @return A pointer to the allocated buffer or NULL if allocation fails.
510 AllocateRuntimeZeroPool (
511 IN UINTN AllocationSize
514 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
518 Allocates and zeros a buffer of type EfiReservedMemoryType.
520 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
521 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
522 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
523 request, then NULL is returned.
525 @param AllocationSize The number of bytes to allocate and zero.
527 @return A pointer to the allocated buffer or NULL if allocation fails.
532 AllocateReservedZeroPool (
533 IN UINTN AllocationSize
536 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
540 Copies a buffer to an allocated buffer of a certian pool type.
542 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
543 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
544 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
545 is not enough memory remaining to satisfy the request, then NULL is returned.
546 If Buffer is NULL, then ASSERT().
547 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
549 @param PoolType The type of pool to allocate.
550 @param AllocationSize The number of bytes to allocate and zero.
551 @param Buffer The buffer to copy to the allocated buffer.
553 @return A pointer to the allocated buffer or NULL if allocation fails.
557 InternalAllocateCopyPool (
558 IN EFI_MEMORY_TYPE PoolType
,
559 IN UINTN AllocationSize
,
560 IN CONST VOID
*Buffer
565 ASSERT (Buffer
!= NULL
);
566 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
568 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
569 if (Memory
!= NULL
) {
570 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
576 Copies a buffer to an allocated buffer of type EfiBootServicesData.
578 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
579 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
580 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
581 is not enough memory remaining to satisfy the request, then NULL is returned.
582 If Buffer is NULL, then ASSERT().
583 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
585 @param AllocationSize The number of bytes to allocate and zero.
586 @param Buffer The buffer to copy to the allocated buffer.
588 @return A pointer to the allocated buffer or NULL if allocation fails.
594 IN UINTN AllocationSize
,
595 IN CONST VOID
*Buffer
598 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
602 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
604 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
605 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
606 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
607 is not enough memory remaining to satisfy the request, then NULL is returned.
608 If Buffer is NULL, then ASSERT().
609 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
611 @param AllocationSize The number of bytes to allocate and zero.
612 @param Buffer The buffer to copy to the allocated buffer.
614 @return A pointer to the allocated buffer or NULL if allocation fails.
619 AllocateRuntimeCopyPool (
620 IN UINTN AllocationSize
,
621 IN CONST VOID
*Buffer
624 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
628 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
630 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
631 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
632 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
633 is not enough memory remaining to satisfy the request, then NULL is returned.
634 If Buffer is NULL, then ASSERT().
635 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
637 @param AllocationSize The number of bytes to allocate and zero.
638 @param Buffer The buffer to copy to the allocated buffer.
640 @return A pointer to the allocated buffer or NULL if allocation fails.
645 AllocateReservedCopyPool (
646 IN UINTN AllocationSize
,
647 IN CONST VOID
*Buffer
650 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
654 Frees a buffer that was previously allocated with one of the pool allocation functions in the
655 Memory Allocation Library.
657 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
658 pool allocation services of the Memory Allocation Library.
659 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
662 @param Buffer Pointer to the buffer to free.
673 Status
= gBS
->FreePool (Buffer
);
674 ASSERT_EFI_ERROR (Status
);
678 Allocates a buffer of a certain pool type at a specified alignment.
680 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
681 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
682 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
683 to satisfy the request, then NULL is returned.
684 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
686 @param PoolType The type of pool to allocate.
687 @param AllocationSize The number of bytes to allocate.
688 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
689 If Alignment is zero, then byte alignment is used.
691 @return A pointer to the allocated buffer or NULL if allocation fails.
695 InternalAllocateAlignedPool (
696 IN EFI_MEMORY_TYPE PoolType
,
697 IN UINTN AllocationSize
,
702 UINTN AlignedAddress
;
704 UINTN OverAllocationSize
;
705 UINTN RealAllocationSize
;
709 // Alignment must be a power of two or zero.
711 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
713 if (Alignment
== 0) {
714 AlignmentMask
= Alignment
;
716 AlignmentMask
= Alignment
- 1;
719 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
721 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
722 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
724 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
726 ASSERT (RealAllocationSize
> AllocationSize
);
728 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
729 if (RawAddress
== NULL
) {
732 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
734 // Save the original memory address just before the aligned address.
736 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
737 *FreePointer
= RawAddress
;
739 return (VOID
*) AlignedAddress
;
743 Allocates a buffer of type EfiBootServicesData at a specified alignment.
745 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
746 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
747 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
748 alignment remaining to satisfy the request, then NULL is returned.
749 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
751 @param AllocationSize The number of bytes to allocate.
752 @param Alignment The requested alignment of the allocation. Must be a power of two.
753 If Alignment is zero, then byte alignment is used.
755 @return A pointer to the allocated buffer or NULL if allocation fails.
760 AllocateAlignedPool (
761 IN UINTN AllocationSize
,
765 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
769 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
771 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
772 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
773 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
774 alignment remaining to satisfy the request, then NULL is returned.
775 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
777 @param AllocationSize The number of bytes to allocate.
778 @param Alignment The requested alignment of the allocation. Must be a power of two.
779 If Alignment is zero, then byte alignment is used.
781 @return A pointer to the allocated buffer or NULL if allocation fails.
786 AllocateAlignedRuntimePool (
787 IN UINTN AllocationSize
,
791 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
795 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
797 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
798 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
799 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
800 alignment remaining to satisfy the request, then NULL is returned.
801 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
803 @param AllocationSize The number of bytes to allocate.
804 @param Alignment The requested alignment of the allocation. Must be a power of two.
805 If Alignment is zero, then byte alignment is used.
807 @return A pointer to the allocated buffer or NULL if allocation fails.
812 AllocateAlignedReservedPool (
813 IN UINTN AllocationSize
,
817 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
821 Allocates and zeros a buffer of a certain pool type at a specified alignment.
823 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
824 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
825 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
826 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
827 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
829 @param PoolType The type of pool to allocate.
830 @param AllocationSize The number of bytes to allocate.
831 @param Alignment The requested alignment of the allocation. Must be a power of two.
832 If Alignment is zero, then byte alignment is used.
834 @return A pointer to the allocated buffer or NULL if allocation fails.
838 InternalAllocateAlignedZeroPool (
839 IN EFI_MEMORY_TYPE PoolType
,
840 IN UINTN AllocationSize
,
845 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
846 if (Memory
!= NULL
) {
847 Memory
= ZeroMem (Memory
, AllocationSize
);
853 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
855 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
856 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
857 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
858 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
860 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
862 @param AllocationSize The number of bytes to allocate.
863 @param Alignment The requested alignment of the allocation. Must be a power of two.
864 If Alignment is zero, then byte alignment is used.
866 @return A pointer to the allocated buffer or NULL if allocation fails.
871 AllocateAlignedZeroPool (
872 IN UINTN AllocationSize
,
876 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
880 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
882 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
883 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
884 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
885 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
887 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
889 @param AllocationSize The number of bytes to allocate.
890 @param Alignment The requested alignment of the allocation. Must be a power of two.
891 If Alignment is zero, then byte alignment is used.
893 @return A pointer to the allocated buffer or NULL if allocation fails.
898 AllocateAlignedRuntimeZeroPool (
899 IN UINTN AllocationSize
,
903 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
907 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
909 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
910 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
911 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
912 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
914 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
916 @param AllocationSize The number of bytes to allocate.
917 @param Alignment The requested alignment of the allocation. Must be a power of two.
918 If Alignment is zero, then byte alignment is used.
920 @return A pointer to the allocated buffer or NULL if allocation fails.
925 AllocateAlignedReservedZeroPool (
926 IN UINTN AllocationSize
,
930 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
934 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
936 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
937 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
938 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
939 to satisfy the request, then NULL is returned.
940 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
942 @param PoolType The type of pool to allocate.
943 @param AllocationSize The number of bytes to allocate.
944 @param Buffer The buffer to copy to the allocated buffer.
945 @param Alignment The requested alignment of the allocation. Must be a power of two.
946 If Alignment is zero, then byte alignment is used.
948 @return A pointer to the allocated buffer or NULL if allocation fails.
952 InternalAllocateAlignedCopyPool (
953 IN EFI_MEMORY_TYPE PoolType
,
954 IN UINTN AllocationSize
,
955 IN CONST VOID
*Buffer
,
961 ASSERT (Buffer
!= NULL
);
962 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
964 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
965 if (Memory
!= NULL
) {
966 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
972 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
974 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
975 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
976 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
977 alignment remaining to satisfy the request, then NULL is returned.
978 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
980 @param AllocationSize The number of bytes to allocate.
981 @param Buffer The buffer to copy to the allocated buffer.
982 @param Alignment The requested alignment of the allocation. Must be a power of two.
983 If Alignment is zero, then byte alignment is used.
985 @return A pointer to the allocated buffer or NULL if allocation fails.
990 AllocateAlignedCopyPool (
991 IN UINTN AllocationSize
,
992 IN CONST VOID
*Buffer
,
996 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
1000 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
1002 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
1003 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1004 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1005 alignment remaining to satisfy the request, then NULL is returned.
1006 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1008 @param AllocationSize The number of bytes to allocate.
1009 @param Buffer The buffer to copy to the allocated buffer.
1010 @param Alignment The requested alignment of the allocation. Must be a power of two.
1011 If Alignment is zero, then byte alignment is used.
1013 @return A pointer to the allocated buffer or NULL if allocation fails.
1018 AllocateAlignedRuntimeCopyPool (
1019 IN UINTN AllocationSize
,
1020 IN CONST VOID
*Buffer
,
1024 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1028 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1030 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1031 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1032 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1033 alignment remaining to satisfy the request, then NULL is returned.
1034 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1036 @param AllocationSize The number of bytes to allocate.
1037 @param Buffer The buffer to copy to the allocated buffer.
1038 @param Alignment The requested alignment of the allocation. Must be a power of two.
1039 If Alignment is zero, then byte alignment is used.
1041 @return A pointer to the allocated buffer or NULL if allocation fails.
1046 AllocateAlignedReservedCopyPool (
1047 IN UINTN AllocationSize
,
1048 IN CONST VOID
*Buffer
,
1052 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1056 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1057 in the Memory Allocation Library.
1059 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1060 aligned pool allocation services of the Memory Allocation Library.
1061 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1062 Library, then ASSERT().
1064 @param Buffer Pointer to the buffer to free.
1078 // Get the pre-saved original address in the over-allocate pool.
1080 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1081 RawAddress
= *FreePointer
;
1083 Status
= gBS
->FreePool (RawAddress
);
1084 ASSERT_EFI_ERROR (Status
);