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