]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Library/DxeCoreMemoryAllocationLib/MemoryAllocationLib.c
Update code to support VS2013 tool chain.
[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
4e1005ec 5 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
cd5ebaa0 6 This program and the accompanying materials \r
0ac72713 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
aa2f249e 130 Allocation Library. If it is not possible to free allocated pages, then this function will\r
446b94b0 131 perform no actions.\r
f6998888 132 \r
0ac72713 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 = CoreFreePages ((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
91403ce9 163 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
0ac72713 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 // Caculate 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 = CoreAllocatePages (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 = CoreFreePages (Memory, UnalignedPages);\r
217 ASSERT_EFI_ERROR (Status);\r
218 }\r
219 Memory = (EFI_PHYSICAL_ADDRESS) (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 = CoreFreePages (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 = CoreAllocatePages (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
f6998888 248 \r
0ac72713 249 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 250 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
0ac72713 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
f6998888 276 \r
0ac72713 277 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 278 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
0ac72713 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
f6998888 304 \r
0ac72713 305 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
91403ce9 306 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
0ac72713 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
aa2f249e 331 Allocation Library. If it is not possible to free allocated pages, then this function will \r
446b94b0 332 perform no actions.\r
f6998888 333 \r
0ac72713 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 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 = CoreFreePages ((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
4e1005ec
ED
378 Memory = NULL;\r
379\r
0ac72713 380 Status = CoreAllocatePool (MemoryType, AllocationSize, &Memory);\r
381 if (EFI_ERROR (Status)) {\r
382 Memory = NULL;\r
383 }\r
384 return Memory;\r
385}\r
386\r
387/**\r
388 Allocates a buffer of type EfiBootServicesData.\r
389\r
390 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
391 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
392 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
393\r
394 @param AllocationSize The number of bytes to allocate.\r
395\r
396 @return A pointer to the allocated buffer or NULL if allocation fails.\r
397\r
398**/\r
399VOID *\r
400EFIAPI\r
401AllocatePool (\r
402 IN UINTN AllocationSize\r
403 )\r
404{\r
405 return InternalAllocatePool (EfiBootServicesData, AllocationSize);\r
406}\r
407\r
408/**\r
409 Allocates a buffer of type EfiRuntimeServicesData.\r
410\r
411 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r
412 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
413 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
414\r
415 @param AllocationSize The number of bytes to allocate.\r
416\r
417 @return A pointer to the allocated buffer or NULL if allocation fails.\r
418\r
419**/\r
420VOID *\r
421EFIAPI\r
422AllocateRuntimePool (\r
423 IN UINTN AllocationSize\r
424 )\r
425{\r
426 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r
427}\r
428\r
429/**\r
31771abd 430 Allocates a buffer of type EfiReservedMemoryType.\r
0ac72713 431\r
31771abd 432 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r
0ac72713 433 a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
434 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
435\r
436 @param AllocationSize The number of bytes to allocate.\r
437\r
438 @return A pointer to the allocated buffer or NULL if allocation fails.\r
439\r
440**/\r
441VOID *\r
442EFIAPI\r
443AllocateReservedPool (\r
444 IN UINTN AllocationSize\r
445 )\r
446{\r
447 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r
448}\r
449\r
450/**\r
446b94b0 451 Allocates and zeros a buffer of a certain pool type.\r
0ac72713 452\r
446b94b0 453 Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r
0ac72713 454 with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r
455 buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r
456 then NULL is returned.\r
457\r
458 @param PoolType The type of memory to allocate.\r
459 @param AllocationSize The number of bytes to allocate and zero.\r
460\r
461 @return A pointer to the allocated buffer or NULL if allocation fails.\r
462\r
463**/\r
464VOID *\r
465InternalAllocateZeroPool (\r
466 IN EFI_MEMORY_TYPE PoolType, \r
467 IN UINTN AllocationSize\r
468 ) \r
469{\r
470 VOID *Memory;\r
471\r
472 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
473 if (Memory != NULL) {\r
474 Memory = ZeroMem (Memory, AllocationSize);\r
475 }\r
476 return Memory;\r
477}\r
478\r
479/**\r
480 Allocates and zeros a buffer of type EfiBootServicesData.\r
481\r
482 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r
483 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
484 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
485 request, then NULL is returned.\r
486\r
487 @param AllocationSize The number of bytes to allocate and zero.\r
488\r
489 @return A pointer to the allocated buffer or NULL if allocation fails.\r
490\r
491**/\r
492VOID *\r
493EFIAPI\r
494AllocateZeroPool (\r
495 IN UINTN AllocationSize\r
496 )\r
497{\r
498 return InternalAllocateZeroPool (EfiBootServicesData, AllocationSize);\r
499}\r
500\r
501/**\r
502 Allocates and zeros a buffer of type EfiRuntimeServicesData.\r
503\r
504 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r
505 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
506 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
507 request, then NULL is returned.\r
508\r
509 @param AllocationSize The number of bytes to allocate and zero.\r
510\r
511 @return A pointer to the allocated buffer or NULL if allocation fails.\r
512\r
513**/\r
514VOID *\r
515EFIAPI\r
516AllocateRuntimeZeroPool (\r
517 IN UINTN AllocationSize\r
518 )\r
519{\r
520 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r
521}\r
522\r
523/**\r
524 Allocates and zeros a buffer of type EfiReservedMemoryType.\r
525\r
526 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r
527 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r
528 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r
529 request, then NULL is returned.\r
530\r
531 @param AllocationSize The number of bytes to allocate and zero.\r
532\r
533 @return A pointer to the allocated buffer or NULL if allocation fails.\r
534\r
535**/\r
536VOID *\r
537EFIAPI\r
538AllocateReservedZeroPool (\r
539 IN UINTN AllocationSize\r
540 )\r
541{\r
542 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r
543}\r
544\r
545/**\r
446b94b0 546 Copies a buffer to an allocated buffer of a certain pool type.\r
0ac72713 547\r
446b94b0 548 Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r
0ac72713 549 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
550 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
551 is not enough memory remaining to satisfy the request, then NULL is returned.\r
552 If Buffer is NULL, then ASSERT().\r
553 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
554\r
555 @param PoolType The type of pool to allocate.\r
556 @param AllocationSize The number of bytes to allocate and zero.\r
557 @param Buffer The buffer to copy to the allocated buffer.\r
558\r
559 @return A pointer to the allocated buffer or NULL if allocation fails.\r
560\r
561**/\r
562VOID *\r
563InternalAllocateCopyPool (\r
564 IN EFI_MEMORY_TYPE PoolType, \r
565 IN UINTN AllocationSize,\r
566 IN CONST VOID *Buffer\r
567 ) \r
568{\r
569 VOID *Memory;\r
570\r
571 ASSERT (Buffer != NULL);\r
572 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r
573\r
574 Memory = InternalAllocatePool (PoolType, AllocationSize);\r
575 if (Memory != NULL) {\r
576 Memory = CopyMem (Memory, Buffer, AllocationSize);\r
577 }\r
578 return Memory;\r
579} \r
580\r
581/**\r
582 Copies a buffer to an allocated buffer of type EfiBootServicesData.\r
583\r
584 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r
585 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
586 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
587 is not enough memory remaining to satisfy the request, then NULL is returned.\r
f6998888 588 \r
0ac72713 589 If Buffer is NULL, then ASSERT().\r
590 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
591\r
592 @param AllocationSize The number of bytes to allocate and zero.\r
593 @param Buffer The buffer to copy to the allocated buffer.\r
594\r
595 @return A pointer to the allocated buffer or NULL if allocation fails.\r
596\r
597**/\r
598VOID *\r
599EFIAPI\r
600AllocateCopyPool (\r
601 IN UINTN AllocationSize,\r
602 IN CONST VOID *Buffer\r
603 )\r
604{\r
605 return InternalAllocateCopyPool (EfiBootServicesData, AllocationSize, Buffer);\r
606}\r
607\r
608/**\r
609 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r
610\r
611 Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r
612 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
613 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
614 is not enough memory remaining to satisfy the request, then NULL is returned.\r
f6998888 615 \r
0ac72713 616 If Buffer is NULL, then ASSERT().\r
617 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
618\r
619 @param AllocationSize The number of bytes to allocate and zero.\r
620 @param Buffer The buffer to copy to the allocated buffer.\r
621\r
622 @return A pointer to the allocated buffer or NULL if allocation fails.\r
623\r
624**/\r
625VOID *\r
626EFIAPI\r
627AllocateRuntimeCopyPool (\r
628 IN UINTN AllocationSize,\r
629 IN CONST VOID *Buffer\r
630 )\r
631{\r
632 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r
633}\r
634\r
635/**\r
636 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r
637\r
638 Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r
639 AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r
640 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r
641 is not enough memory remaining to satisfy the request, then NULL is returned.\r
f6998888 642 \r
0ac72713 643 If Buffer is NULL, then ASSERT().\r
644 If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r
645\r
646 @param AllocationSize The number of bytes to allocate and zero.\r
647 @param Buffer The buffer to copy to the allocated buffer.\r
648\r
649 @return A pointer to the allocated buffer or NULL if allocation fails.\r
650\r
651**/\r
652VOID *\r
653EFIAPI\r
654AllocateReservedCopyPool (\r
655 IN UINTN AllocationSize,\r
656 IN CONST VOID *Buffer\r
657 )\r
658{\r
659 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r
660}\r
661\r
f6998888 662/**\r
663 Reallocates a buffer of a specified memory type.\r
664\r
665 Allocates and zeros the number bytes specified by NewSize from memory of the type\r
666 specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r
667 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
668 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
669 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
670 enough memory remaining to satisfy the request, then NULL is returned.\r
671 \r
56304569 672 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
673 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
f6998888 674\r
675 @param PoolType The type of pool to allocate.\r
676 @param OldSize The size, in bytes, of OldBuffer.\r
677 @param NewSize The size, in bytes, of the buffer to reallocate.\r
678 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
679 parameter that may be NULL.\r
680\r
681 @return A pointer to the allocated buffer or NULL if allocation fails.\r
682\r
683**/\r
684VOID *\r
685InternalReallocatePool (\r
686 IN EFI_MEMORY_TYPE PoolType, \r
687 IN UINTN OldSize,\r
688 IN UINTN NewSize,\r
689 IN VOID *OldBuffer OPTIONAL\r
690 )\r
691{\r
692 VOID *NewBuffer;\r
693\r
ae55f4eb 694 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r
f6998888 695 if (NewBuffer != NULL && OldBuffer != NULL) {\r
696 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r
697 FreePool (OldBuffer);\r
698 }\r
699 return NewBuffer;\r
700}\r
701\r
702/**\r
703 Reallocates a buffer of type EfiBootServicesData.\r
704\r
705 Allocates and zeros the number bytes specified by NewSize from memory of type\r
706 EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
707 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
708 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
709 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
710 enough memory remaining to satisfy the request, then NULL is returned.\r
711 \r
56304569 712 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
713 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
f6998888 714\r
715 @param OldSize The size, in bytes, of OldBuffer.\r
716 @param NewSize The size, in bytes, of the buffer to reallocate.\r
717 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
718 parameter that may be NULL.\r
719\r
720 @return A pointer to the allocated buffer or NULL if allocation fails.\r
721\r
722**/\r
723VOID *\r
724EFIAPI\r
725ReallocatePool (\r
726 IN UINTN OldSize,\r
727 IN UINTN NewSize,\r
728 IN VOID *OldBuffer OPTIONAL\r
729 )\r
730{\r
731 return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r
732}\r
733\r
734/**\r
735 Reallocates a buffer of type EfiRuntimeServicesData.\r
736\r
737 Allocates and zeros the number bytes specified by NewSize from memory of type\r
738 EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r
739 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
740 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
741 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
742 enough memory remaining to satisfy the request, then NULL is returned.\r
743\r
56304569 744 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
745 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
f6998888 746\r
747 @param OldSize The size, in bytes, of OldBuffer.\r
748 @param NewSize The size, in bytes, of the buffer to reallocate.\r
749 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
750 parameter that may be NULL.\r
751\r
752 @return A pointer to the allocated buffer or NULL if allocation fails.\r
753\r
754**/\r
755VOID *\r
756EFIAPI\r
757ReallocateRuntimePool (\r
758 IN UINTN OldSize,\r
759 IN UINTN NewSize,\r
760 IN VOID *OldBuffer OPTIONAL\r
761 )\r
762{\r
763 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r
764}\r
765\r
766/**\r
767 Reallocates a buffer of type EfiReservedMemoryType.\r
768\r
769 Allocates and zeros the number bytes specified by NewSize from memory of type\r
770 EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r
771 NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r
772 OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r
773 If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r
774 enough memory remaining to satisfy the request, then NULL is returned.\r
6a30c6d0 775\r
56304569 776 If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r
777 is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r
f6998888 778\r
779 @param OldSize The size, in bytes, of OldBuffer.\r
780 @param NewSize The size, in bytes, of the buffer to reallocate.\r
781 @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r
782 parameter that may be NULL.\r
783\r
784 @return A pointer to the allocated buffer or NULL if allocation fails.\r
785\r
786**/\r
787VOID *\r
788EFIAPI\r
789ReallocateReservedPool (\r
790 IN UINTN OldSize,\r
791 IN UINTN NewSize,\r
792 IN VOID *OldBuffer OPTIONAL\r
793 )\r
794{\r
795 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r
796}\r
797\r
0ac72713 798/**\r
799 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
800 Memory Allocation Library.\r
801\r
802 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
aa2f249e 803 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
446b94b0 804 resources, then this function will perform no actions.\r
f6998888 805 \r
0ac72713 806 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
807 then ASSERT().\r
808\r
809 @param Buffer Pointer to the buffer to free.\r
810\r
811**/\r
812VOID\r
813EFIAPI\r
814FreePool (\r
815 IN VOID *Buffer\r
816 )\r
817{\r
818 EFI_STATUS Status;\r
819\r
820 Status = CoreFreePool (Buffer);\r
821 ASSERT_EFI_ERROR (Status);\r
822}\r
823\r