TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryAllocationLib.c
Go to the documentation of this file.
1
10#include <PiPei.h>
11
12#include <Library/BaseLib.h>
14#include <Library/PrePiLib.h>
15#include <Library/DebugLib.h>
16
18VOID *
19EFIAPI
20InternalAllocatePages (
21 IN UINTN Pages,
22 IN EFI_MEMORY_TYPE MemoryType
23 )
24{
27
28 Hob.Raw = GetHobList ();
29
30 NewTop = Hob.HandoffInformationTable->EfiFreeMemoryTop & ~(EFI_PHYSICAL_ADDRESS)EFI_PAGE_MASK;
31 NewTop -= Pages * EFI_PAGE_SIZE;
32
33 //
34 // Verify that there is sufficient memory to satisfy the allocation
35 //
36 if (NewTop < (Hob.HandoffInformationTable->EfiFreeMemoryBottom + sizeof (EFI_HOB_MEMORY_ALLOCATION))) {
37 return NULL;
38 }
39
40 //
41 // Update the PHIT to reflect the memory usage
42 //
43 Hob.HandoffInformationTable->EfiFreeMemoryTop = NewTop;
44
45 //
46 // Create a memory allocation HOB.
47 //
49 Hob.HandoffInformationTable->EfiFreeMemoryTop,
50 Pages * EFI_PAGE_SIZE,
51 MemoryType
52 );
53
54 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
55}
56
70VOID *
71EFIAPI
73 IN UINTN Pages
74 )
75{
76 return InternalAllocatePages (Pages, EfiBootServicesData);
77}
78
92VOID *
93EFIAPI
95 IN UINTN Pages
96 )
97{
98 return InternalAllocatePages (Pages, EfiRuntimeServicesData);
99}
100
117VOID *
118EFIAPI
120 IN UINTN Pages,
121 IN UINTN Alignment
122 )
123{
124 VOID *Memory;
125 UINTN AlignmentMask;
126
127 //
128 // Alignment must be a power of two or zero.
129 //
130 ASSERT ((Alignment & (Alignment - 1)) == 0);
131
132 if (Pages == 0) {
133 return NULL;
134 }
135
136 //
137 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
138 //
139 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
140 //
141 // We would rather waste some memory to save PEI code size.
142 //
143 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
144 if (Alignment == 0) {
145 AlignmentMask = Alignment;
146 } else {
147 AlignmentMask = Alignment - 1;
148 }
149
150 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
151}
152
170VOID
171EFIAPI
173 IN VOID *Buffer,
174 IN UINTN Pages
175 )
176{
177 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
178 // The allocated memory is lost.
179}
180
193VOID *
194EFIAPI
196 IN UINTN AllocationSize
197 )
198{
200
201 Hob = GetHobList ();
202
203 //
204 // Verify that there is sufficient memory to satisfy the allocation
205 //
206 if (AllocationSize > 0x10000) {
207 // Please call AllocatePages for big allocations
208 return 0;
209 } else {
211 EFI_HOB_TYPE_MEMORY_POOL,
212 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) +
213 AllocationSize)
214 );
215 return (VOID *)(Hob + 1);
216 }
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}
249
264VOID
265EFIAPI
267 IN VOID *Buffer
268 )
269{
270 // Not implemented yet
271}
272
294VOID *
295EFIAPI
297 IN UINTN OldSize,
298 IN UINTN NewSize,
299 IN VOID *OldBuffer OPTIONAL
300 )
301{
302 VOID *NewBuffer;
303
304 // Validate the OldBuffer is HobAllocated.
306 EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
307
308 if (OldBuffer != NULL) {
309 HandOffHob = GetHobList ();
310 ASSERT (((EFI_PHYSICAL_ADDRESS)(UINTN)OldBuffer >= HandOffHob->EfiMemoryBottom));
311 ASSERT (((EFI_PHYSICAL_ADDRESS)((UINTN)OldBuffer + OldSize) <= HandOffHob->EfiFreeMemoryBottom));
312 }
313
315
316 // If new buffer would be smaller just return old buffer as FreePool isn't supported.
317 if ((OldBuffer != NULL) && (OldSize >= NewSize)) {
318 return OldBuffer;
319 }
320
321 NewBuffer = AllocateZeroPool (NewSize);
322 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
323 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
324 FreePool (OldBuffer);
325 }
326
327 return NewBuffer;
328}
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 CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
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 ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateRuntimePages(IN UINTN Pages)
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define MIN(a, b)
Definition: Base.h:1007
#define IN
Definition: Base.h:279
#define DEBUG_CODE_BEGIN()
Definition: DebugLib.h:564
#define DEBUG_CODE_END()
Definition: DebugLib.h:578
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
@ EfiRuntimeServicesData
EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom
Definition: PiHob.h:92
EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
Definition: PiHob.h:88
EFI_PHYSICAL_ADDRESS EfiMemoryBottom
Definition: PiHob.h:83