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