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