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
17 #include "MemoryAllocationLibInternals.h"
20 Allocates one or more 4KB pages of a certain memory type.
22 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated
23 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.
24 If there is not enough memory remaining to satisfy the request, then NULL is returned.
26 @param MemoryType The type of memory to allocate.
27 @param Pages The number of 4 KB pages to allocate.
29 @return A pointer to the allocated buffer or NULL if allocation fails.
33 InternalAllocatePages (
34 IN EFI_MEMORY_TYPE MemoryType
,
39 EFI_PHYSICAL_ADDRESS Memory
;
45 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
46 if (EFI_ERROR (Status
)) {
49 return (VOID
*) (UINTN
) Memory
;
53 Allocates one or more 4KB pages of type EfiBootServicesData.
55 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the
56 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
57 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
60 @param Pages The number of 4 KB pages to allocate.
62 @return A pointer to the allocated buffer or NULL if allocation fails.
71 return InternalAllocatePages (EfiBootServicesData
, Pages
);
75 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
77 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
78 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
79 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
82 @param Pages The number of 4 KB pages to allocate.
84 @return A pointer to the allocated buffer or NULL if allocation fails.
89 AllocateRuntimePages (
93 return InternalAllocatePages (EfiRuntimeServicesData
, Pages
);
97 Allocates one or more 4KB pages of type EfiReservedMemoryType.
99 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the
100 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
101 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
104 @param Pages The number of 4 KB pages to allocate.
106 @return A pointer to the allocated buffer or NULL if allocation fails.
111 AllocateReservedPages (
115 return InternalAllocatePages (EfiReservedMemoryType
, Pages
);
119 Frees one or more 4KB pages that were previously allocated with one of the page allocation
120 functions in the Memory Allocation Library.
122 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
123 must have been allocated on a previous call to the page allocation services of the Memory
125 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
127 If Pages is zero, then ASSERT().
129 @param Buffer Pointer to the buffer of pages to free.
130 @param Pages The number of 4 KB pages to free.
143 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
144 ASSERT_EFI_ERROR (Status
);
148 Allocates one or more 4KB pages of a certain memory type at a specified alignment.
150 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment
151 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.
152 If there is not enough memory at the specified alignment remaining to satisfy the request, then
154 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
156 @param MemoryType The type of memory to allocate.
157 @param Pages The number of 4 KB pages to allocate.
158 @param Alignment The requested alignment of the allocation. Must be a power of two.
159 If Alignment is zero, then byte alignment is used.
161 @return A pointer to the allocated buffer or NULL if allocation fails.
165 InternalAllocateAlignedPages (
166 IN EFI_MEMORY_TYPE MemoryType
,
172 EFI_PHYSICAL_ADDRESS Memory
;
175 UINTN UnalignedPages
;
179 // Alignment must be a power of two or zero.
181 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
186 if (Alignment
> EFI_PAGE_SIZE
) {
188 // Caculate the total number of pages since alignment is larger than page size.
190 AlignmentMask
= Alignment
- 1;
191 RealPages
= Pages
+ EFI_SIZE_TO_PAGES (Alignment
);
193 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
195 ASSERT (RealPages
> Pages
);
197 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, RealPages
, &Memory
);
198 if (EFI_ERROR (Status
)) {
201 AlignedMemory
= ((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
;
202 UnalignedPages
= EFI_SIZE_TO_PAGES ((UINTN
) Memory
- AlignedMemory
);
203 if (UnalignedPages
> 0) {
205 // Free first unaligned page(s).
207 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
208 ASSERT_EFI_ERROR (Status
);
210 Memory
= (EFI_PHYSICAL_ADDRESS
) (AlignedMemory
+ EFI_PAGES_TO_SIZE (Pages
));
211 UnalignedPages
= RealPages
- Pages
- UnalignedPages
;
212 if (UnalignedPages
> 0) {
214 // Free last unaligned page(s).
216 Status
= gBS
->FreePages (Memory
, UnalignedPages
);
217 ASSERT_EFI_ERROR (Status
);
221 // Do not over-allocate pages in this case.
223 Status
= gBS
->AllocatePages (AllocateAnyPages
, MemoryType
, Pages
, &Memory
);
224 if (EFI_ERROR (Status
)) {
227 AlignedMemory
= (UINTN
) Memory
;
229 return (VOID
*) AlignedMemory
;
233 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
235 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
236 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
237 returned. If there is not enough memory at the specified alignment remaining to satisfy the
238 request, then NULL is returned.
239 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
241 @param Pages The number of 4 KB pages to allocate.
242 @param Alignment The requested alignment of the allocation. Must be a power of two.
243 If Alignment is zero, then byte alignment is used.
245 @return A pointer to the allocated buffer or NULL if allocation fails.
250 AllocateAlignedPages (
255 return InternalAllocateAlignedPages (EfiBootServicesData
, Pages
, Alignment
);
259 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.
261 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an
262 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
263 returned. If there is not enough memory at the specified alignment remaining to satisfy the
264 request, then NULL is returned.
265 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
267 @param Pages The number of 4 KB pages to allocate.
268 @param Alignment The requested alignment of the allocation. Must be a power of two.
269 If Alignment is zero, then byte alignment is used.
271 @return A pointer to the allocated buffer or NULL if allocation fails.
276 AllocateAlignedRuntimePages (
281 return InternalAllocateAlignedPages (EfiRuntimeServicesData
, Pages
, Alignment
);
285 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
287 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an
288 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
289 returned. If there is not enough memory at the specified alignment remaining to satisfy the
290 request, then NULL is returned.
291 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
293 @param Pages The number of 4 KB pages to allocate.
294 @param Alignment The requested alignment of the allocation. Must be a power of two.
295 If Alignment is zero, then byte alignment is used.
297 @return A pointer to the allocated buffer or NULL if allocation fails.
302 AllocateAlignedReservedPages (
307 return InternalAllocateAlignedPages (EfiReservedMemoryType
, Pages
, Alignment
);
311 Frees one or more 4KB pages that were previously allocated with one of the aligned page
312 allocation functions in the Memory Allocation Library.
314 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
315 must have been allocated on a previous call to the aligned page allocation services of the Memory
317 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation
318 Library, then ASSERT().
319 If Pages is zero, then ASSERT().
321 @param Buffer Pointer to the buffer of pages to free.
322 @param Pages The number of 4 KB pages to free.
335 Status
= gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) Buffer
, Pages
);
336 ASSERT_EFI_ERROR (Status
);
340 Allocates a buffer of a certain pool type.
342 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a
343 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
344 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
346 @param MemoryType The type of memory to allocate.
347 @param AllocationSize The number of bytes to allocate.
349 @return A pointer to the allocated buffer or NULL if allocation fails.
353 InternalAllocatePool (
354 IN EFI_MEMORY_TYPE MemoryType
,
355 IN UINTN AllocationSize
361 Status
= gBS
->AllocatePool (MemoryType
, AllocationSize
, &Memory
);
362 if (EFI_ERROR (Status
)) {
369 Allocates a buffer of type EfiBootServicesData.
371 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
372 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
373 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
375 @param AllocationSize The number of bytes to allocate.
377 @return A pointer to the allocated buffer or NULL if allocation fails.
383 IN UINTN AllocationSize
386 return InternalAllocatePool (EfiBootServicesData
, AllocationSize
);
390 Allocates a buffer of type EfiRuntimeServicesData.
392 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns
393 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
394 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
396 @param AllocationSize The number of bytes to allocate.
398 @return A pointer to the allocated buffer or NULL if allocation fails.
403 AllocateRuntimePool (
404 IN UINTN AllocationSize
407 return InternalAllocatePool (EfiRuntimeServicesData
, AllocationSize
);
411 Allocates a buffer of type EfieservedMemoryType.
413 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType and returns
414 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
415 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
417 @param AllocationSize The number of bytes to allocate.
419 @return A pointer to the allocated buffer or NULL if allocation fails.
424 AllocateReservedPool (
425 IN UINTN AllocationSize
428 return InternalAllocatePool (EfiReservedMemoryType
, AllocationSize
);
432 Allocates and zeros a buffer of a certian pool type.
434 Allocates the number bytes specified by AllocationSize of a certian pool type, clears the buffer
435 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid
436 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,
437 then NULL is returned.
439 @param PoolType The type of memory to allocate.
440 @param AllocationSize The number of bytes to allocate and zero.
442 @return A pointer to the allocated buffer or NULL if allocation fails.
446 InternalAllocateZeroPool (
447 IN EFI_MEMORY_TYPE PoolType
,
448 IN UINTN AllocationSize
453 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
454 if (Memory
!= NULL
) {
455 Memory
= ZeroMem (Memory
, AllocationSize
);
461 Allocates and zeros a buffer of type EfiBootServicesData.
463 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
464 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
465 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
466 request, then NULL is returned.
468 @param AllocationSize The number of bytes to allocate and zero.
470 @return A pointer to the allocated buffer or NULL if allocation fails.
476 IN UINTN AllocationSize
479 return InternalAllocateZeroPool (EfiBootServicesData
, AllocationSize
);
483 Allocates and zeros a buffer of type EfiRuntimeServicesData.
485 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the
486 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
487 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
488 request, then NULL is returned.
490 @param AllocationSize The number of bytes to allocate and zero.
492 @return A pointer to the allocated buffer or NULL if allocation fails.
497 AllocateRuntimeZeroPool (
498 IN UINTN AllocationSize
501 return InternalAllocateZeroPool (EfiRuntimeServicesData
, AllocationSize
);
505 Allocates and zeros a buffer of type EfiReservedMemoryType.
507 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the
508 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
509 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
510 request, then NULL is returned.
512 @param AllocationSize The number of bytes to allocate and zero.
514 @return A pointer to the allocated buffer or NULL if allocation fails.
519 AllocateReservedZeroPool (
520 IN UINTN AllocationSize
523 return InternalAllocateZeroPool (EfiReservedMemoryType
, AllocationSize
);
527 Copies a buffer to an allocated buffer of a certian pool type.
529 Allocates the number bytes specified by AllocationSize of a certian pool type, copies
530 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
531 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
532 is not enough memory remaining to satisfy the request, then NULL is returned.
533 If Buffer is NULL, then ASSERT().
534 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
536 @param PoolType The type of pool to allocate.
537 @param AllocationSize The number of bytes to allocate and zero.
538 @param Buffer The buffer to copy to the allocated buffer.
540 @return A pointer to the allocated buffer or NULL if allocation fails.
544 InternalAllocateCopyPool (
545 IN EFI_MEMORY_TYPE PoolType
,
546 IN UINTN AllocationSize
,
547 IN CONST VOID
*Buffer
552 ASSERT (Buffer
!= NULL
);
553 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
555 Memory
= InternalAllocatePool (PoolType
, AllocationSize
);
556 if (Memory
!= NULL
) {
557 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
563 Copies a buffer to an allocated buffer of type EfiBootServicesData.
565 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies
566 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
567 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
568 is not enough memory remaining to satisfy the request, then NULL is returned.
569 If Buffer is NULL, then ASSERT().
570 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
572 @param AllocationSize The number of bytes to allocate and zero.
573 @param Buffer The buffer to copy to the allocated buffer.
575 @return A pointer to the allocated buffer or NULL if allocation fails.
581 IN UINTN AllocationSize
,
582 IN CONST VOID
*Buffer
585 return InternalAllocateCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
);
589 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
591 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies
592 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
593 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
594 is not enough memory remaining to satisfy the request, then NULL is returned.
595 If Buffer is NULL, then ASSERT().
596 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
598 @param AllocationSize The number of bytes to allocate and zero.
599 @param Buffer The buffer to copy to the allocated buffer.
601 @return A pointer to the allocated buffer or NULL if allocation fails.
606 AllocateRuntimeCopyPool (
607 IN UINTN AllocationSize
,
608 IN CONST VOID
*Buffer
611 return InternalAllocateCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
);
615 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
617 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies
618 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the
619 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
620 is not enough memory remaining to satisfy the request, then NULL is returned.
621 If Buffer is NULL, then ASSERT().
622 If AllocationSize is greater than (MAX_ADDRESS ? Buffer + 1), then ASSERT().
624 @param AllocationSize The number of bytes to allocate and zero.
625 @param Buffer The buffer to copy to the allocated buffer.
627 @return A pointer to the allocated buffer or NULL if allocation fails.
632 AllocateReservedCopyPool (
633 IN UINTN AllocationSize
,
634 IN CONST VOID
*Buffer
637 return InternalAllocateCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
);
641 Frees a buffer that was previously allocated with one of the pool allocation functions in the
642 Memory Allocation Library.
644 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
645 pool allocation services of the Memory Allocation Library.
646 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
649 @param Buffer Pointer to the buffer to free.
660 Status
= gBS
->FreePool (Buffer
);
661 ASSERT_EFI_ERROR (Status
);
665 Allocates a buffer of a certain pool type at a specified alignment.
667 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
668 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
669 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
670 to satisfy the request, then NULL is returned.
671 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
673 @param PoolType The type of pool to allocate.
674 @param AllocationSize The number of bytes to allocate.
675 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
676 If Alignment is zero, then byte alignment is used.
678 @return A pointer to the allocated buffer or NULL if allocation fails.
682 InternalAllocateAlignedPool (
683 IN EFI_MEMORY_TYPE PoolType
,
684 IN UINTN AllocationSize
,
689 UINTN AlignedAddress
;
691 UINTN OverAllocationSize
;
692 UINTN RealAllocationSize
;
696 // Alignment must be a power of two or zero.
698 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
700 if (Alignment
== 0) {
701 AlignmentMask
= Alignment
;
703 AlignmentMask
= Alignment
- 1;
706 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
708 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
709 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
711 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
713 ASSERT (RealAllocationSize
> AllocationSize
);
715 RawAddress
= InternalAllocatePool (PoolType
, RealAllocationSize
);
716 if (RawAddress
== NULL
) {
719 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
721 // Save the original memory address just before the aligned address.
723 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
724 *FreePointer
= RawAddress
;
726 return (VOID
*) AlignedAddress
;
730 Allocates a buffer of type EfiBootServicesData at a specified alignment.
732 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
733 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
734 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
735 alignment remaining to satisfy the request, then NULL is returned.
736 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
738 @param AllocationSize The number of bytes to allocate.
739 @param Alignment The requested alignment of the allocation. Must be a power of two.
740 If Alignment is zero, then byte alignment is used.
742 @return A pointer to the allocated buffer or NULL if allocation fails.
747 AllocateAlignedPool (
748 IN UINTN AllocationSize
,
752 return InternalAllocateAlignedPool (EfiBootServicesData
, AllocationSize
, Alignment
);
756 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
758 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
759 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
760 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
761 alignment remaining to satisfy the request, then NULL is returned.
762 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
764 @param AllocationSize The number of bytes to allocate.
765 @param Alignment The requested alignment of the allocation. Must be a power of two.
766 If Alignment is zero, then byte alignment is used.
768 @return A pointer to the allocated buffer or NULL if allocation fails.
773 AllocateAlignedRuntimePool (
774 IN UINTN AllocationSize
,
778 return InternalAllocateAlignedPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
782 Allocates a buffer of type EfieservedMemoryType at a specified alignment.
784 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
785 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
786 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
787 alignment remaining to satisfy the request, then NULL is returned.
788 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
790 @param AllocationSize The number of bytes to allocate.
791 @param Alignment The requested alignment of the allocation. Must be a power of two.
792 If Alignment is zero, then byte alignment is used.
794 @return A pointer to the allocated buffer or NULL if allocation fails.
799 AllocateAlignedReservedPool (
800 IN UINTN AllocationSize
,
804 return InternalAllocateAlignedPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
808 Allocates and zeros a buffer of a certain pool type at a specified alignment.
810 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
811 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
812 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
813 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
814 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
816 @param PoolType The type of pool to allocate.
817 @param AllocationSize The number of bytes to allocate.
818 @param Alignment The requested alignment of the allocation. Must be a power of two.
819 If Alignment is zero, then byte alignment is used.
821 @return A pointer to the allocated buffer or NULL if allocation fails.
825 InternalAllocateAlignedZeroPool (
826 IN EFI_MEMORY_TYPE PoolType
,
827 IN UINTN AllocationSize
,
832 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
833 if (Memory
!= NULL
) {
834 Memory
= ZeroMem (Memory
, AllocationSize
);
840 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
842 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
843 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
844 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
845 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
847 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
849 @param AllocationSize The number of bytes to allocate.
850 @param Alignment The requested alignment of the allocation. Must be a power of two.
851 If Alignment is zero, then byte alignment is used.
853 @return A pointer to the allocated buffer or NULL if allocation fails.
858 AllocateAlignedZeroPool (
859 IN UINTN AllocationSize
,
863 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
867 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
869 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData with an
870 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
871 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
872 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
874 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
876 @param AllocationSize The number of bytes to allocate.
877 @param Alignment The requested alignment of the allocation. Must be a power of two.
878 If Alignment is zero, then byte alignment is used.
880 @return A pointer to the allocated buffer or NULL if allocation fails.
885 AllocateAlignedRuntimeZeroPool (
886 IN UINTN AllocationSize
,
890 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData
, AllocationSize
, Alignment
);
894 Allocates and zeros a buffer of type EfieservedMemoryType at a specified alignment.
896 Allocates the number bytes specified by AllocationSize of type EfieservedMemoryType with an
897 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
898 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
899 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
901 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
903 @param AllocationSize The number of bytes to allocate.
904 @param Alignment The requested alignment of the allocation. Must be a power of two.
905 If Alignment is zero, then byte alignment is used.
907 @return A pointer to the allocated buffer or NULL if allocation fails.
912 AllocateAlignedReservedZeroPool (
913 IN UINTN AllocationSize
,
917 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType
, AllocationSize
, Alignment
);
921 Copies a buffer to an allocated buffer of a certain pool type at a specified alignment.
923 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
924 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
925 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
926 to satisfy the request, then NULL is returned.
927 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
929 @param PoolType The type of pool to allocate.
930 @param AllocationSize The number of bytes to allocate.
931 @param Buffer The buffer to copy to the allocated buffer.
932 @param Alignment The requested alignment of the allocation. Must be a power of two.
933 If Alignment is zero, then byte alignment is used.
935 @return A pointer to the allocated buffer or NULL if allocation fails.
939 InternalAllocateAlignedCopyPool (
940 IN EFI_MEMORY_TYPE PoolType
,
941 IN UINTN AllocationSize
,
942 IN CONST VOID
*Buffer
,
948 ASSERT (Buffer
!= NULL
);
949 ASSERT (AllocationSize
<= (MAX_ADDRESS
- (UINTN
) Buffer
+ 1));
951 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
952 if (Memory
!= NULL
) {
953 Memory
= CopyMem (Memory
, Buffer
, AllocationSize
);
959 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
961 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData type with an
962 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
963 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
964 alignment remaining to satisfy the request, then NULL is returned.
965 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
967 @param AllocationSize The number of bytes to allocate.
968 @param Buffer The buffer to copy to the allocated buffer.
969 @param Alignment The requested alignment of the allocation. Must be a power of two.
970 If Alignment is zero, then byte alignment is used.
972 @return A pointer to the allocated buffer or NULL if allocation fails.
977 AllocateAlignedCopyPool (
978 IN UINTN AllocationSize
,
979 IN CONST VOID
*Buffer
,
983 return InternalAllocateAlignedCopyPool (EfiBootServicesData
, AllocationSize
, Buffer
, Alignment
);
987 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
989 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData type with an
990 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
991 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
992 alignment remaining to satisfy the request, then NULL is returned.
993 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
995 @param AllocationSize The number of bytes to allocate.
996 @param Buffer The buffer to copy to the allocated buffer.
997 @param Alignment The requested alignment of the allocation. Must be a power of two.
998 If Alignment is zero, then byte alignment is used.
1000 @return A pointer to the allocated buffer or NULL if allocation fails.
1005 AllocateAlignedRuntimeCopyPool (
1006 IN UINTN AllocationSize
,
1007 IN CONST VOID
*Buffer
,
1011 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData
, AllocationSize
, Buffer
, Alignment
);
1015 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
1017 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType type with an
1018 alignment specified by Alignment. The allocated buffer is returned. If AllocationSize is 0,
1019 then a valid buffer of 0 size is returned. If there is not enough memory at the specified
1020 alignment remaining to satisfy the request, then NULL is returned.
1021 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
1023 @param AllocationSize The number of bytes to allocate.
1024 @param Buffer The buffer to copy to the allocated buffer.
1025 @param Alignment The requested alignment of the allocation. Must be a power of two.
1026 If Alignment is zero, then byte alignment is used.
1028 @return A pointer to the allocated buffer or NULL if allocation fails.
1033 AllocateAlignedReservedCopyPool (
1034 IN UINTN AllocationSize
,
1035 IN CONST VOID
*Buffer
,
1039 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType
, AllocationSize
, Buffer
, Alignment
);
1043 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
1044 in the Memory Allocation Library.
1046 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
1047 aligned pool allocation services of the Memory Allocation Library.
1048 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
1049 Library, then ASSERT().
1051 @param Buffer Pointer to the buffer to free.
1065 // Get the pre-saved original address in the over-allocate pool.
1067 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
1068 RawAddress
= *FreePointer
;
1070 Status
= gBS
->FreePool (RawAddress
);
1071 ASSERT_EFI_ERROR (Status
);