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