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