]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c
1. Add defines for MAX values for UEFI data types.
[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
58380e9c 5 Copyright (c) 2006 - 2010, 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
e386b444 24\r
e386b444 25\r
26/**\r
27 Allocates one or more 4KB pages of a certain memory type.\r
28\r
29 Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r
30 buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r
31 If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
32\r
33 @param MemoryType The type of memory to allocate.\r
34 @param Pages The number of 4 KB pages to allocate.\r
35\r
36 @return A pointer to the allocated buffer or NULL if allocation fails.\r
37\r
38**/\r
39VOID *\r
40InternalAllocatePages (\r
41 IN EFI_MEMORY_TYPE MemoryType, \r
42 IN UINTN Pages\r
43 )\r
44{\r
45 EFI_STATUS Status;\r
46 EFI_PHYSICAL_ADDRESS Memory; \r
e386b444 47\r
48 if (Pages == 0) {\r
49 return NULL;\r
50 }\r
51\r
1c280088 52 Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r
e386b444 53 if (EFI_ERROR (Status)) {\r
9e6fa6d2 54 return NULL;\r
e386b444 55 }\r
56 return (VOID *) (UINTN) Memory;\r
57}\r
58\r
59/**\r
60 Allocates one or more 4KB pages of type EfiBootServicesData.\r
61\r
62 Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r
63 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
64 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
65 returned.\r
66\r
67 @param Pages The number of 4 KB pages to allocate.\r
68\r
69 @return A pointer to the allocated buffer or NULL if allocation fails.\r
70\r
71**/\r
72VOID *\r
73EFIAPI\r
74AllocatePages (\r
75 IN UINTN Pages\r
76 )\r
77{\r
78 return InternalAllocatePages (EfiBootServicesData, Pages);\r
79}\r
80\r
81/**\r
82 Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r
83\r
84 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r
85 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
86 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
87 returned.\r
88\r
89 @param Pages The number of 4 KB pages to allocate.\r
90\r
91 @return A pointer to the allocated buffer or NULL if allocation fails.\r
92\r
93**/\r
94VOID *\r
95EFIAPI\r
96AllocateRuntimePages (\r
97 IN UINTN Pages\r
98 )\r
99{\r
100 return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r
101}\r
102\r
103/**\r
104 Allocates one or more 4KB pages of type EfiReservedMemoryType.\r
105\r
106 Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r
107 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
108 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
109 returned.\r
110\r
111 @param Pages The number of 4 KB pages to allocate.\r
112\r
113 @return A pointer to the allocated buffer or NULL if allocation fails.\r
114\r
115**/\r
116VOID *\r
117EFIAPI\r
118AllocateReservedPages (\r
119 IN UINTN Pages\r
120 )\r
121{\r
122 return InternalAllocatePages (EfiReservedMemoryType, Pages);\r
123}\r
124\r
125/**\r
126 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
127 functions in the Memory Allocation Library.\r
128\r
129 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
130 must have been allocated on a previous call to the page allocation services of the Memory\r
6e10b70a 131 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 132 perform no actions.\r
9638ba6d 133 \r
e386b444 134 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
135 then ASSERT().\r
136 If Pages is zero, then ASSERT().\r
137 \r
2fc59a00 138 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 139 @param Pages The number of 4 KB pages to free.\r
140\r
141**/\r
142VOID\r
143EFIAPI\r
144FreePages (\r
145 IN VOID *Buffer,\r
146 IN UINTN Pages\r
147 )\r
148{\r
6e10b70a 149 ASSERT (Pages != 0);\r
e386b444 150 //\r
151 // PEI phase does not support to free pages, so leave it as NOP.\r
152 //\r
153}\r
154\r
155/**\r
156 Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r
157\r
158 Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r
159 specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r
160 If there is not enough memory at the specified alignment remaining to satisfy the request, then\r
161 NULL is returned.\r
162 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
163\r
164 @param MemoryType The type of memory to allocate.\r
165 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 166 @param Alignment The requested alignment of the allocation. \r
167 Must be a power of two.\r
e386b444 168 If Alignment is zero, then byte alignment is used.\r
169\r
170 @return A pointer to the allocated buffer or NULL if allocation fails.\r
171\r
172**/\r
173VOID *\r
174InternalAllocateAlignedPages (\r
175 IN EFI_MEMORY_TYPE MemoryType, \r
176 IN UINTN Pages,\r
177 IN UINTN Alignment\r
178 )\r
179{\r
180 VOID *Memory;\r
181 UINTN AlignmentMask;\r
182\r
183 //\r
184 // Alignment must be a power of two or zero.\r
185 //\r
186 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
187\r
188 if (Pages == 0) {\r
189 return NULL;\r
190 }\r
191 //\r
192 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
193 //\r
194 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
195 //\r
196 // We would rather waste some memory to save PEI code size.\r
197 //\r
198 Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));\r
199 if (Alignment == 0) {\r
200 AlignmentMask = Alignment;\r
201 } else {\r
202 AlignmentMask = Alignment - 1; \r
203 }\r
204 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
205}\r
206\r
207/**\r
208 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
209\r
210 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
211 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
212 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
213 request, then NULL is returned.\r
9638ba6d 214 \r
e386b444 215 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
216\r
217 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 218 @param Alignment The requested alignment of the allocation. \r
219 Must be a power of two.\r
e386b444 220 If Alignment is zero, then byte alignment is used.\r
221\r
222 @return A pointer to the allocated buffer or NULL if allocation fails.\r
223\r
224**/\r
225VOID *\r
226EFIAPI\r
227AllocateAlignedPages (\r
228 IN UINTN Pages,\r
229 IN UINTN Alignment\r
230 )\r
231{\r
232 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r
233}\r
234\r
235/**\r
236 Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r
237\r
238 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r
239 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
240 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
241 request, then NULL is returned.\r
9638ba6d 242 \r
e386b444 243 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
244\r
245 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 246 @param Alignment The requested alignment of the allocation. \r
247 Must be a power of two.\r
e386b444 248 If Alignment is zero, then byte alignment is used.\r
249\r
250 @return A pointer to the allocated buffer or NULL if allocation fails.\r
251\r
252**/\r
253VOID *\r
254EFIAPI\r
255AllocateAlignedRuntimePages (\r
256 IN UINTN Pages,\r
257 IN UINTN Alignment\r
258 )\r
259{\r
260 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r
261}\r
262\r
263/**\r
264 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r
265\r
266 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r
267 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
268 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
269 request, then NULL is returned.\r
9638ba6d 270 \r
e386b444 271 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
272\r
273 @param Pages The number of 4 KB pages to allocate.\r
58380e9c 274 @param Alignment The requested alignment of the allocation. \r
275 Must be a power of two.\r
e386b444 276 If Alignment is zero, then byte alignment is used.\r
277\r
278 @return A pointer to the allocated buffer or NULL if allocation fails.\r
279\r
280**/\r
281VOID *\r
282EFIAPI\r
283AllocateAlignedReservedPages (\r
284 IN UINTN Pages,\r
285 IN UINTN Alignment\r
286 )\r
287{\r
288 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r
289}\r
290\r
291/**\r
292 Frees one or more 4KB pages that were previously allocated with one of the aligned page\r
293 allocation functions in the Memory Allocation Library.\r
294\r
295 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
296 must have been allocated on a previous call to the aligned page allocation services of the Memory\r
6e10b70a 297 Allocation Library. If it is not possible to free allocated pages, then this function will \r
446b94b0 298 perform no actions.\r
9638ba6d 299 \r
e386b444 300 If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r
301 Library, then ASSERT().\r
302 If Pages is zero, then ASSERT().\r
303 \r
2fc59a00 304 @param Buffer The pointer to the buffer of pages to free.\r
e386b444 305 @param Pages The number of 4 KB pages to free.\r
306\r
307**/\r
308VOID\r
309EFIAPI\r
310FreeAlignedPages (\r
311 IN VOID *Buffer,\r
312 IN UINTN Pages\r
313 )\r
314{\r
6e10b70a 315 ASSERT (Pages != 0);\r
e386b444 316 //\r
317 // PEI phase does not support to free pages, so leave it as NOP.\r
318 //\r
319}\r
320\r
321/**\r
322 Allocates a buffer of a certain pool type.\r
323\r
324 Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r
325 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
326 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
327\r
328 @param MemoryType The type of memory to allocate.\r
329 @param AllocationSize The number of bytes to allocate.\r
330\r
331 @return A pointer to the allocated buffer or NULL if allocation fails.\r
332\r
333**/\r
334VOID *\r
335InternalAllocatePool (\r
336 IN EFI_MEMORY_TYPE MemoryType, \r
337 IN UINTN AllocationSize\r
338 )\r
339{\r
340 //\r
341 // If we need lots of small runtime/reserved memory type from PEI in the future, \r
342 // we can consider providing a more complex algorithm that allocates runtime pages and \r
343 // provide pool allocations from those pages. \r
344 //\r
345 return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));\r
346}\r
347\r
348/**\r
349 Allocates a buffer of type EfiBootServicesData.\r
350\r
351 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
352 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
353 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
354\r
355 @param AllocationSize The number of bytes to allocate.\r
356\r
357 @return A pointer to the allocated buffer or NULL if allocation fails.\r
358\r
359**/\r
360VOID *\r
361EFIAPI\r
362AllocatePool (\r
363 IN UINTN AllocationSize\r
364 )\r
365{\r
366 EFI_STATUS Status;\r
e386b444 367 VOID *Buffer;\r
368 \r
1c280088 369 Status = PeiServicesAllocatePool (AllocationSize, &Buffer);\r
e386b444 370 if (EFI_ERROR (Status)) {\r
371 Buffer = NULL;\r
372 }\r
373 return Buffer;\r
374}\r
375\r
376/**\r
377 Allocates a buffer of type EfiRuntimeServicesData.\r
378\r
379 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
380 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
381 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
382\r
383 @param AllocationSize The number of bytes to allocate.\r
384\r
385 @return A pointer to the allocated buffer or NULL if allocation fails.\r
386\r
387**/\r
388VOID *\r
389EFIAPI\r
390AllocateRuntimePool (\r
391 IN UINTN AllocationSize\r
392 )\r
393{\r
394 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
395}\r
396\r
397/**\r
8789c5e0 398 Allocates a buffer of type EfiReservedMemoryType.\r
e386b444 399\r
8789c5e0 400 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
e386b444 401 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
402 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
403\r
404 @param AllocationSize The number of bytes to allocate.\r
405\r
406 @return A pointer to the allocated buffer or NULL if allocation fails.\r
407\r
408**/\r
409VOID *\r
410EFIAPI\r
411AllocateReservedPool (\r
412 IN UINTN AllocationSize\r
413 )\r
414{\r
415 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
416}\r
417\r
418/**\r
446b94b0 419 Allocates and zeros a buffer of a certain pool type.\r
e386b444 420\r
446b94b0 421 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
e386b444 422 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
423 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
424 then NULL is returned.\r
425\r
426 @param PoolType The type of memory to allocate.\r
427 @param AllocationSize The number of bytes to allocate and zero.\r
428\r
429 @return A pointer to the allocated buffer or NULL if allocation fails.\r
430\r
431**/\r
432VOID *\r
433InternalAllocateZeroPool (\r
434 IN EFI_MEMORY_TYPE PoolType, \r
435 IN UINTN AllocationSize\r
436 ) \r
437{\r
438 VOID *Memory;\r
439\r
440 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
441 if (Memory != NULL) {\r
442 Memory = ZeroMem (Memory, AllocationSize);\r
443 }\r
444 return Memory;\r
445}\r
446\r
447/**\r
448 Allocates and zeros a buffer of type EfiBootServicesData.\r
449\r
450 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
451 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
452 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
453 request, then NULL is returned.\r
454\r
455 @param AllocationSize The number of bytes to allocate and zero.\r
456\r
457 @return A pointer to the allocated buffer or NULL if allocation fails.\r
458\r
459**/\r
460VOID *\r
461EFIAPI\r
462AllocateZeroPool (\r
463 IN UINTN AllocationSize\r
464 )\r
465{\r
466 VOID *Memory;\r
467\r
468 Memory = AllocatePool (AllocationSize);\r
469 if (Memory != NULL) {\r
470 Memory = ZeroMem (Memory, AllocationSize);\r
471 }\r
472 return Memory;\r
473}\r
474\r
475/**\r
476 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
477\r
478 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
479 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
480 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
481 request, then NULL is returned.\r
482\r
483 @param AllocationSize The number of bytes to allocate and zero.\r
484\r
485 @return A pointer to the allocated buffer or NULL if allocation fails.\r
486\r
487**/\r
488VOID *\r
489EFIAPI\r
490AllocateRuntimeZeroPool (\r
491 IN UINTN AllocationSize\r
492 )\r
493{\r
494 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
495}\r
496\r
497/**\r
498 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
499\r
500 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
501 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
502 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
503 request, then NULL is returned.\r
504\r
505 @param AllocationSize The number of bytes to allocate and zero.\r
506\r
507 @return A pointer to the allocated buffer or NULL if allocation fails.\r
508\r
509**/\r
510VOID *\r
511EFIAPI\r
512AllocateReservedZeroPool (\r
513 IN UINTN AllocationSize\r
514 )\r
515{\r
516 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
517}\r
518\r
519/**\r
446b94b0 520 Copies a buffer to an allocated buffer of a certain pool type.\r
e386b444 521\r
446b94b0 522 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
e386b444 523 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
524 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
525 is not enough memory remaining to satisfy the request, then NULL is returned.\r
526 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 527 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 528\r
529 @param PoolType The type of pool to allocate.\r
530 @param AllocationSize The number of bytes to allocate and zero.\r
531 @param Buffer The buffer to copy to the allocated buffer.\r
532\r
533 @return A pointer to the allocated buffer or NULL if allocation fails.\r
534\r
535**/\r
536VOID *\r
537InternalAllocateCopyPool (\r
538 IN EFI_MEMORY_TYPE PoolType, \r
539 IN UINTN AllocationSize,\r
540 IN CONST VOID *Buffer\r
541 ) \r
542{\r
543 VOID *Memory;\r
544\r
545 ASSERT (Buffer != NULL);\r
546 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
547\r
548 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
549 if (Memory != NULL) {\r
550 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
551 }\r
552 return Memory;\r
553} \r
554\r
555/**\r
556 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
557\r
558 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
559 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
560 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
561 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 562 \r
e386b444 563 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 564 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 565\r
566 @param AllocationSize The number of bytes to allocate and zero.\r
567 @param Buffer The buffer to copy to the allocated buffer.\r
568\r
569 @return A pointer to the allocated buffer or NULL if allocation fails.\r
570\r
571**/\r
572VOID *\r
573EFIAPI\r
574AllocateCopyPool (\r
575 IN UINTN AllocationSize,\r
576 IN CONST VOID *Buffer\r
577 )\r
578{\r
579 VOID *Memory;\r
580\r
581 ASSERT (Buffer != NULL);\r
582 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
583\r
584 Memory = AllocatePool (AllocationSize);\r
585 if (Memory != NULL) {\r
586 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
587 }\r
588 return Memory;\r
589}\r
590\r
591/**\r
592 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
593\r
594 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
595 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
596 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
597 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 598 \r
e386b444 599 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 600 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 601\r
602 @param AllocationSize The number of bytes to allocate and zero.\r
603 @param Buffer The buffer to copy to the allocated buffer.\r
604\r
605 @return A pointer to the allocated buffer or NULL if allocation fails.\r
606\r
607**/\r
608VOID *\r
609EFIAPI\r
610AllocateRuntimeCopyPool (\r
611 IN UINTN AllocationSize,\r
612 IN CONST VOID *Buffer\r
613 )\r
614{\r
615 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
616}\r
617\r
618/**\r
619 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
620\r
621 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
622 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
623 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
624 is not enough memory remaining to satisfy the request, then NULL is returned.\r
9638ba6d 625 \r
e386b444 626 If Buffer is NULL, then ASSERT().\r
9e6fa6d2 627 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
e386b444 628\r
629 @param AllocationSize The number of bytes to allocate and zero.\r
630 @param Buffer The buffer to copy to the allocated buffer.\r
631\r
632 @return A pointer to the allocated buffer or NULL if allocation fails.\r
633\r
634**/\r
635VOID *\r
636EFIAPI\r
637AllocateReservedCopyPool (\r
638 IN UINTN AllocationSize,\r
639 IN CONST VOID *Buffer\r
640 )\r
641{\r
642 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
643}\r
644\r
3b6a71fc 645/**\r
646 Reallocates a buffer of a specified memory type.\r
647\r
648 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
649 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r
650 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
651 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
652 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
653 enough memory remaining to satisfy the request, then NULL is returned.\r
654 \r
56304569 655 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
656 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 657\r
658 @param PoolType The type of pool to allocate.\r
659 @param OldSize The size, in bytes, of OldBuffer.\r
660 @param NewSize The size, in bytes, of the buffer to reallocate.\r
58380e9c 661 @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
662 optional parameter that may be NULL.\r
3b6a71fc 663\r
664 @return A pointer to the allocated buffer or NULL if allocation fails.\r
665\r
666**/\r
667VOID *\r
3b6a71fc 668InternalReallocatePool (\r
669 IN EFI_MEMORY_TYPE PoolType, \r
670 IN UINTN OldSize,\r
671 IN UINTN NewSize,\r
672 IN VOID *OldBuffer OPTIONAL\r
673 )\r
674{\r
675 VOID *NewBuffer;\r
676\r
6babbe1f 677 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
3b6a71fc 678 if (NewBuffer != NULL && OldBuffer != NULL) {\r
679 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
680 FreePool (OldBuffer);\r
681 }\r
682 return NewBuffer;\r
683}\r
684\r
685/**\r
686 Reallocates a buffer of type EfiBootServicesData.\r
687\r
688 Allocates and zeros the number bytes specified by NewSize from memory of type\r
689 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
690 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
691 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
692 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
693 enough memory remaining to satisfy the request, then NULL is returned.\r
694 \r
56304569 695 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
696 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 697\r
698 @param OldSize The size, in bytes, of OldBuffer.\r
699 @param NewSize The size, in bytes, of the buffer to reallocate.\r
700 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
701 parameter that may be NULL.\r
702\r
703 @return A pointer to the allocated buffer or NULL if allocation fails.\r
704\r
705**/\r
706VOID *\r
707EFIAPI\r
708ReallocatePool (\r
709 IN UINTN OldSize,\r
710 IN UINTN NewSize,\r
711 IN VOID *OldBuffer OPTIONAL\r
712 )\r
713{\r
714 return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r
715}\r
716\r
717/**\r
718 Reallocates a buffer of type EfiRuntimeServicesData.\r
719\r
720 Allocates and zeros the number bytes specified by NewSize from memory of type\r
721 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
722 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
723 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
724 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
725 enough memory remaining to satisfy the request, then NULL is returned.\r
726\r
56304569 727 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
728 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 729\r
730 @param OldSize The size, in bytes, of OldBuffer.\r
731 @param NewSize The size, in bytes, of the buffer to reallocate.\r
732 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
733 parameter that may be NULL.\r
734\r
735 @return A pointer to the allocated buffer or NULL if allocation fails.\r
736\r
737**/\r
738VOID *\r
739EFIAPI\r
740ReallocateRuntimePool (\r
741 IN UINTN OldSize,\r
742 IN UINTN NewSize,\r
743 IN VOID *OldBuffer OPTIONAL\r
744 )\r
745{\r
746 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
747}\r
748\r
749/**\r
750 Reallocates a buffer of type EfiReservedMemoryType.\r
751\r
752 Allocates and zeros the number bytes specified by NewSize from memory of type\r
753 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
754 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
755 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
756 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
757 enough memory remaining to satisfy the request, then NULL is returned.\r
2297186d 758\r
56304569 759 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
760 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
3b6a71fc 761\r
762 @param OldSize The size, in bytes, of OldBuffer.\r
763 @param NewSize The size, in bytes, of the buffer to reallocate.\r
58380e9c 764 @param OldBuffer The buffer to copy to the allocated buffer. This is an \r
765 optional parameter that may be NULL.\r
3b6a71fc 766\r
767 @return A pointer to the allocated buffer or NULL if allocation fails.\r
768\r
769**/\r
770VOID *\r
771EFIAPI\r
772ReallocateReservedPool (\r
773 IN UINTN OldSize,\r
774 IN UINTN NewSize,\r
775 IN VOID *OldBuffer OPTIONAL\r
776 )\r
777{\r
778 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
779}\r
780\r
e386b444 781/**\r
782 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
783 Memory Allocation Library.\r
784\r
785 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
6e10b70a 786 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
446b94b0 787 resources, then this function will perform no actions.\r
9638ba6d 788 \r
e386b444 789 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
790 then ASSERT().\r
791\r
2fc59a00 792 @param Buffer The pointer to the buffer to free.\r
e386b444 793\r
794**/\r
795VOID\r
796EFIAPI\r
797FreePool (\r
798 IN VOID *Buffer\r
799 )\r
800{\r
801 //\r
802 // PEI phase does not support to free pool, so leave it as NOP.\r
803 //\r
804}\r
805\r
7d582d6b 806\r