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