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.
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
24 #include "MemoryAllocationLibInternals.h"
27 Allocates one or more 4KB pages of a certain memory type.
29 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
30 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
31 If there is not enough memory remaining to satisfy the request, then NULL is returned.
33 @param MemoryType The type of memory to allocate.
34 @param Pages The number of 4 KB pages to allocate.
36 @return A pointer to the allocated buffer or NULL if allocation fails.
40 InternalAllocatePages (
41 IN EFI_MEMORY_TYPE MemoryType
,
46 EFI_PHYSICAL_ADDRESS Memory
;
52 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
53 if (EFI_ERROR (Status
)) {
56 return (VOID
*) (UINTN
) Memory
;
60 Allocates one or more 4KB pages of type EfiBootServicesData.
62 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
63 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
64 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
67 @param Pages The number of 4 KB pages to allocate.
69 @return A pointer to the allocated buffer or NULL if allocation fails.
78 return InternalAllocatePages (EfiBootServicesData
, Pages
);
82 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
84 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
85 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
86 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
89 @param Pages The number of 4 KB pages to allocate.
91 @return A pointer to the allocated buffer or NULL if allocation fails.
96 AllocateRuntimePages (
100 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
104 Allocates one or more 4KB pages of type EfiReservedMemoryType.
106 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
107 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
108 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
111 @param Pages The number of 4 KB pages to allocate.
113 @return A pointer to the allocated buffer or NULL if allocation fails.
118 AllocateReservedPages (
122 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
126 Frees one or more 4KB pages that were previously allocated with one of the page allocation
127 functions in the Memory Allocation Library.
129 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
130 must have been allocated on a previous call to the page allocation services of the Memory
132 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
134 If Pages is zero, then ASSERT().
136 @param Buffer Pointer to the buffer of pages to free.
137 @param Pages The number of 4 KB pages to free.
150 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
151 ASSERT_EFI_ERROR (Status
);
155 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
157 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
158 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
159 If there is not enough memory at the specified alignment remaining to satisfy the request, then
161 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
163 @param MemoryType The type of memory to allocate.
164 @param Pages The number of 4 KB pages to allocate.
165 @param Alignment The requested alignment of the allocation. Must be a power of two.
166 If Alignment is zero, then byte alignment is used.
168 @return A pointer to the allocated buffer or NULL if allocation fails.
172 InternalAllocateAlignedPages (
173 IN EFI_MEMORY_TYPE MemoryType
,
179 EFI_PHYSICAL_ADDRESS Memory
;
182 UINTN UnalignedPages
;
186 // Alignment must be a power of two or zero.
188 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
193 if (Alignment
> EFI_PAGE_SIZE
) {
195 // Caculate the total number of pages since alignment is larger than page size.
197 AlignmentMask
= Alignment
- 1;
198 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
200 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
202 ASSERT (RealPages
> Pages
);
204 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
205 if (EFI_ERROR (Status
)) {
208 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
209 UnalignedPages
= EFI_SIZE_TO_PAGES (AlignedMemory
- (UINTN
) Memory
);
210 if (UnalignedPages
> 0) {
212 // Free first unaligned page(s).
214 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
215 ASSERT_EFI_ERROR (Status
);
217 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
218 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
219 if (UnalignedPages
> 0) {
221 // Free last unaligned page(s).
223 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
224 ASSERT_EFI_ERROR (Status
);
228 // Do not over-allocate pages in this case.
230 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
231 if (EFI_ERROR (Status
)) {
234 AlignedMemory
= (UINTN
) Memory
;
236 return (VOID
*) AlignedMemory
;
240 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
242 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
243 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
244 returned. If there is not enough memory at the specified alignment remaining to satisfy the
245 request, then NULL is returned.
246 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
248 @param Pages The number of 4 KB pages to allocate.
249 @param Alignment The requested alignment of the allocation. Must be a power of two.
250 If Alignment is zero, then byte alignment is used.
252 @return A pointer to the allocated buffer or NULL if allocation fails.
257 AllocateAlignedPages (
262 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
266 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
268 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
269 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
270 returned. If there is not enough memory at the specified alignment remaining to satisfy the
271 request, then NULL is returned.
272 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
274 @param Pages The number of 4 KB pages to allocate.
275 @param Alignment The requested alignment of the allocation. Must be a power of two.
276 If Alignment is zero, then byte alignment is used.
278 @return A pointer to the allocated buffer or NULL if allocation fails.
283 AllocateAlignedRuntimePages (
288 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
292 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
294 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
295 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
296 returned. If there is not enough memory at the specified alignment remaining to satisfy the
297 request, then NULL is returned.
298 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
300 @param Pages The number of 4 KB pages to allocate.
301 @param Alignment The requested alignment of the allocation. Must be a power of two.
302 If Alignment is zero, then byte alignment is used.
304 @return A pointer to the allocated buffer or NULL if allocation fails.
309 AllocateAlignedReservedPages (
314 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
318 Frees one or more 4KB pages that were previously allocated with one of the aligned page
319 allocation functions in the Memory Allocation Library.
321 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
322 must have been allocated on a previous call to the aligned page allocation services of the Memory
324 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
325 Library, then ASSERT().
326 If Pages is zero, then ASSERT().
328 @param Buffer Pointer to the buffer of pages to free.
329 @param Pages The number of 4 KB pages to free.
342 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
343 ASSERT_EFI_ERROR (Status
);
347 Allocates a buffer of a certain pool type.
349 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
350 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
351 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
353 @param MemoryType The type of memory to allocate.
354 @param AllocationSize The number of bytes to allocate.
356 @return A pointer to the allocated buffer or NULL if allocation fails.
360 InternalAllocatePool (
361 IN EFI_MEMORY_TYPE MemoryType
,
362 IN UINTN AllocationSize
368 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
369 if (EFI_ERROR (Status
)) {
376 Allocates a buffer of type EfiBootServicesData.
378 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
379 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
380 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
382 @param AllocationSize The number of bytes to allocate.
384 @return A pointer to the allocated buffer or NULL if allocation fails.
390 IN UINTN AllocationSize
393 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
397 Allocates a buffer of type EfiRuntimeServicesData.
399 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
400 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
401 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
403 @param AllocationSize The number of bytes to allocate.
405 @return A pointer to the allocated buffer or NULL if allocation fails.
410 AllocateRuntimePool (
411 IN UINTN AllocationSize
414 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
418 Allocates a buffer of type EfieservedMemoryType.
420 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
421 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
422 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
424 @param AllocationSize The number of bytes to allocate.
426 @return A pointer to the allocated buffer or NULL if allocation fails.
431 AllocateReservedPool (
432 IN UINTN AllocationSize
435 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
439 Allocates and zeros a buffer of a certian pool type.
441 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
442 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
443 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
444 then NULL is returned.
446 @param PoolType The type of memory to allocate.
447 @param AllocationSize The number of bytes to allocate and zero.
449 @return A pointer to the allocated buffer or NULL if allocation fails.
453 InternalAllocateZeroPool (
454 IN EFI_MEMORY_TYPE PoolType
,
455 IN UINTN AllocationSize
460 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
461 if (Memory
!= NULL
) {
462 Memory
= ZeroMem (Memory
, AllocationSize
);
468 Allocates and zeros a buffer of type EfiBootServicesData.
470 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
471 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
472 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
473 request, then NULL is returned.
475 @param AllocationSize The number of bytes to allocate and zero.
477 @return A pointer to the allocated buffer or NULL if allocation fails.
483 IN UINTN AllocationSize
486 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
490 Allocates and zeros a buffer of type EfiRuntimeServicesData.
492 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
493 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
494 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
495 request, then NULL is returned.
497 @param AllocationSize The number of bytes to allocate and zero.
499 @return A pointer to the allocated buffer or NULL if allocation fails.
504 AllocateRuntimeZeroPool (
505 IN UINTN AllocationSize
508 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
512 Allocates and zeros a buffer of type EfiReservedMemoryType.
514 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
515 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
516 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
517 request, then NULL is returned.
519 @param AllocationSize The number of bytes to allocate and zero.
521 @return A pointer to the allocated buffer or NULL if allocation fails.
526 AllocateReservedZeroPool (
527 IN UINTN AllocationSize
530 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
534 Copies a buffer to an allocated buffer of a certian pool type.
536 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
537 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
538 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
539 is not enough memory remaining to satisfy the request, then NULL is returned.
540 If Buffer is NULL, then ASSERT().
541 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
543 @param PoolType The type of pool to allocate.
544 @param AllocationSize The number of bytes to allocate and zero.
545 @param Buffer The buffer to copy to the allocated buffer.
547 @return A pointer to the allocated buffer or NULL if allocation fails.
551 InternalAllocateCopyPool (
552 IN EFI_MEMORY_TYPE PoolType
,
553 IN UINTN AllocationSize
,
554 IN CONST VOID
*Buffer
559 ASSERT (Buffer
!= NULL
);
560 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
562 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
563 if (Memory
!= NULL
) {
564 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
570 Copies a buffer to an allocated buffer of type EfiBootServicesData.
572 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
573 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
574 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
575 is not enough memory remaining to satisfy the request, then NULL is returned.
576 If Buffer is NULL, then ASSERT().
577 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
579 @param AllocationSize The number of bytes to allocate and zero.
580 @param Buffer The buffer to copy to the allocated buffer.
582 @return A pointer to the allocated buffer or NULL if allocation fails.
588 IN UINTN AllocationSize
,
589 IN CONST VOID
*Buffer
592 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
596 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
598 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
599 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
600 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
601 is not enough memory remaining to satisfy the request, then NULL is returned.
602 If Buffer is NULL, then ASSERT().
603 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
605 @param AllocationSize The number of bytes to allocate and zero.
606 @param Buffer The buffer to copy to the allocated buffer.
608 @return A pointer to the allocated buffer or NULL if allocation fails.
613 AllocateRuntimeCopyPool (
614 IN UINTN AllocationSize
,
615 IN CONST VOID
*Buffer
618 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
622 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
624 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
625 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
626 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
627 is not enough memory remaining to satisfy the request, then NULL is returned.
628 If Buffer is NULL, then ASSERT().
629 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
631 @param AllocationSize The number of bytes to allocate and zero.
632 @param Buffer The buffer to copy to the allocated buffer.
634 @return A pointer to the allocated buffer or NULL if allocation fails.
639 AllocateReservedCopyPool (
640 IN UINTN AllocationSize
,
641 IN CONST VOID
*Buffer
644 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
648 Frees a buffer that was previously allocated with one of the pool allocation functions in the
649 Memory Allocation Library.
651 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
652 pool allocation services of the Memory Allocation Library.
653 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
656 @param Buffer Pointer to the buffer to free.
667 Status
= gBS
->FreePool (Buffer
);
668 ASSERT_EFI_ERROR (Status
);
672 Allocates a buffer of a certain pool type at a specified alignment.
674 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
675 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
676 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
677 to satisfy the request, then NULL is returned.
678 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
680 @param PoolType The type of pool to allocate.
681 @param AllocationSize The number of bytes to allocate.
682 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
683 If Alignment is zero, then byte alignment is used.
685 @return A pointer to the allocated buffer or NULL if allocation fails.
689 InternalAllocateAlignedPool (
690 IN EFI_MEMORY_TYPE PoolType
,
691 IN UINTN AllocationSize
,
696 UINTN AlignedAddress
;
698 UINTN OverAllocationSize
;
699 UINTN RealAllocationSize
;
703 // Alignment must be a power of two or zero.
705 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
707 if (Alignment
== 0) {
708 AlignmentMask
= Alignment
;
710 AlignmentMask
= Alignment
- 1;
713 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
715 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
716 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
718 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
720 ASSERT (RealAllocationSize
> AllocationSize
);
722 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
723 if (RawAddress
== NULL
) {
726 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
728 // Save the original memory address just before the aligned address.
730 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
731 *FreePointer
= RawAddress
;
733 return (VOID
*) AlignedAddress
;
737 Allocates a buffer of type EfiBootServicesData at a specified alignment.
739 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
740 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
741 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
742 alignment remaining to satisfy the request, then NULL is returned.
743 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
745 @param AllocationSize The number of bytes to allocate.
746 @param Alignment The requested alignment of the allocation. Must be a power of two.
747 If Alignment is zero, then byte alignment is used.
749 @return A pointer to the allocated buffer or NULL if allocation fails.
754 AllocateAlignedPool (
755 IN UINTN AllocationSize
,
759 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
763 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
765 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
766 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
767 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
768 alignment remaining to satisfy the request, then NULL is returned.
769 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
771 @param AllocationSize The number of bytes to allocate.
772 @param Alignment The requested alignment of the allocation. Must be a power of two.
773 If Alignment is zero, then byte alignment is used.
775 @return A pointer to the allocated buffer or NULL if allocation fails.
780 AllocateAlignedRuntimePool (
781 IN UINTN AllocationSize
,
785 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
789 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
791 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
792 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
793 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
794 alignment remaining to satisfy the request, then NULL is returned.
795 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
797 @param AllocationSize The number of bytes to allocate.
798 @param Alignment The requested alignment of the allocation. Must be a power of two.
799 If Alignment is zero, then byte alignment is used.
801 @return A pointer to the allocated buffer or NULL if allocation fails.
806 AllocateAlignedReservedPool (
807 IN UINTN AllocationSize
,
811 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
815 Allocates and zeros a buffer of a certain pool type at a specified alignment.
817 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
818 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
819 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
820 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
821 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
823 @param PoolType The type of pool to allocate.
824 @param AllocationSize The number of bytes to allocate.
825 @param Alignment The requested alignment of the allocation. Must be a power of two.
826 If Alignment is zero, then byte alignment is used.
828 @return A pointer to the allocated buffer or NULL if allocation fails.
832 InternalAllocateAlignedZeroPool (
833 IN EFI_MEMORY_TYPE PoolType
,
834 IN UINTN AllocationSize
,
839 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
840 if (Memory
!= NULL
) {
841 Memory
= ZeroMem (Memory
, AllocationSize
);
847 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
849 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
850 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
851 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
852 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
854 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
856 @param AllocationSize The number of bytes to allocate.
857 @param Alignment The requested alignment of the allocation. Must be a power of two.
858 If Alignment is zero, then byte alignment is used.
860 @return A pointer to the allocated buffer or NULL if allocation fails.
865 AllocateAlignedZeroPool (
866 IN UINTN AllocationSize
,
870 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
874 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
876 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
877 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
878 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
879 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
881 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
883 @param AllocationSize The number of bytes to allocate.
884 @param Alignment The requested alignment of the allocation. Must be a power of two.
885 If Alignment is zero, then byte alignment is used.
887 @return A pointer to the allocated buffer or NULL if allocation fails.
892 AllocateAlignedRuntimeZeroPool (
893 IN UINTN AllocationSize
,
897 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
901 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
903 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
904 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
905 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
906 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
908 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
910 @param AllocationSize The number of bytes to allocate.
911 @param Alignment The requested alignment of the allocation. Must be a power of two.
912 If Alignment is zero, then byte alignment is used.
914 @return A pointer to the allocated buffer or NULL if allocation fails.
919 AllocateAlignedReservedZeroPool (
920 IN UINTN AllocationSize
,
924 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
928 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
930 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
931 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
932 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
933 to satisfy the request, then NULL is returned.
934 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
936 @param PoolType The type of pool to allocate.
937 @param AllocationSize The number of bytes to allocate.
938 @param Buffer The buffer to copy to the allocated buffer.
939 @param Alignment The requested alignment of the allocation. Must be a power of two.
940 If Alignment is zero, then byte alignment is used.
942 @return A pointer to the allocated buffer or NULL if allocation fails.
946 InternalAllocateAlignedCopyPool (
947 IN EFI_MEMORY_TYPE PoolType
,
948 IN UINTN AllocationSize
,
949 IN CONST VOID
*Buffer
,
955 ASSERT (Buffer
!= NULL
);
956 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
958 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
959 if (Memory
!= NULL
) {
960 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
966 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
968 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
969 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
970 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
971 alignment remaining to satisfy the request, then NULL is returned.
972 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
974 @param AllocationSize The number of bytes to allocate.
975 @param Buffer The buffer to copy to the allocated buffer.
976 @param Alignment The requested alignment of the allocation. Must be a power of two.
977 If Alignment is zero, then byte alignment is used.
979 @return A pointer to the allocated buffer or NULL if allocation fails.
984 AllocateAlignedCopyPool (
985 IN UINTN AllocationSize
,
986 IN CONST VOID
*Buffer
,
990 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
994 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
996 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
997 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
998 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
999 alignment remaining to satisfy the request, then NULL is returned.
1000 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1002 @param AllocationSize The number of bytes to allocate.
1003 @param Buffer The buffer to copy to the allocated buffer.
1004 @param Alignment The requested alignment of the allocation. Must be a power of two.
1005 If Alignment is zero, then byte alignment is used.
1007 @return A pointer to the allocated buffer or NULL if allocation fails.
1012 AllocateAlignedRuntimeCopyPool (
1013 IN UINTN AllocationSize
,
1014 IN CONST VOID
*Buffer
,
1018 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1022 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1024 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1025 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1026 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1027 alignment remaining to satisfy the request, then NULL is returned.
1028 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1030 @param AllocationSize The number of bytes to allocate.
1031 @param Buffer The buffer to copy to the allocated buffer.
1032 @param Alignment The requested alignment of the allocation. Must be a power of two.
1033 If Alignment is zero, then byte alignment is used.
1035 @return A pointer to the allocated buffer or NULL if allocation fails.
1040 AllocateAlignedReservedCopyPool (
1041 IN UINTN AllocationSize
,
1042 IN CONST VOID
*Buffer
,
1046 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1050 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1051 in the Memory Allocation Library.
1053 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1054 aligned pool allocation services of the Memory Allocation Library.
1055 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1056 Library, then ASSERT().
1058 @param Buffer Pointer to the buffer to free.
1072 // Get the pre-saved original address in the over-allocate pool.
1074 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1075 RawAddress
= *FreePointer
;
1077 Status
= gBS
->FreePool (RawAddress
);
1078 ASSERT_EFI_ERROR (Status
);
1088 if (Buffer
!= NULL
) {