4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include "UefiPayloadEntry.h"
14 Allocates one or more pages of type EfiBootServicesData.
16 Allocates the number of pages of MemoryType and returns a pointer to the
17 allocated buffer. The buffer returned is aligned on a 4KB boundary.
18 If Pages is 0, then NULL is returned.
19 If there is not enough memory availble to satisfy the request, then NULL
22 @param Pages The number of 4 KB pages to allocate.
23 @return A pointer to the allocated buffer or NULL if allocation fails.
31 EFI_PEI_HOB_POINTERS Hob
;
32 EFI_PHYSICAL_ADDRESS Offset
;
33 EFI_HOB_HANDOFF_INFO_TABLE
*HobTable
;
35 Hob
.Raw
= GetHobList ();
36 HobTable
= Hob
.HandoffInformationTable
;
42 // Make sure allocation address is page alligned.
43 Offset
= HobTable
->EfiFreeMemoryTop
& EFI_PAGE_MASK
;
45 HobTable
->EfiFreeMemoryTop
-= Offset
;
49 // Check available memory for the allocation
51 if (HobTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) < HobTable
->EfiFreeMemoryBottom
) {
55 HobTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
56 BuildMemoryAllocationHob (HobTable
->EfiFreeMemoryTop
, Pages
* EFI_PAGE_SIZE
, EfiBootServicesData
);
58 return (VOID
*)(UINTN
)HobTable
->EfiFreeMemoryTop
;
62 Frees one or more 4KB pages that were previously allocated with one of the page allocation
63 functions in the Memory Allocation Library.
65 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
66 must have been allocated on a previous call to the page allocation services of the Memory
67 Allocation Library. If it is not possible to free allocated pages, then this function will
70 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
72 If Pages is zero, then ASSERT().
74 @param Buffer Pointer to the buffer of pages to free.
75 @param Pages The number of 4 KB pages to free.
88 Allocates one or more pages of type EfiBootServicesData at a specified alignment.
90 Allocates the number of pages specified by Pages of type EfiBootServicesData with an
91 alignment specified by Alignment.
92 If Pages is 0, then NULL is returned.
93 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
94 If there is no enough memory at the specified alignment available to satisfy the
95 request, then NULL is returned.
97 @param Pages The number of 4 KB pages to allocate.
98 @param Alignment The requested alignment of the allocation.
100 @return A pointer to the allocated buffer or NULL if allocation fails.
104 AllocateAlignedPages (
113 // Alignment must be a power of two or zero.
115 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
124 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
126 Memory
= (VOID
*)(UINTN
)AllocatePages (Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
127 if (Memory
== NULL
) {
131 if (Alignment
== 0) {
132 AlignmentMask
= Alignment
;
134 AlignmentMask
= Alignment
- 1;
137 return (VOID
*)(UINTN
)(((UINTN
)Memory
+ AlignmentMask
) & ~AlignmentMask
);
141 Allocates a buffer of type EfiBootServicesData.
143 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
144 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
145 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
147 @param AllocationSize The number of bytes to allocate.
149 @return A pointer to the allocated buffer or NULL if allocation fails.
155 IN UINTN AllocationSize
158 EFI_HOB_MEMORY_POOL
*Hob
;
160 if (AllocationSize
> 0x4000) {
161 // Please use AllocatePages for big allocations
165 Hob
= (EFI_HOB_MEMORY_POOL
*)CreateHob (EFI_HOB_TYPE_MEMORY_POOL
, (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) + AllocationSize
));
166 return (VOID
*)(Hob
+ 1);
170 Allocates and zeros a buffer of type EfiBootServicesData.
172 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
173 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
174 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
175 request, then NULL is returned.
177 @param AllocationSize The number of bytes to allocate and zero.
179 @return A pointer to the allocated buffer or NULL if allocation fails.
185 IN UINTN AllocationSize
190 Buffer
= AllocatePool (AllocationSize
);
191 if (Buffer
== NULL
) {
195 ZeroMem (Buffer
, AllocationSize
);