]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c
MdePkg PeiServicesLib: Add PeiServicesFreePages
[mirror_edk2.git] / MdePkg / Library / PeiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
e386b444 1/** @file\r
9e6fa6d2
LG
2 Support routines for memory allocation routines \r
3 based on PeiService for PEI phase drivers.\r
e386b444 4\r
34363594 5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
19388d29 6 This program and the accompanying materials \r
e386b444 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
2fc59a00 9 http://opensource.org/licenses/bsd-license.php. \r
e386b444 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
e386b444 14**/\r
15\r
c892d846 16\r
c7d265a9 17#include <PiPei.h>\r
c892d846 18\r
19\r
c7d265a9 20#include <Library/MemoryAllocationLib.h>\r
1c280088 21#include <Library/PeiServicesLib.h>\r
c7d265a9 22#include <Library/BaseMemoryLib.h>\r
23#include <Library/DebugLib.h>\r
1dde8b08 24#include <Library/HobLib.h>\r
e386b444 25\r
e386b444 26\r
27/**\r
28 Allocates one or more 4KB pages of a certain memory type.\r
29\r
30 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
31 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
32 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
33\r
34 @param MemoryType The type of memory to allocate.\r
35 @param Pages The number of 4 KB pages to allocate.\r
36\r
37 @return A pointer to the allocated buffer or NULL if allocation fails.\r
38\r
39**/\r
40VOID *\r
41InternalAllocatePages (\r
42 IN EFI_MEMORY_TYPE MemoryType, \r
43 IN UINTN Pages\r
44 )\r
45{\r
46 EFI_STATUS Status;\r
47 EFI_PHYSICAL_ADDRESS Memory; \r
e386b444 48\r
49 if (Pages == 0) {\r
50 return NULL;\r
51 }\r
52\r
34363594 53 Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
e386b444 54 if (EFI_ERROR (Status)) {\r
9e6fa6d2 55 return NULL;\r
e386b444 56 }\r
1dde8b08 57\r
e386b444 58 return (VOID *) (UINTN) Memory;\r
59}\r
60\r
61/**\r
62 Allocates one or more 4KB pages of type EfiBootServicesData.\r
63\r
64 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
65 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
66 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
67 returned.\r
68\r
69 @param Pages The number of 4 KB pages to allocate.\r
70\r
71 @return A pointer to the allocated buffer or NULL if allocation fails.\r
72\r
73**/\r
74VOID *\r
75EFIAPI\r
76AllocatePages (\r
77 IN UINTN Pages\r
78 )\r
79{\r
80 return InternalAllocatePages (EfiBootServicesData, Pages);\r
81}\r
82\r
83/**\r
84 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
85\r
86 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
87 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
88 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
89 returned.\r
90\r
91 @param Pages The number of 4 KB pages to allocate.\r
92\r
93 @return A pointer to the allocated buffer or NULL if allocation fails.\r
94\r
95**/\r
96VOID *\r
97EFIAPI\r
98AllocateRuntimePages (\r
99 IN UINTN Pages\r
100 )\r
101{\r
102 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
103}\r
104\r
105/**\r
106 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
107\r
108 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
109 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
110 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
111 returned.\r
112\r
113 @param Pages The number of 4 KB pages to allocate.\r
114\r
115 @return A pointer to the allocated buffer or NULL if allocation fails.\r
116\r
117**/\r
118VOID *\r
119EFIAPI\r
120AllocateReservedPages (\r
121 IN UINTN Pages\r
122 )\r
123{\r
124 return InternalAllocatePages (EfiReservedMemoryType, Pages);\r
125}\r
126\r
127/**\r
128 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
129 functions in the Memory Allocation Library.\r
130\r
131 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
132 must have been allocated on a previous call to the page allocation services of the Memory\r
6e10b70a 133 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 134 perform no actions.\r
9638ba6d 135 \r
e386b444 136 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
137 then ASSERT().\r
138 If Pages is zero, then ASSERT().\r
139 \r
2fc59a00 140 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 141 @param Pages The number of 4 KB pages to free.\r
142\r
143**/\r
144VOID\r
145EFIAPI\r
146FreePages (\r
147 IN VOID *Buffer,\r
148 IN UINTN Pages\r
149 )\r
150{\r
6e10b70a 151 ASSERT (Pages != 0);\r
e386b444 152 //\r
153 // PEI phase does not support to free pages, so leave it as NOP.\r
154 //\r
155}\r
156\r
157/**\r
158 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
159\r
160 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
161 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
162 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
163 NULL is returned.\r
164 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 165 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 166\r
167 @param MemoryType The type of memory to allocate.\r
168 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 169 @param Alignment The requested alignment of the allocation. \r
170 Must be a power of two.\r
e386b444 171 If Alignment is zero, then byte alignment is used.\r
172\r
173 @return A pointer to the allocated buffer or NULL if allocation fails.\r
174\r
175**/\r
176VOID *\r
177InternalAllocateAlignedPages (\r
178 IN EFI_MEMORY_TYPE MemoryType, \r
179 IN UINTN Pages,\r
180 IN UINTN Alignment\r
181 )\r
182{\r
6809c872
LG
183 EFI_PHYSICAL_ADDRESS Memory;\r
184 EFI_PHYSICAL_ADDRESS AlignedMemory;\r
185 EFI_PEI_HOB_POINTERS Hob;\r
4c103c55
LG
186 BOOLEAN SkipBeforeMemHob;\r
187 BOOLEAN SkipAfterMemHob;\r
6809c872
LG
188 EFI_PHYSICAL_ADDRESS HobBaseAddress;\r
189 UINT64 HobLength;\r
190 EFI_MEMORY_TYPE HobMemoryType;\r
191 UINTN TotalPages;\r
e386b444 192\r
193 //\r
194 // Alignment must be a power of two or zero.\r
195 //\r
196 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
197\r
198 if (Pages == 0) {\r
199 return NULL;\r
200 }\r
201 //\r
202 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
203 //\r
6809c872
LG
204 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); \r
205\r
e386b444 206 //\r
207 // We would rather waste some memory to save PEI code size.\r
6809c872
LG
208 // meaning in addition to the requested size for the aligned mem,\r
209 // we simply reserve an overhead memory equal to Alignmemt(page-aligned), no matter what.\r
210 // The overhead mem size could be reduced later with more involved malloc mechanisms\r
211 // (e.g., somthing that can detect the alignment boundary before allocating memory or \r
212 // can request that memory be allocated at a certain address that is aleady aligned).\r
213 //\r
214 TotalPages = Pages + (Alignment <= EFI_PAGE_SIZE ? 0 : EFI_SIZE_TO_PAGES(Alignment));\r
215 Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) InternalAllocatePages (MemoryType, TotalPages);\r
216 if (Memory == 0) {\r
217 DEBUG((DEBUG_INFO, "Out of memory resource! \n"));\r
218 return NULL;\r
219 }\r
220 DEBUG ((DEBUG_INFO, "Allocated Memory unaligned: Address = 0x%LX, Pages = 0x%X, Type = %d \n", Memory, TotalPages, (UINTN) MemoryType));\r
221\r
222 //\r
223 // Alignment calculation\r
224 //\r
225 AlignedMemory = Memory;\r
226 if (Alignment > EFI_PAGE_SIZE) {\r
227 AlignedMemory = ALIGN_VALUE (Memory, Alignment);\r
228 }\r
229 DEBUG ((DEBUG_INFO, "After aligning to 0x%X bytes: Address = 0x%LX, Pages = 0x%X \n", Alignment, AlignedMemory, Pages));\r
230\r
231 //\r
232 // In general three HOBs cover the total allocated space.\r
233 // The aligned portion is covered by the aligned mem HOB and\r
234 // the unaligned(to be freed) portions before and after the aligned portion are covered by newly created HOBs.\r
e386b444 235 //\r
6809c872
LG
236 // Before mem HOB covers the region between "Memory" and "AlignedMemory"\r
237 // Aligned mem HOB covers the region between "AlignedMemory" and "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)"\r
238 // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)"\r
239 //\r
240 // The before or after mem HOBs need to be skipped under special cases where the aligned portion\r
241 // touches either the top or bottom of the original allocated space.\r
242 //\r
4c103c55
LG
243 SkipBeforeMemHob = FALSE;\r
244 SkipAfterMemHob = FALSE;\r
6809c872
LG
245 if (Memory == AlignedMemory) {\r
246 SkipBeforeMemHob = TRUE;\r
247 }\r
248 if ((Memory + EFI_PAGES_TO_SIZE(TotalPages)) == (AlignedMemory + EFI_PAGES_TO_SIZE(Pages))) {\r
249 //\r
250 // This condition is never met in the current implementation.\r
251 // There is always some after-mem since the overhead mem(used in TotalPages)\r
252 // is no less than Alignment.\r
253 //\r
254 SkipAfterMemHob = TRUE;\r
255 }\r
256\r
257 // \r
258 // Search for the mem HOB referring to the original(unaligned) allocation \r
259 // and update the size and type if needed.\r
260 //\r
261 Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r
262 while (Hob.Raw != NULL) {\r
263 if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == Memory) {\r
264 break;\r
265 }\r
266 Hob.Raw = GET_NEXT_HOB (Hob);\r
267 Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r
268 }\r
269 ASSERT (Hob.Raw != NULL);\r
270 if (SkipBeforeMemHob) {\r
271 //\r
272 // Use this HOB as aligned mem HOB as there is no portion before it.\r
273 //\r
274 HobLength = EFI_PAGES_TO_SIZE(Pages);\r
275 Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength;\r
276 } else {\r
277 //\r
278 // Use this HOB as before mem HOB and create a new HOB for the aligned portion \r
279 //\r
280 HobLength = (AlignedMemory - Memory); \r
281 Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength;\r
282 Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiConventionalMemory;\r
283 }\r
284\r
285 HobBaseAddress = Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress;\r
286 HobMemoryType = Hob.MemoryAllocation->AllocDescriptor.MemoryType;\r
287\r
288 //\r
289 // Build the aligned mem HOB if needed\r
290 //\r
291 if (!SkipBeforeMemHob) {\r
292 DEBUG((DEBUG_INFO, "Updated before-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r
293 HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r
294\r
295 HobBaseAddress = AlignedMemory;\r
296 HobLength = EFI_PAGES_TO_SIZE(Pages);\r
297 HobMemoryType = MemoryType;\r
298\r
299 BuildMemoryAllocationHob (\r
300 HobBaseAddress,\r
301 HobLength,\r
302 HobMemoryType\r
303 );\r
304\r
305 DEBUG((DEBUG_INFO, "Created aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r
306 HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r
e386b444 307 } else {\r
6809c872
LG
308 if (HobBaseAddress != 0) {\r
309 DEBUG((DEBUG_INFO, "Updated aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r
310 HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r
311 }\r
e386b444 312 }\r
6809c872
LG
313\r
314\r
315 //\r
316 // Build the after mem HOB if needed\r
317 //\r
318 if (!SkipAfterMemHob) {\r
319 HobBaseAddress = AlignedMemory + EFI_PAGES_TO_SIZE(Pages);\r
320 HobLength = (Memory + EFI_PAGES_TO_SIZE(TotalPages)) - (AlignedMemory + EFI_PAGES_TO_SIZE(Pages));\r
321 HobMemoryType = EfiConventionalMemory;\r
322\r
323 BuildMemoryAllocationHob (\r
324 HobBaseAddress,\r
325 HobLength,\r
326 HobMemoryType\r
327 );\r
328\r
329 DEBUG((DEBUG_INFO, "Created after-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r
330 HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r
331 }\r
332\r
333 return (VOID *) (UINTN) AlignedMemory;\r
e386b444 334}\r
335\r
336/**\r
337 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
338\r
339 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
340 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
341 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
342 request, then NULL is returned.\r
9638ba6d 343 \r
e386b444 344 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 345 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 346\r
347 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 348 @param Alignment The requested alignment of the allocation. \r
349 Must be a power of two.\r
e386b444 350 If Alignment is zero, then byte alignment is used.\r
351\r
352 @return A pointer to the allocated buffer or NULL if allocation fails.\r
353\r
354**/\r
355VOID *\r
356EFIAPI\r
357AllocateAlignedPages (\r
358 IN UINTN Pages,\r
359 IN UINTN Alignment\r
360 )\r
361{\r
362 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r
363}\r
364\r
365/**\r
366 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
367\r
368 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
369 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
370 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
371 request, then NULL is returned.\r
9638ba6d 372 \r
e386b444 373 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 374 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 375\r
376 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 377 @param Alignment The requested alignment of the allocation. \r
378 Must be a power of two.\r
e386b444 379 If Alignment is zero, then byte alignment is used.\r
380\r
381 @return A pointer to the allocated buffer or NULL if allocation fails.\r
382\r
383**/\r
384VOID *\r
385EFIAPI\r
386AllocateAlignedRuntimePages (\r
387 IN UINTN Pages,\r
388 IN UINTN Alignment\r
389 )\r
390{\r
391 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
392}\r
393\r
394/**\r
395 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
396\r
397 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
398 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
399 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
400 request, then NULL is returned.\r
9638ba6d 401 \r
e386b444 402 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
1346352d 403 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
e386b444 404\r
405 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 406 @param Alignment The requested alignment of the allocation. \r
407 Must be a power of two.\r
e386b444 408 If Alignment is zero, then byte alignment is used.\r
409\r
410 @return A pointer to the allocated buffer or NULL if allocation fails.\r
411\r
412**/\r
413VOID *\r
414EFIAPI\r
415AllocateAlignedReservedPages (\r
416 IN UINTN Pages,\r
417 IN UINTN Alignment\r
418 )\r
419{\r
420 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r
421}\r
422\r
423/**\r
424 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
425 allocation functions in the Memory Allocation Library.\r
426\r
427 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
428 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
6e10b70a 429 Allocation Library. If it is not possible to free allocated pages, then this function will \r
446b94b0 430 perform no actions.\r
9638ba6d 431 \r
e386b444 432 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
433 Library, then ASSERT().\r
434 If Pages is zero, then ASSERT().\r
435 \r
2fc59a00 436 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 437 @param Pages The number of 4 KB pages to free.\r
438\r
439**/\r
440VOID\r
441EFIAPI\r
442FreeAlignedPages (\r
443 IN VOID *Buffer,\r
444 IN UINTN Pages\r
445 )\r
446{\r
6e10b70a 447 ASSERT (Pages != 0);\r
e386b444 448 //\r
449 // PEI phase does not support to free pages, so leave it as NOP.\r
450 //\r
451}\r
452\r
453/**\r
454 Allocates a buffer of a certain pool type.\r
455\r
456 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
457 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
458 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
459\r
460 @param MemoryType The type of memory to allocate.\r
461 @param AllocationSize The number of bytes to allocate.\r
462\r
463 @return A pointer to the allocated buffer or NULL if allocation fails.\r
464\r
465**/\r
466VOID *\r
467InternalAllocatePool (\r
468 IN EFI_MEMORY_TYPE MemoryType, \r
469 IN UINTN AllocationSize\r
470 )\r
471{\r
472 //\r
473 // If we need lots of small runtime/reserved memory type from PEI in the future, \r
474 // we can consider providing a more complex algorithm that allocates runtime pages and \r
475 // provide pool allocations from those pages. \r
476 //\r
477 return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));\r
478}\r
479\r
480/**\r
481 Allocates a buffer of type EfiBootServicesData.\r
482\r
483 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
484 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
485 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
486\r
487 @param AllocationSize The number of bytes to allocate.\r
488\r
489 @return A pointer to the allocated buffer or NULL if allocation fails.\r
490\r
491**/\r
492VOID *\r
493EFIAPI\r
494AllocatePool (\r
495 IN UINTN AllocationSize\r
496 )\r
497{\r
498 EFI_STATUS Status;\r
e386b444 499 VOID *Buffer;\r
500 \r
1c280088 501 Status = PeiServicesAllocatePool (AllocationSize, &Buffer);\r
e386b444 502 if (EFI_ERROR (Status)) {\r
503 Buffer = NULL;\r
504 }\r
505 return Buffer;\r
506}\r
507\r
508/**\r
509 Allocates a buffer of type EfiRuntimeServicesData.\r
510\r
511 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
512 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
513 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
514\r
515 @param AllocationSize The number of bytes to allocate.\r
516\r
517 @return A pointer to the allocated buffer or NULL if allocation fails.\r
518\r
519**/\r
520VOID *\r
521EFIAPI\r
522AllocateRuntimePool (\r
523 IN UINTN AllocationSize\r
524 )\r
525{\r
526 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
527}\r
528\r
529/**\r
8789c5e0 530 Allocates a buffer of type EfiReservedMemoryType.\r
e386b444 531\r
8789c5e0 532 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
e386b444 533 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
534 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
535\r
536 @param AllocationSize The number of bytes to allocate.\r
537\r
538 @return A pointer to the allocated buffer or NULL if allocation fails.\r
539\r
540**/\r
541VOID *\r
542EFIAPI\r
543AllocateReservedPool (\r
544 IN UINTN AllocationSize\r
545 )\r
546{\r
547 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
548}\r
549\r
550/**\r
446b94b0 551 Allocates and zeros a buffer of a certain pool type.\r
e386b444 552\r
446b94b0 553 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
e386b444 554 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
555 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
556 then NULL is returned.\r
557\r
558 @param PoolType The type of memory to allocate.\r
559 @param AllocationSize The number of bytes to allocate and zero.\r
560\r
561 @return A pointer to the allocated buffer or NULL if allocation fails.\r
562\r
563**/\r
564VOID *\r
565InternalAllocateZeroPool (\r
566 IN EFI_MEMORY_TYPE PoolType, \r
567 IN UINTN AllocationSize\r
568 ) \r
569{\r
570 VOID *Memory;\r
571\r
572 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
573 if (Memory != NULL) {\r
574 Memory = ZeroMem (Memory, AllocationSize);\r
575 }\r
576 return Memory;\r
577}\r
578\r
579/**\r
580 Allocates and zeros a buffer of type EfiBootServicesData.\r
581\r
582 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
583 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
584 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
585 request, then NULL is returned.\r
586\r
587 @param AllocationSize The number of bytes to allocate and zero.\r
588\r
589 @return A pointer to the allocated buffer or NULL if allocation fails.\r
590\r
591**/\r
592VOID *\r
593EFIAPI\r
594AllocateZeroPool (\r
595 IN UINTN AllocationSize\r
596 )\r
597{\r
598 VOID *Memory;\r
599\r
600 Memory = AllocatePool (AllocationSize);\r
601 if (Memory != NULL) {\r
602 Memory = ZeroMem (Memory, AllocationSize);\r
603 }\r
604 return Memory;\r
605}\r
606\r
607/**\r
608 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
609\r
610 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
611 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
612 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
613 request, then NULL is returned.\r
614\r
615 @param AllocationSize The number of bytes to allocate and zero.\r
616\r
617 @return A pointer to the allocated buffer or NULL if allocation fails.\r
618\r
619**/\r
620VOID *\r
621EFIAPI\r
622AllocateRuntimeZeroPool (\r
623 IN UINTN AllocationSize\r
624 )\r
625{\r
626 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
627}\r
628\r
629/**\r
630 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
631\r
632 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
633 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
634 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
635 request, then NULL is returned.\r
636\r
637 @param AllocationSize The number of bytes to allocate and zero.\r
638\r
639 @return A pointer to the allocated buffer or NULL if allocation fails.\r
640\r
641**/\r
642VOID *\r
643EFIAPI\r
644AllocateReservedZeroPool (\r
645 IN UINTN AllocationSize\r
646 )\r
647{\r
648 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
649}\r
650\r
651/**\r
446b94b0 652 Copies a buffer to an allocated buffer of a certain pool type.\r
e386b444 653\r
446b94b0 654 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
e386b444 655 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
656 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
657 is not enough memory remaining to satisfy the request, then NULL is returned.\r
658 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 659 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 660\r
661 @param PoolType The type of pool to allocate.\r
662 @param AllocationSize The number of bytes to allocate and zero.\r
663 @param Buffer The buffer to copy to the allocated buffer.\r
664\r
665 @return A pointer to the allocated buffer or NULL if allocation fails.\r
666\r
667**/\r
668VOID *\r
669InternalAllocateCopyPool (\r
670 IN EFI_MEMORY_TYPE PoolType, \r
671 IN UINTN AllocationSize,\r
672 IN CONST VOID *Buffer\r
673 ) \r
674{\r
675 VOID *Memory;\r
676\r
677 ASSERT (Buffer != NULL);\r
678 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
679\r
680 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
681 if (Memory != NULL) {\r
682 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
683 }\r
684 return Memory;\r
685} \r
686\r
687/**\r
688 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
689\r
690 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
691 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
692 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
693 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 694 \r
e386b444 695 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 696 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 697\r
698 @param AllocationSize The number of bytes to allocate and zero.\r
699 @param Buffer The buffer to copy to the allocated buffer.\r
700\r
701 @return A pointer to the allocated buffer or NULL if allocation fails.\r
702\r
703**/\r
704VOID *\r
705EFIAPI\r
706AllocateCopyPool (\r
707 IN UINTN AllocationSize,\r
708 IN CONST VOID *Buffer\r
709 )\r
710{\r
711 VOID *Memory;\r
712\r
713 ASSERT (Buffer != NULL);\r
714 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
715\r
716 Memory = AllocatePool (AllocationSize);\r
717 if (Memory != NULL) {\r
718 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
719 }\r
720 return Memory;\r
721}\r
722\r
723/**\r
724 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
725\r
726 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
727 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
728 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
729 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 730 \r
e386b444 731 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 732 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 733\r
734 @param AllocationSize The number of bytes to allocate and zero.\r
735 @param Buffer The buffer to copy to the allocated buffer.\r
736\r
737 @return A pointer to the allocated buffer or NULL if allocation fails.\r
738\r
739**/\r
740VOID *\r
741EFIAPI\r
742AllocateRuntimeCopyPool (\r
743 IN UINTN AllocationSize,\r
744 IN CONST VOID *Buffer\r
745 )\r
746{\r
747 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
748}\r
749\r
750/**\r
751 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
752\r
753 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
754 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
755 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
756 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 757 \r
e386b444 758 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 759 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 760\r
761 @param AllocationSize The number of bytes to allocate and zero.\r
762 @param Buffer The buffer to copy to the allocated buffer.\r
763\r
764 @return A pointer to the allocated buffer or NULL if allocation fails.\r
765\r
766**/\r
767VOID *\r
768EFIAPI\r
769AllocateReservedCopyPool (\r
770 IN UINTN AllocationSize,\r
771 IN CONST VOID *Buffer\r
772 )\r
773{\r
774 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
775}\r
776\r
3b6a71fc 777/**\r
778 Reallocates a buffer of a specified memory type.\r
779\r
780 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
781 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r
782 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
783 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
784 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
785 enough memory remaining to satisfy the request, then NULL is returned.\r
786 \r
56304569 787 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
788 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 789\r
790 @param PoolType The type of pool to allocate.\r
791 @param OldSize The size, in bytes, of OldBuffer.\r
792 @param NewSize The size, in bytes, of the buffer to reallocate.\r
58380e9c 793 @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
794 optional parameter that may be NULL.\r
3b6a71fc 795\r
796 @return A pointer to the allocated buffer or NULL if allocation fails.\r
797\r
798**/\r
799VOID *\r
3b6a71fc 800InternalReallocatePool (\r
801 IN EFI_MEMORY_TYPE PoolType, \r
802 IN UINTN OldSize,\r
803 IN UINTN NewSize,\r
804 IN VOID *OldBuffer OPTIONAL\r
805 )\r
806{\r
807 VOID *NewBuffer;\r
808\r
6babbe1f 809 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
3b6a71fc 810 if (NewBuffer != NULL && OldBuffer != NULL) {\r
811 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
812 FreePool (OldBuffer);\r
813 }\r
814 return NewBuffer;\r
815}\r
816\r
817/**\r
818 Reallocates a buffer of type EfiBootServicesData.\r
819\r
820 Allocates and zeros the number bytes specified by NewSize from memory of type\r
821 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
822 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
823 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
824 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
825 enough memory remaining to satisfy the request, then NULL is returned.\r
826 \r
56304569 827 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
828 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 829\r
830 @param OldSize The size, in bytes, of OldBuffer.\r
831 @param NewSize The size, in bytes, of the buffer to reallocate.\r
832 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
833 parameter that may be NULL.\r
834\r
835 @return A pointer to the allocated buffer or NULL if allocation fails.\r
836\r
837**/\r
838VOID *\r
839EFIAPI\r
840ReallocatePool (\r
841 IN UINTN OldSize,\r
842 IN UINTN NewSize,\r
843 IN VOID *OldBuffer OPTIONAL\r
844 )\r
845{\r
846 return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r
847}\r
848\r
849/**\r
850 Reallocates a buffer of type EfiRuntimeServicesData.\r
851\r
852 Allocates and zeros the number bytes specified by NewSize from memory of type\r
853 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
854 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
855 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
856 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
857 enough memory remaining to satisfy the request, then NULL is returned.\r
858\r
56304569 859 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
860 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 861\r
862 @param OldSize The size, in bytes, of OldBuffer.\r
863 @param NewSize The size, in bytes, of the buffer to reallocate.\r
864 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
865 parameter that may be NULL.\r
866\r
867 @return A pointer to the allocated buffer or NULL if allocation fails.\r
868\r
869**/\r
870VOID *\r
871EFIAPI\r
872ReallocateRuntimePool (\r
873 IN UINTN OldSize,\r
874 IN UINTN NewSize,\r
875 IN VOID *OldBuffer OPTIONAL\r
876 )\r
877{\r
878 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
879}\r
880\r
881/**\r
882 Reallocates a buffer of type EfiReservedMemoryType.\r
883\r
884 Allocates and zeros the number bytes specified by NewSize from memory of type\r
885 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
886 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
887 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
888 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
889 enough memory remaining to satisfy the request, then NULL is returned.\r
2297186d 890\r
56304569 891 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
892 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 893\r
894 @param OldSize The size, in bytes, of OldBuffer.\r
895 @param NewSize The size, in bytes, of the buffer to reallocate.\r
58380e9c 896 @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
897 optional parameter that may be NULL.\r
3b6a71fc 898\r
899 @return A pointer to the allocated buffer or NULL if allocation fails.\r
900\r
901**/\r
902VOID *\r
903EFIAPI\r
904ReallocateReservedPool (\r
905 IN UINTN OldSize,\r
906 IN UINTN NewSize,\r
907 IN VOID *OldBuffer OPTIONAL\r
908 )\r
909{\r
910 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
911}\r
912\r
e386b444 913/**\r
914 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
915 Memory Allocation Library.\r
916\r
917 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
6e10b70a 918 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
446b94b0 919 resources, then this function will perform no actions.\r
9638ba6d 920 \r
e386b444 921 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
922 then ASSERT().\r
923\r
2fc59a00 924 @param Buffer The pointer to the buffer to free.\r
e386b444 925\r
926**/\r
927VOID\r
928EFIAPI\r
929FreePool (\r
930 IN VOID *Buffer\r
931 )\r
932{\r
933 //\r
934 // PEI phase does not support to free pool, so leave it as NOP.\r
935 //\r
936}\r
937\r
7d582d6b 938\r