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