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