]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
Add 3 reallocation pool services in MemoryAllocationLib class.
[mirror_edk2.git] / MdeModulePkg / Library / DxeCoreMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
0ac72713 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 - 2008, Intel Corporation<BR>\r
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
14**/\r
15\r
16\r
17#include <PiDxe.h>\r
18\r
f6998888 19\r
0ac72713 20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/BaseMemoryLib.h>\r
22#include <Library/DebugLib.h>\r
0ac72713 23#include "DxeCoreMemoryAllocationServices.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 = CoreAllocatePages (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.\r
f6998888 131 \r
0ac72713 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 = CoreFreePages ((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 = CoreAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);\r
205 if (EFI_ERROR (Status)) {\r
206 return NULL;\r
207 }\r
208 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r
209 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r
210 if (UnalignedPages > 0) {\r
211 //\r
212 // Free first unaligned page(s).\r
213 //\r
214 Status = CoreFreePages (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 = CoreFreePages (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 = CoreAllocatePages (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
f6998888 246 \r
0ac72713 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
f6998888 273 \r
0ac72713 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
f6998888 300 \r
0ac72713 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
f6998888 327 \r
0ac72713 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 = CoreFreePages ((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 = CoreAllocatePool (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
31771abd 422 Allocates a buffer of type EfiReservedMemoryType.\r
0ac72713 423\r
31771abd 424 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
0ac72713 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
545 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
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
f6998888 580 \r
0ac72713 581 If Buffer is NULL, then ASSERT().\r
582 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
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
f6998888 607 \r
0ac72713 608 If Buffer is NULL, then ASSERT().\r
609 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
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
f6998888 634 \r
0ac72713 635 If Buffer is NULL, then ASSERT().\r
636 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
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
f6998888 654/**\r
655 Reallocates a buffer of a specified memory type.\r
656\r
657 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
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
664 If OldBuffer is NULL, then ASSERT().\r
665 If NewSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
666 If OldSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
667\r
668 @param PoolType The type of pool to allocate.\r
669 @param OldSize The size, in bytes, of OldBuffer.\r
670 @param NewSize The size, in bytes, of the buffer to reallocate.\r
671 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
672 parameter that may be NULL.\r
673\r
674 @return A pointer to the allocated buffer or NULL if allocation fails.\r
675\r
676**/\r
677VOID *\r
678InternalReallocatePool (\r
679 IN EFI_MEMORY_TYPE PoolType, \r
680 IN UINTN OldSize,\r
681 IN UINTN NewSize,\r
682 IN VOID *OldBuffer OPTIONAL\r
683 )\r
684{\r
685 VOID *NewBuffer;\r
686\r
687 NewBuffer = AllocateZeroPool (NewSize);\r
688 if (NewBuffer != NULL && OldBuffer != NULL) {\r
689 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
690 FreePool (OldBuffer);\r
691 }\r
692 return NewBuffer;\r
693}\r
694\r
695/**\r
696 Reallocates a buffer of type EfiBootServicesData.\r
697\r
698 Allocates and zeros the number bytes specified by NewSize from memory of type\r
699 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
700 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
701 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
702 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
703 enough memory remaining to satisfy the request, then NULL is returned.\r
704 \r
705 If OldBuffer is NULL, then ASSERT().\r
706 If NewSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
707 If OldSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
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
731 Allocates and zeros the number bytes specified by NewSize from memory of type\r
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
738 If OldBuffer is NULL, then ASSERT().\r
739 If NewSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
740 If OldSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
741\r
742 @param OldSize The size, in bytes, of OldBuffer.\r
743 @param NewSize The size, in bytes, of the buffer to reallocate.\r
744 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
745 parameter that may be NULL.\r
746\r
747 @return A pointer to the allocated buffer or NULL if allocation fails.\r
748\r
749**/\r
750VOID *\r
751EFIAPI\r
752ReallocateRuntimePool (\r
753 IN UINTN OldSize,\r
754 IN UINTN NewSize,\r
755 IN VOID *OldBuffer OPTIONAL\r
756 )\r
757{\r
758 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
759}\r
760\r
761/**\r
762 Reallocates a buffer of type EfiReservedMemoryType.\r
763\r
764 Allocates and zeros the number bytes specified by NewSize from memory of type\r
765 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
766 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
767 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
768 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
769 enough memory remaining to satisfy the request, then NULL is returned.\r
770 \r
771 If OldBuffer is NULL, then ASSERT().\r
772 If NewSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
773 If OldSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
774\r
775 @param OldSize The size, in bytes, of OldBuffer.\r
776 @param NewSize The size, in bytes, of the buffer to reallocate.\r
777 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
778 parameter that may be NULL.\r
779\r
780 @return A pointer to the allocated buffer or NULL if allocation fails.\r
781\r
782**/\r
783VOID *\r
784EFIAPI\r
785ReallocateReservedPool (\r
786 IN UINTN OldSize,\r
787 IN UINTN NewSize,\r
788 IN VOID *OldBuffer OPTIONAL\r
789 )\r
790{\r
791 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
792}\r
793\r
0ac72713 794/**\r
795 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
796 Memory Allocation Library.\r
797\r
798 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
799 pool allocation services of the Memory Allocation Library.\r
f6998888 800 \r
0ac72713 801 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
802 then ASSERT().\r
803\r
804 @param Buffer Pointer to the buffer to free.\r
805\r
806**/\r
807VOID\r
808EFIAPI\r
809FreePool (\r
810 IN VOID *Buffer\r
811 )\r
812{\r
813 EFI_STATUS Status;\r
814\r
815 Status = CoreFreePool (Buffer);\r
816 ASSERT_EFI_ERROR (Status);\r
817}\r
818\r