TianoCore EDK2 master
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
30VOID *
31EFIAPI
33 IN UINTN Pages
34 )
35{
38
39 Hob.Raw = GetHobList ();
40
41 // Check to see if on 4k boundary
42 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
43 if (Offset != 0) {
44 // If not aligned, make the allocation aligned.
45 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
46 }
47
48 //
49 // Verify that there is sufficient memory to satisfy the allocation
50 //
51 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
52 return 0;
53 } else {
54 //
55 // Update the PHIT to reflect the memory usage
56 //
57 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
58
59 // This routine used to create a memory allocation HOB a la PEI, but that's not
60 // necessary for us.
61
62 //
63 // Create a memory allocation HOB.
64 //
66 Hob.HandoffInformationTable->EfiFreeMemoryTop,
67 Pages * EFI_PAGE_SIZE,
69 );
70 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
71 }
72}
73
90VOID *
91EFIAPI
93 IN UINTN Pages,
94 IN UINTN Alignment
95 )
96{
97 VOID *Memory;
98 UINTN AlignmentMask;
99
100 //
101 // Alignment must be a power of two or zero.
102 //
103 ASSERT ((Alignment & (Alignment - 1)) == 0);
104
105 if (Pages == 0) {
106 return NULL;
107 }
108
109 //
110 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
111 //
112 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
113 //
114 // We would rather waste some memory to save PEI code size.
115 //
116 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
117 if (Alignment == 0) {
118 AlignmentMask = Alignment;
119 } else {
120 AlignmentMask = Alignment - 1;
121 }
122
123 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
124}
125
143VOID
144EFIAPI
146 IN VOID *Buffer,
147 IN UINTN Pages
148 )
149{
150 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
151 // The allocated memory is lost.
152}
153
166VOID *
167EFIAPI
169 IN UINTN AllocationSize
170 )
171{
173
174 Hob = GetHobList ();
175
176 //
177 // Verify that there is sufficient memory to satisfy the allocation
178 //
179 if (AllocationSize > 0x10000) {
180 // Please call AllocatePages for big allocations
181 return 0;
182 } else {
184 EFI_HOB_TYPE_MEMORY_POOL,
185 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) +
186 AllocationSize)
187 );
188 return (VOID *)(Hob + 1);
189 }
190}
191
205VOID *
206EFIAPI
208 IN UINTN AllocationSize
209 )
210{
211 VOID *Buffer;
212
213 Buffer = AllocatePool (AllocationSize);
214 if (Buffer == NULL) {
215 return NULL;
216 }
217
218 ZeroMem (Buffer, AllocationSize);
219
220 return Buffer;
221}
222
237VOID
238EFIAPI
240 IN VOID *Buffer
241 )
242{
243 // Not implemented yet
244}
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
#define NULL
Definition: Base.h:312
#define IN
Definition: Base.h:279
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
#define ASSERT(Expression)
Definition: DebugLib.h:391
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 FreePool(IN VOID *Buffer)
VOID * CreateHob(IN UINT16 HobType, IN UINT16 HobLenght)
Definition: Hob.c:101
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:49
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:197
@ EfiBootServicesData
EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom
Definition: PiHob.h:92
EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
Definition: PiHob.h:88