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