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