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