]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
EmbeddedPkg: Fix various typos
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
a6caee65 1/** @file\r
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
878b807a 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
a6caee65 7\r
8**/\r
9\r
10#include <PiPei.h>\r
11\r
12#include <Library/BaseLib.h>\r
864cba95 13#include <Library/BaseMemoryLib.h>\r
a6caee65 14#include <Library/PrePiLib.h>\r
15#include <Library/DebugLib.h>\r
16\r
17\r
18\r
19/**\r
20 Allocates one or more 4KB pages of type EfiBootServicesData.\r
21\r
22 Allocates the number of 4KB pages of MemoryType and returns a pointer to the\r
23 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
24 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
25 returned.\r
26\r
27 @param Pages The number of 4 KB pages to allocate.\r
28\r
29 @return A pointer to the allocated buffer or NULL if allocation fails.\r
30\r
31**/\r
32VOID *\r
33EFIAPI\r
34AllocatePages (\r
35 IN UINTN Pages\r
36 )\r
37{\r
38 EFI_PEI_HOB_POINTERS Hob;\r
39 EFI_PHYSICAL_ADDRESS Offset;\r
40\r
41 Hob.Raw = GetHobList ();\r
42\r
43 // Check to see if on 4k boundary\r
44 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
45 if (Offset != 0) {\r
46 // If not aligned, make the allocation aligned.\r
47 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
48 }\r
49\r
50 //\r
51 // Verify that there is sufficient memory to satisfy the allocation\r
52 //\r
53 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\r
54 return 0;\r
55 } else {\r
56 //\r
57 // Update the PHIT to reflect the memory usage\r
58 //\r
59 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
60\r
61 // This routine used to create a memory allocation HOB a la PEI, but that's not\r
62 // necessary for us.\r
63\r
64 //\r
65 // Create a memory allocation HOB.\r
66 //\r
67 BuildMemoryAllocationHob (\r
68 Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
69 Pages * EFI_PAGE_SIZE,\r
70 EfiBootServicesData\r
71 );\r
72 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
73 }\r
74}\r
75\r
76\r
77/**\r
78 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
79\r
80 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
81 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
82 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
83 request, then NULL is returned.\r
84 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
85\r
86 @param Pages The number of 4 KB pages to allocate.\r
87 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
88 If Alignment is zero, then byte alignment is used.\r
89\r
90 @return A pointer to the allocated buffer or NULL if allocation fails.\r
91\r
92**/\r
93VOID *\r
94EFIAPI\r
95AllocateAlignedPages (\r
96 IN UINTN Pages,\r
97 IN UINTN Alignment\r
98 )\r
99{\r
100 VOID *Memory;\r
101 UINTN AlignmentMask;\r
102\r
103 //\r
104 // Alignment must be a power of two or zero.\r
105 //\r
106 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
107\r
108 if (Pages == 0) {\r
109 return NULL;\r
110 }\r
111 //\r
112 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
113 //\r
114 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
115 //\r
116 // We would rather waste some memory to save PEI code size.\r
117 //\r
118 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
119 if (Alignment == 0) {\r
120 AlignmentMask = Alignment;\r
121 } else {\r
122 AlignmentMask = Alignment - 1;\r
123 }\r
124 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
125}\r
126\r
127\r
0b342ffb
OM
128/**\r
129 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
130 functions in the Memory Allocation Library.\r
131\r
132 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
133 must have been allocated on a previous call to the page allocation services of the Memory\r
134 Allocation Library. If it is not possible to free allocated pages, then this function will\r
135 perform no actions.\r
136\r
137 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
138 then ASSERT().\r
139 If Pages is zero, then ASSERT().\r
a6caee65 140\r
0b342ffb
OM
141 @param Buffer Pointer to the buffer of pages to free.\r
142 @param Pages The number of 4 KB pages to free.\r
143\r
144**/\r
145VOID\r
146EFIAPI\r
147FreePages (\r
148 IN VOID *Buffer,\r
149 IN UINTN Pages\r
150 )\r
151{\r
152 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.\r
153 // The allocated memory is lost.\r
154}\r
a6caee65 155\r
156/**\r
157 Allocates a buffer of type EfiBootServicesData.\r
158\r
159 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
160 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
161 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
162\r
163 @param AllocationSize The number of bytes to allocate.\r
164\r
165 @return A pointer to the allocated buffer or NULL if allocation fails.\r
166\r
167**/\r
168VOID *\r
169EFIAPI\r
170AllocatePool (\r
171 IN UINTN AllocationSize\r
172 )\r
173{\r
174 EFI_HOB_MEMORY_POOL *Hob;\r
175\r
176 Hob = GetHobList ();\r
177\r
3402aac7 178\r
a6caee65 179 //\r
180 // Verify that there is sufficient memory to satisfy the allocation\r
181 //\r
182 if (AllocationSize > 0x10000) {\r
c6a72cd7 183 // Please call AllocatePages for big allocations\r
a6caee65 184 return 0;\r
185 } else {\r
186\r
187 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));\r
188 return (VOID *)(Hob + 1);\r
189 }\r
190}\r
191\r
864cba95
JB
192/**\r
193 Allocates and zeros a buffer of type EfiBootServicesData.\r
194\r
195 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
196 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
197 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
198 request, then NULL is returned.\r
199\r
200 @param AllocationSize The number of bytes to allocate and zero.\r
201\r
202 @return A pointer to the allocated buffer or NULL if allocation fails.\r
203\r
204**/\r
205VOID *\r
206EFIAPI\r
207AllocateZeroPool (\r
208 IN UINTN AllocationSize\r
209 )\r
210{\r
211 VOID *Buffer;\r
212\r
213 Buffer = AllocatePool (AllocationSize);\r
214 if (Buffer == NULL) {\r
215 return NULL;\r
216 }\r
217\r
218 ZeroMem (Buffer, AllocationSize);\r
219\r
220 return Buffer;\r
221}\r
222\r
a6caee65 223/**\r
224 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
225 Memory Allocation Library.\r
226\r
227 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
228 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
229 resources, then this function will perform no actions.\r
230\r
231 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
232 then ASSERT().\r
233\r
234 @param Buffer Pointer to the buffer to free.\r
235\r
236**/\r
237VOID\r
238EFIAPI\r
239FreePool (\r
240 IN VOID *Buffer\r
241 )\r
242{\r
243 // Not implemented yet\r
244}\r