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