TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryAllocation.c
Go to the documentation of this file.
1
11#include "UefiPayloadEntry.h"
12
26VOID *
27EFIAPI
29 IN UINTN Pages,
30 IN EFI_MEMORY_TYPE MemoryType
31 )
32{
36
37 Hob.Raw = GetHobList ();
38 HobTable = Hob.HandoffInformationTable;
39
40 if (Pages == 0) {
41 return NULL;
42 }
43
44 // Make sure allocation address is page alligned.
45 Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
46 if (Offset != 0) {
47 HobTable->EfiFreeMemoryTop -= Offset;
48 }
49
50 //
51 // Check available memory for the allocation
52 //
53 if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
54 return NULL;
55 }
56
57 HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
58 BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, MemoryType);
59
60 return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
61}
62
75VOID *
76EFIAPI
78 IN UINTN Pages
79 )
80{
84
85 Hob.Raw = GetHobList ();
86 HobTable = Hob.HandoffInformationTable;
87
88 if (Pages == 0) {
89 return NULL;
90 }
91
92 // Make sure allocation address is page aligned.
93 Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
94 if (Offset != 0) {
95 HobTable->EfiFreeMemoryTop -= Offset;
96 }
97
98 //
99 // Check available memory for the allocation
100 //
101 if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
102 return NULL;
103 }
104
105 HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
106 BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, EfiBootServicesData);
107
108 return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
109}
110
128VOID
129EFIAPI
131 IN VOID *Buffer,
132 IN UINTN Pages
133 )
134{
135}
136
152VOID *
153EFIAPI
155 IN UINTN Pages,
156 IN UINTN Alignment
157 )
158{
159 VOID *Memory;
160 UINTN AlignmentMask;
161
162 //
163 // Alignment must be a power of two or zero.
164 //
165 ASSERT ((Alignment & (Alignment - 1)) == 0);
166
167 if (Pages == 0) {
168 return NULL;
169 }
170
171 //
172 // Check overflow.
173 //
174 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
175
176 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
177 if (Memory == NULL) {
178 return NULL;
179 }
180
181 if (Alignment == 0) {
182 AlignmentMask = Alignment;
183 } else {
184 AlignmentMask = Alignment - 1;
185 }
186
187 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
188}
189
202VOID *
203EFIAPI
205 IN UINTN AllocationSize
206 )
207{
209
210 if (AllocationSize > 0x4000) {
211 // Please use AllocatePages for big allocations
212 return NULL;
213 }
214
215 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + AllocationSize));
216 return (VOID *)(Hob + 1);
217}
218
232VOID *
233EFIAPI
235 IN UINTN AllocationSize
236 )
237{
238 VOID *Buffer;
239
240 Buffer = AllocatePool (AllocationSize);
241 if (Buffer == NULL) {
242 return NULL;
243 }
244
245 ZeroMem (Buffer, AllocationSize);
246
247 return Buffer;
248}
UINT64 UINTN
#define MAX_ADDRESS
VOID EFIAPI BuildMemoryAllocationHob(IN EFI_PHYSICAL_ADDRESS BaseAddress, IN UINT64 Length, IN EFI_MEMORY_TYPE MemoryType)
Definition: HobLib.c:601
VOID *EFIAPI GetHobList(VOID)
Definition: HobLib.c:76
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define IN
Definition: Base.h:279
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateAlignedPages(IN UINTN Pages, IN UINTN Alignment)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID *EFIAPI PayloadAllocatePages(IN UINTN Pages, IN EFI_MEMORY_TYPE MemoryType)
VOID * CreateHob(IN UINT16 HobType, IN UINT16 HobLenght)
Definition: Hob.c:101
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
EFI_MEMORY_TYPE
@ EfiBootServicesData
EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom
Definition: PiHob.h:92
EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
Definition: PiHob.h:88