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