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
19 Allocates one or more 4KB pages of a certain memory type.
21 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
22 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
23 If there is not enough memory remaining to satisfy the request, then NULL is returned.
25 @param MemoryType The type of memory to allocate.
26 @param Pages The number of 4 KB pages to allocate.
28 @return A pointer to the allocated buffer or NULL if allocation fails.
32 InternalAllocatePages (
33 IN EFI_MEMORY_TYPE MemoryType
,
38 EFI_PHYSICAL_ADDRESS Memory
;
44 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
45 if (EFI_ERROR (Status
)) {
48 return (VOID
*) (UINTN
) Memory
;
52 Allocates one or more 4KB pages of type EfiBootServicesData.
54 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
55 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
56 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
59 @param Pages The number of 4 KB pages to allocate.
61 @return A pointer to the allocated buffer or NULL if allocation fails.
70 return InternalAllocatePages (EfiBootServicesData
, Pages
);
74 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
76 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
77 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
78 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
81 @param Pages The number of 4 KB pages to allocate.
83 @return A pointer to the allocated buffer or NULL if allocation fails.
88 AllocateRuntimePages (
92 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
96 Allocates one or more 4KB pages of type EfiReservedMemoryType.
98 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
99 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
100 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
103 @param Pages The number of 4 KB pages to allocate.
105 @return A pointer to the allocated buffer or NULL if allocation fails.
110 AllocateReservedPages (
114 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
118 Frees one or more 4KB pages that were previously allocated with one of the page allocation
119 functions in the Memory Allocation Library.
121 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
122 must have been allocated on a previous call to the page allocation services of the Memory
124 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
126 If Pages is zero, then ASSERT().
128 @param Buffer Pointer to the buffer of pages to free.
129 @param Pages The number of 4 KB pages to free.
142 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
143 ASSERT_EFI_ERROR (Status
);
147 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
149 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
150 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
151 If there is not enough memory at the specified alignment remaining to satisfy the request, then
153 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
155 @param MemoryType The type of memory to allocate.
156 @param Pages The number of 4 KB pages to allocate.
157 @param Alignment The requested alignment of the allocation. Must be a power of two.
158 If Alignment is zero, then byte alignment is used.
160 @return A pointer to the allocated buffer or NULL if allocation fails.
164 InternalAllocateAlignedPages (
165 IN EFI_MEMORY_TYPE MemoryType
,
171 EFI_PHYSICAL_ADDRESS Memory
;
174 UINTN UnalignedPages
;
178 // Alignment must be a power of two or zero.
180 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
185 if (Alignment
> EFI_PAGE_SIZE
) {
187 // Caculate the total number of pages since alignment is larger than page size.
189 AlignmentMask
= Alignment
- 1;
190 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
192 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
194 ASSERT (RealPages
> Pages
);
196 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
197 if (EFI_ERROR (Status
)) {
200 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
201 UnalignedPages
= EFI_SIZE_TO_PAGES ((UINTN
) Memory
- AlignedMemory
);
202 if (UnalignedPages
> 0) {
204 // Free first unaligned page(s).
206 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
207 ASSERT_EFI_ERROR (Status
);
209 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
210 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
211 if (UnalignedPages
> 0) {
213 // Free last unaligned page(s).
215 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
216 ASSERT_EFI_ERROR (Status
);
220 // Do not over-allocate pages in this case.
222 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
223 if (EFI_ERROR (Status
)) {
226 AlignedMemory
= (UINTN
) Memory
;
228 return (VOID
*) AlignedMemory
;
232 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
234 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
235 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
236 returned. If there is not enough memory at the specified alignment remaining to satisfy the
237 request, then NULL is returned.
238 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
240 @param Pages The number of 4 KB pages to allocate.
241 @param Alignment The requested alignment of the allocation. Must be a power of two.
242 If Alignment is zero, then byte alignment is used.
244 @return A pointer to the allocated buffer or NULL if allocation fails.
249 AllocateAlignedPages (
254 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
258 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
260 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
261 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
262 returned. If there is not enough memory at the specified alignment remaining to satisfy the
263 request, then NULL is returned.
264 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
266 @param Pages The number of 4 KB pages to allocate.
267 @param Alignment The requested alignment of the allocation. Must be a power of two.
268 If Alignment is zero, then byte alignment is used.
270 @return A pointer to the allocated buffer or NULL if allocation fails.
275 AllocateAlignedRuntimePages (
280 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
284 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
286 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
287 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
288 returned. If there is not enough memory at the specified alignment remaining to satisfy the
289 request, then NULL is returned.
290 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
292 @param Pages The number of 4 KB pages to allocate.
293 @param Alignment The requested alignment of the allocation. Must be a power of two.
294 If Alignment is zero, then byte alignment is used.
296 @return A pointer to the allocated buffer or NULL if allocation fails.
301 AllocateAlignedReservedPages (
306 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
310 Frees one or more 4KB pages that were previously allocated with one of the aligned page
311 allocation functions in the Memory Allocation Library.
313 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
314 must have been allocated on a previous call to the aligned page allocation services of the Memory
316 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
317 Library, then ASSERT().
318 If Pages is zero, then ASSERT().
320 @param Buffer Pointer to the buffer of pages to free.
321 @param Pages The number of 4 KB pages to free.
334 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
335 ASSERT_EFI_ERROR (Status
);
339 Allocates a buffer of a certain pool type.
341 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
342 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
343 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
345 @param MemoryType The type of memory to allocate.
346 @param AllocationSize The number of bytes to allocate.
348 @return A pointer to the allocated buffer or NULL if allocation fails.
352 InternalAllocatePool (
353 IN EFI_MEMORY_TYPE MemoryType
,
354 IN UINTN AllocationSize
360 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
361 if (EFI_ERROR (Status
)) {
368 Allocates a buffer of type EfiBootServicesData.
370 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
371 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
372 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
374 @param AllocationSize The number of bytes to allocate.
376 @return A pointer to the allocated buffer or NULL if allocation fails.
382 IN UINTN AllocationSize
385 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
389 Allocates a buffer of type EfiRuntimeServicesData.
391 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
392 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
393 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
395 @param AllocationSize The number of bytes to allocate.
397 @return A pointer to the allocated buffer or NULL if allocation fails.
402 AllocateRuntimePool (
403 IN UINTN AllocationSize
406 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
410 Allocates a buffer of type EfieservedMemoryType.
412 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
413 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
414 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
416 @param AllocationSize The number of bytes to allocate.
418 @return A pointer to the allocated buffer or NULL if allocation fails.
423 AllocateReservedPool (
424 IN UINTN AllocationSize
427 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
431 Allocates and zeros a buffer of a certian pool type.
433 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
434 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
435 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
436 then NULL is returned.
438 @param PoolType The type of memory to allocate.
439 @param AllocationSize The number of bytes to allocate and zero.
441 @return A pointer to the allocated buffer or NULL if allocation fails.
445 InternalAllocateZeroPool (
446 IN EFI_MEMORY_TYPE PoolType
,
447 IN UINTN AllocationSize
452 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
453 if (Memory
!= NULL
) {
454 Memory
= ZeroMem (Memory
, AllocationSize
);
460 Allocates and zeros a buffer of type EfiBootServicesData.
462 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
463 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
464 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
465 request, then NULL is returned.
467 @param AllocationSize The number of bytes to allocate and zero.
469 @return A pointer to the allocated buffer or NULL if allocation fails.
475 IN UINTN AllocationSize
478 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
482 Allocates and zeros a buffer of type EfiRuntimeServicesData.
484 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
485 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
486 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
487 request, then NULL is returned.
489 @param AllocationSize The number of bytes to allocate and zero.
491 @return A pointer to the allocated buffer or NULL if allocation fails.
496 AllocateRuntimeZeroPool (
497 IN UINTN AllocationSize
500 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
504 Allocates and zeros a buffer of type EfiReservedMemoryType.
506 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
507 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
508 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
509 request, then NULL is returned.
511 @param AllocationSize The number of bytes to allocate and zero.
513 @return A pointer to the allocated buffer or NULL if allocation fails.
518 AllocateReservedZeroPool (
519 IN UINTN AllocationSize
522 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
526 Copies a buffer to an allocated buffer of a certian pool type.
528 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
529 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
530 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
531 is not enough memory remaining to satisfy the request, then NULL is returned.
532 If Buffer is NULL, then ASSERT().
533 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
535 @param PoolType The type of pool to allocate.
536 @param AllocationSize The number of bytes to allocate and zero.
537 @param Buffer The buffer to copy to the allocated buffer.
539 @return A pointer to the allocated buffer or NULL if allocation fails.
543 InternalAllocateCopyPool (
544 IN EFI_MEMORY_TYPE PoolType
,
545 IN UINTN AllocationSize
,
546 IN CONST VOID
*Buffer
551 ASSERT (Buffer
!= NULL
);
552 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
554 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
555 if (Memory
!= NULL
) {
556 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
562 Copies a buffer to an allocated buffer of type EfiBootServicesData.
564 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
565 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
566 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
567 is not enough memory remaining to satisfy the request, then NULL is returned.
568 If Buffer is NULL, then ASSERT().
569 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
571 @param AllocationSize The number of bytes to allocate and zero.
572 @param Buffer The buffer to copy to the allocated buffer.
574 @return A pointer to the allocated buffer or NULL if allocation fails.
580 IN UINTN AllocationSize
,
581 IN CONST VOID
*Buffer
584 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
588 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
590 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
591 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
592 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
593 is not enough memory remaining to satisfy the request, then NULL is returned.
594 If Buffer is NULL, then ASSERT().
595 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
597 @param AllocationSize The number of bytes to allocate and zero.
598 @param Buffer The buffer to copy to the allocated buffer.
600 @return A pointer to the allocated buffer or NULL if allocation fails.
605 AllocateRuntimeCopyPool (
606 IN UINTN AllocationSize
,
607 IN CONST VOID
*Buffer
610 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
614 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
616 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
617 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
618 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
619 is not enough memory remaining to satisfy the request, then NULL is returned.
620 If Buffer is NULL, then ASSERT().
621 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
623 @param AllocationSize The number of bytes to allocate and zero.
624 @param Buffer The buffer to copy to the allocated buffer.
626 @return A pointer to the allocated buffer or NULL if allocation fails.
631 AllocateReservedCopyPool (
632 IN UINTN AllocationSize
,
633 IN CONST VOID
*Buffer
636 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
640 Frees a buffer that was previously allocated with one of the pool allocation functions in the
641 Memory Allocation Library.
643 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
644 pool allocation services of the Memory Allocation Library.
645 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
648 @param Buffer Pointer to the buffer to free.
659 Status
= gBS
->FreePool (Buffer
);
660 ASSERT_EFI_ERROR (Status
);
664 Allocates a buffer of a certain pool type at a specified alignment.
666 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
667 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
668 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
669 to satisfy the request, then NULL is returned.
670 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
672 @param PoolType The type of pool to allocate.
673 @param AllocationSize The number of bytes to allocate.
674 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
675 If Alignment is zero, then byte alignment is used.
677 @return A pointer to the allocated buffer or NULL if allocation fails.
681 InternalAllocateAlignedPool (
682 IN EFI_MEMORY_TYPE PoolType
,
683 IN UINTN AllocationSize
,
688 UINTN AlignedAddress
;
690 UINTN OverAllocationSize
;
691 UINTN RealAllocationSize
;
695 // Alignment must be a power of two or zero.
697 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
699 if (Alignment
== 0) {
700 AlignmentMask
= Alignment
;
702 AlignmentMask
= Alignment
- 1;
705 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
707 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
708 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
710 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
712 ASSERT (RealAllocationSize
> AllocationSize
);
714 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
715 if (RawAddress
== NULL
) {
718 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
720 // Save the original memory address just before the aligned address.
722 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
723 *FreePointer
= RawAddress
;
725 return (VOID
*) AlignedAddress
;
729 Allocates a buffer of type EfiBootServicesData at a specified alignment.
731 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
732 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
733 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
734 alignment remaining to satisfy the request, then NULL is returned.
735 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
737 @param AllocationSize The number of bytes to allocate.
738 @param Alignment The requested alignment of the allocation. Must be a power of two.
739 If Alignment is zero, then byte alignment is used.
741 @return A pointer to the allocated buffer or NULL if allocation fails.
746 AllocateAlignedPool (
747 IN UINTN AllocationSize
,
751 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
755 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
757 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
758 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
759 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
760 alignment remaining to satisfy the request, then NULL is returned.
761 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
763 @param AllocationSize The number of bytes to allocate.
764 @param Alignment The requested alignment of the allocation. Must be a power of two.
765 If Alignment is zero, then byte alignment is used.
767 @return A pointer to the allocated buffer or NULL if allocation fails.
772 AllocateAlignedRuntimePool (
773 IN UINTN AllocationSize
,
777 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
781 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
783 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
784 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
785 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
786 alignment remaining to satisfy the request, then NULL is returned.
787 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
789 @param AllocationSize The number of bytes to allocate.
790 @param Alignment The requested alignment of the allocation. Must be a power of two.
791 If Alignment is zero, then byte alignment is used.
793 @return A pointer to the allocated buffer or NULL if allocation fails.
798 AllocateAlignedReservedPool (
799 IN UINTN AllocationSize
,
803 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
807 Allocates and zeros a buffer of a certain pool type at a specified alignment.
809 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
810 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
811 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
812 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
813 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
815 @param PoolType The type of pool to allocate.
816 @param AllocationSize The number of bytes to allocate.
817 @param Alignment The requested alignment of the allocation. Must be a power of two.
818 If Alignment is zero, then byte alignment is used.
820 @return A pointer to the allocated buffer or NULL if allocation fails.
824 InternalAllocateAlignedZeroPool (
825 IN EFI_MEMORY_TYPE PoolType
,
826 IN UINTN AllocationSize
,
831 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
832 if (Memory
!= NULL
) {
833 Memory
= ZeroMem (Memory
, AllocationSize
);
839 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
841 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
842 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
843 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
844 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
846 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
848 @param AllocationSize The number of bytes to allocate.
849 @param Alignment The requested alignment of the allocation. Must be a power of two.
850 If Alignment is zero, then byte alignment is used.
852 @return A pointer to the allocated buffer or NULL if allocation fails.
857 AllocateAlignedZeroPool (
858 IN UINTN AllocationSize
,
862 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
866 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
868 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
869 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
870 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
871 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
873 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
875 @param AllocationSize The number of bytes to allocate.
876 @param Alignment The requested alignment of the allocation. Must be a power of two.
877 If Alignment is zero, then byte alignment is used.
879 @return A pointer to the allocated buffer or NULL if allocation fails.
884 AllocateAlignedRuntimeZeroPool (
885 IN UINTN AllocationSize
,
889 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
893 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
895 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
896 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
897 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
898 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
900 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
902 @param AllocationSize The number of bytes to allocate.
903 @param Alignment The requested alignment of the allocation. Must be a power of two.
904 If Alignment is zero, then byte alignment is used.
906 @return A pointer to the allocated buffer or NULL if allocation fails.
911 AllocateAlignedReservedZeroPool (
912 IN UINTN AllocationSize
,
916 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
920 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
922 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
923 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
924 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
925 to satisfy the request, then NULL is returned.
926 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
928 @param PoolType The type of pool to allocate.
929 @param AllocationSize The number of bytes to allocate.
930 @param Buffer The buffer to copy to the allocated buffer.
931 @param Alignment The requested alignment of the allocation. Must be a power of two.
932 If Alignment is zero, then byte alignment is used.
934 @return A pointer to the allocated buffer or NULL if allocation fails.
938 InternalAllocateAlignedCopyPool (
939 IN EFI_MEMORY_TYPE PoolType
,
940 IN UINTN AllocationSize
,
941 IN CONST VOID
*Buffer
,
947 ASSERT (Buffer
!= NULL
);
948 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
950 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
951 if (Memory
!= NULL
) {
952 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
958 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
960 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
961 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
962 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
963 alignment remaining to satisfy the request, then NULL is returned.
964 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
966 @param AllocationSize The number of bytes to allocate.
967 @param Buffer The buffer to copy to the allocated buffer.
968 @param Alignment The requested alignment of the allocation. Must be a power of two.
969 If Alignment is zero, then byte alignment is used.
971 @return A pointer to the allocated buffer or NULL if allocation fails.
976 AllocateAlignedCopyPool (
977 IN UINTN AllocationSize
,
978 IN CONST VOID
*Buffer
,
982 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
986 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
988 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
989 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
990 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
991 alignment remaining to satisfy the request, then NULL is returned.
992 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
994 @param AllocationSize The number of bytes to allocate.
995 @param Buffer The buffer to copy to the allocated buffer.
996 @param Alignment The requested alignment of the allocation. Must be a power of two.
997 If Alignment is zero, then byte alignment is used.
999 @return A pointer to the allocated buffer or NULL if allocation fails.
1004 AllocateAlignedRuntimeCopyPool (
1005 IN UINTN AllocationSize
,
1006 IN CONST VOID
*Buffer
,
1010 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1014 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1016 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1017 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1018 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1019 alignment remaining to satisfy the request, then NULL is returned.
1020 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1022 @param AllocationSize The number of bytes to allocate.
1023 @param Buffer The buffer to copy to the allocated buffer.
1024 @param Alignment The requested alignment of the allocation. Must be a power of two.
1025 If Alignment is zero, then byte alignment is used.
1027 @return A pointer to the allocated buffer or NULL if allocation fails.
1032 AllocateAlignedReservedCopyPool (
1033 IN UINTN AllocationSize
,
1034 IN CONST VOID
*Buffer
,
1038 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1042 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1043 in the Memory Allocation Library.
1045 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1046 aligned pool allocation services of the Memory Allocation Library.
1047 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1048 Library, then ASSERT().
1050 @param Buffer Pointer to the buffer to free.
1064 // Get the pre-saved original address in the over-allocate pool.
1066 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1067 RawAddress
= *FreePointer
;
1069 Status
= gBS
->FreePool (RawAddress
);
1070 ASSERT_EFI_ERROR (Status
);