63641569ffe65d2450d59ee6c6e25ae8994faee9
[mirror_edk2.git] / MdePkg / Library / PeiMemoryAllocationLib / MemoryAllocationLib.c
1 /** @file
2 Support routines for memory allocation routines for use with drivers.
3
4 Copyright (c) 2006, Intel Corporation<BR>
5 All rights reserved. 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 Module Name: MemoryAllocationLib.c
14
15 **/
16
17
18
19 /**
20 Allocates the number of 4KB pages specified by Pages of a certain memory type.
21
22 @param MemoryType The type of memory to allocate.
23 @param Pages The number of 4 KB pages to allocate.
24
25 @return A pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
26 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 **/
30 VOID *
31 InternalAllocatePages (
32 IN EFI_MEMORY_TYPE MemoryType,
33 IN UINTN Pages
34 )
35 {
36 EFI_STATUS Status;
37 EFI_PHYSICAL_ADDRESS Memory;
38 EFI_PEI_SERVICES **PeiServices;
39
40 if (Pages == 0) {
41 return NULL;
42 }
43
44 PeiServices = GetPeiServicesTablePointer ();
45 Status = (*PeiServices)->AllocatePages (PeiServices, MemoryType, Pages, &Memory);
46 if (EFI_ERROR (Status)) {
47 Memory = 0;
48 }
49 return (VOID *) (UINTN) Memory;
50 }
51
52 /**
53 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData.
54
55 @param Pages The number of 4 KB pages to allocate.
56
57 @return A pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
58 If Pages is 0, then NULL is returned.
59 If there is not enough memory remaining to satisfy the request, then NULL is returned.
60
61 **/
62 VOID *
63 EFIAPI
64 AllocatePages (
65 IN UINTN Pages
66 )
67 {
68 return InternalAllocatePages (EfiBootServicesData, Pages);
69 }
70
71 /**
72 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData.
73
74 @param Pages The number of 4 KB pages to allocate.
75
76 @return A pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
77 If Pages is 0, then NULL is returned.
78 If there is not enough memory remaining to satisfy the request, then NULL is returned.
79
80 **/
81 VOID *
82 EFIAPI
83 AllocateRuntimePages (
84 IN UINTN Pages
85 )
86 {
87 return InternalAllocatePages (EfiRuntimeServicesData, Pages);
88 }
89
90 /**
91 Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType.
92
93 @param Pages The number of 4 KB pages to allocate.
94
95 @return A pointer to the allocated buffer. The buffer returned is aligned on a 4KB boundary.
96 If Pages is 0, then NULL is returned.
97 If there is not enough memory remaining to satisfy the request, then NULL is returned.
98
99 **/
100 VOID *
101 EFIAPI
102 AllocateReservedPages (
103 IN UINTN Pages
104 )
105 {
106 return InternalAllocatePages (EfiReservedMemoryType, Pages);
107 }
108
109 /**
110 Frees one or more 4KB pages that were previously allocated with
111 one of the page allocation functions in the Memory Allocation Library.
112
113 @param Buffer Pointer to the buffer of pages to free.
114 @param Pages The number of 4 KB pages to free.
115
116 **/
117 VOID
118 EFIAPI
119 FreePages (
120 IN VOID *Buffer,
121 IN UINTN Pages
122 )
123 {
124 //
125 // PEI phase does not support to free pages, so leave it as NOP.
126 //
127 }
128
129 /**
130 Allocates the number of 4KB pages specified by Pages of a certian memory type
131 with an alignment specified by Alignment.
132
133 @param MemoryType The type of memory to allocate.
134 @param Pages The number of 4 KB pages to allocate.
135 @param Alignment The requested alignment of the allocation. Must be a power of two.
136 If Alignment is zero, then byte alignment is used.
137
138 @return The allocated buffer is returned. If Pages is 0, then NULL is returned.
139 If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
140
141 **/
142 VOID *
143 InternalAllocateAlignedPages (
144 IN EFI_MEMORY_TYPE MemoryType,
145 IN UINTN Pages,
146 IN UINTN Alignment
147 )
148 {
149 VOID *Memory;
150 UINTN AlignmentMask;
151
152 //
153 // Alignment must be a power of two or zero.
154 //
155 ASSERT ((Alignment & (Alignment - 1)) == 0);
156
157 if (Pages == 0) {
158 return NULL;
159 }
160 //
161 // We would rather waste some memory to save PEI code size.
162 //
163 Memory = InternalAllocatePages (MemoryType, Pages + EFI_SIZE_TO_PAGES (Alignment));
164 if (Alignment == 0) {
165 AlignmentMask = Alignment;
166 } else {
167 AlignmentMask = Alignment - 1;
168 }
169 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);
170 }
171
172 /**
173 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData
174 with an alignment specified by Alignment.
175
176 @param Pages The number of 4 KB pages to allocate.
177 @param Alignment The requested alignment of the allocation. Must be a power of two.
178 If Alignment is zero, then byte alignment is used.
179
180 @return The allocated buffer is returned. If Pages is 0, then NULL is returned.
181 If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
182
183 **/
184 VOID *
185 EFIAPI
186 AllocateAlignedPages (
187 IN UINTN Pages,
188 IN UINTN Alignment
189 )
190 {
191 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);
192 }
193
194 /**
195 Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData
196 with an alignment specified by Alignment.
197
198 @param Pages The number of 4 KB pages to allocate.
199 @param Alignment The requested alignment of the allocation. Must be a power of two.
200 If Alignment is zero, then byte alignment is used.
201
202 @return The allocated buffer is returned. If Pages is 0, then NULL is returned.
203 If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
204
205 **/
206 VOID *
207 EFIAPI
208 AllocateAlignedRuntimePages (
209 IN UINTN Pages,
210 IN UINTN Alignment
211 )
212 {
213 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
214 }
215
216 /**
217 Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.
218
219 @param Pages The number of 4 KB pages to allocate.
220 @param Alignment The requested alignment of the allocation. Must be a power of two.
221 If Alignment is zero, then byte alignment is used.
222
223 @return The allocated buffer is returned. If Pages is 0, then NULL is returned.
224 If there is not enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
225
226 **/
227 VOID *
228 EFIAPI
229 AllocateAlignedReservedPages (
230 IN UINTN Pages,
231 IN UINTN Alignment
232 )
233 {
234 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);
235 }
236
237 /**
238 Frees one or more 4KB pages that were previously allocated with
239 one of the aligned page allocation functions in the Memory Allocation Library.
240
241 @param Buffer Pointer to the buffer of pages to free.
242 @param Pages The number of 4 KB pages to free.
243
244 **/
245 VOID
246 EFIAPI
247 FreeAlignedPages (
248 IN VOID *Buffer,
249 IN UINTN Pages
250 )
251 {
252 //
253 // PEI phase does not support to free pages, so leave it as NOP.
254 //
255 }
256
257 /**
258 Allocates a buffer of a certain memory type.
259
260 @param MemoryType The type of memory to allocate.
261 @param AllocationSize The number of bytes to allocate.
262
263 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
264 If there is not enough memory remaining to satisfy the request, then NULL is returned.
265
266 **/
267 VOID *
268 InternalAllocatePool (
269 IN EFI_MEMORY_TYPE MemoryType,
270 IN UINTN AllocationSize
271 )
272 {
273 //
274 // If we need lots of small runtime/reserved memory type from PEI in the future,
275 // we can consider providing a more complex algorithm that allocates runtime pages and
276 // provide pool allocations from those pages.
277 //
278 return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));
279 }
280
281 /**
282 Allocates a buffer of type EfiBootServicesData.
283
284 @param AllocationSize The number of bytes to allocate.
285
286 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
287 If there is not enough memory remaining to satisfy the request, then NULL is returned.
288
289 **/
290 VOID *
291 EFIAPI
292 AllocatePool (
293 IN UINTN AllocationSize
294 )
295 {
296 EFI_STATUS Status;
297 EFI_PEI_SERVICES **PeiServices;
298 VOID *Buffer;
299
300 PeiServices = GetPeiServicesTablePointer ();
301
302 Status = (*PeiServices)->AllocatePool (PeiServices, AllocationSize, &Buffer);
303 if (EFI_ERROR (Status)) {
304 Buffer = NULL;
305 }
306 return Buffer;
307 }
308
309 /**
310 Allocates a buffer of type EfiRuntimeServicesData.
311
312 @param AllocationSize The number of bytes to allocate.
313
314 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
315 If there is not enough memory remaining to satisfy the request, then NULL is returned.
316
317 **/
318 VOID *
319 EFIAPI
320 AllocateRuntimePool (
321 IN UINTN AllocationSize
322 )
323 {
324 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
325 }
326
327 /**
328 Allocates a buffer of type EfiReservedMemoryType.
329
330 @param AllocationSize The number of bytes to allocate.
331
332 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
333 If there is not enough memory remaining to satisfy the request, then NULL is returned.
334
335 **/
336 VOID *
337 EFIAPI
338 AllocateReservedPool (
339 IN UINTN AllocationSize
340 )
341 {
342 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);
343 }
344
345 /**
346 Allocates and zeros a buffer of a certian pool type.
347
348 @param PoolType The type of memory to allocate.
349 @param AllocationSize The number of bytes to allocate and zero.
350
351 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
352 If there is not enough memory remaining to satisfy the request, then NULL is returned.
353
354 **/
355 VOID *
356 InternalAllocateZeroPool (
357 IN EFI_MEMORY_TYPE PoolType,
358 IN UINTN AllocationSize
359 )
360 {
361 VOID *Memory;
362
363 Memory = InternalAllocatePool (PoolType, AllocationSize);
364 if (Memory != NULL) {
365 Memory = ZeroMem (Memory, AllocationSize);
366 }
367 return Memory;
368 }
369
370 /**
371 Allocates and zeros a buffer of type EfiBootServicesData.
372
373 @param AllocationSize The number of bytes to allocate and zero.
374
375 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
376 If there is not enough memory remaining to satisfy the request, then NULL is returned.
377
378 **/
379 VOID *
380 EFIAPI
381 AllocateZeroPool (
382 IN UINTN AllocationSize
383 )
384 {
385 VOID *Memory;
386
387 Memory = AllocatePool (AllocationSize);
388 if (Memory != NULL) {
389 Memory = ZeroMem (Memory, AllocationSize);
390 }
391 return Memory;
392 }
393
394 /**
395 Allocates and zeros a buffer of type EfiRuntimeServicesData.
396
397 @param AllocationSize The number of bytes to allocate and zero.
398
399 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
400 If there is not enough memory remaining to satisfy the request, then NULL is returned.
401
402 **/
403 VOID *
404 EFIAPI
405 AllocateRuntimeZeroPool (
406 IN UINTN AllocationSize
407 )
408 {
409 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
410 }
411
412 /**
413 Allocates and zeros a buffer of type EfiReservedMemoryType.
414
415 @param AllocationSize The number of bytes to allocate and zero.
416
417 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
418 If there is not enough memory remaining to satisfy the request, then NULL is returned.
419
420 **/
421 VOID *
422 EFIAPI
423 AllocateReservedZeroPool (
424 IN UINTN AllocationSize
425 )
426 {
427 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);
428 }
429
430 /**
431 Copies a buffer to an allocated buffer of a certian memory type.
432
433 @param MemoryType The type of pool to allocate.
434 @param AllocationSize The number of bytes to allocate and zero.
435 @param Buffer The buffer to copy to the allocated buffer.
436
437 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
438 If there is not enough memory remaining to satisfy the request, then NULL is returned.
439
440 **/
441 VOID *
442 InternalAllocateCopyPool (
443 IN EFI_MEMORY_TYPE PoolType,
444 IN UINTN AllocationSize,
445 IN CONST VOID *Buffer
446 )
447 {
448 VOID *Memory;
449
450 Memory = InternalAllocatePool (PoolType, AllocationSize);
451 if (Memory != NULL) {
452 Memory = CopyMem (Memory, Buffer, AllocationSize);
453 }
454 return Memory;
455 }
456
457 /**
458 Copies a buffer to an allocated buffer of type EfiBootServicesData.
459
460 @param AllocationSize The number of bytes to allocate.
461 @param Buffer The buffer to copy to the allocated buffer.
462
463 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
464 If there is not enough memory remaining to satisfy the request, then NULL is returned.
465
466 **/
467 VOID *
468 EFIAPI
469 AllocateCopyPool (
470 IN UINTN AllocationSize,
471 IN CONST VOID *Buffer
472 )
473 {
474 VOID *Memory;
475
476 Memory = AllocatePool (AllocationSize);
477 if (Memory != NULL) {
478 Memory = CopyMem (Memory, Buffer, AllocationSize);
479 }
480 return Memory;
481 }
482
483 /**
484 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.
485
486 @param AllocationSize The number of bytes to allocate.
487 @param Buffer The buffer to copy to the allocated buffer.
488
489 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
490 If there is not enough memory remaining to satisfy the request, then NULL is returned.
491
492 **/
493 VOID *
494 EFIAPI
495 AllocateRuntimeCopyPool (
496 IN UINTN AllocationSize,
497 IN CONST VOID *Buffer
498 )
499 {
500 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
501 }
502
503 /**
504 Copies a buffer to an allocated buffer of type EfiReservedMemoryType.
505
506 @param AllocationSize The number of bytes to allocate.
507 @param Buffer The buffer to copy to the allocated buffer.
508
509 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
510 If there is not enough memory remaining to satisfy the request, then NULL is returned.
511
512 **/
513 VOID *
514 EFIAPI
515 AllocateReservedCopyPool (
516 IN UINTN AllocationSize,
517 IN CONST VOID *Buffer
518 )
519 {
520 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);
521 }
522
523 /**
524 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
525
526 @param Buffer Pointer to the buffer to free.
527
528 **/
529 VOID
530 EFIAPI
531 FreePool (
532 IN VOID *Buffer
533 )
534 {
535 //
536 // PEI phase does not support to free pool, so leave it as NOP.
537 //
538 }
539
540 /**
541 Allocates a buffer of a certain pool type at a specified alignment.
542
543 @param PoolType The type of pool to allocate.
544 @param AllocationSize The number of bytes to allocate.
545 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
546
547 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
548 If there is not enough memory remaining to satisfy the request, then NULL is returned.
549
550 **/
551 VOID *
552 InternalAllocateAlignedPool (
553 IN EFI_MEMORY_TYPE PoolType,
554 IN UINTN AllocationSize,
555 IN UINTN Alignment
556 )
557 {
558 VOID *RawAddress;
559 UINTN AlignedAddress;
560 UINTN AlignmentMask;
561
562 //
563 // Alignment must be a power of two or zero.
564 //
565 ASSERT ((Alignment & (Alignment - 1)) == 0);
566
567 if (Alignment == 0) {
568 AlignmentMask = Alignment;
569 } else {
570 AlignmentMask = Alignment - 1;
571 }
572
573 RawAddress = InternalAllocatePool (PoolType, AllocationSize + AlignmentMask);
574
575 AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;
576
577 return (VOID *) AlignedAddress;
578 }
579
580 /**
581 Allocates a buffer of type EfiBootServicesData at a specified alignment.
582
583 @param AllocationSize The number of bytes to allocate.
584 @param Alignment The requested alignment of the allocation. Must be a power of two. If Alignment is zero, then byte alignment is used.
585
586 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
587 If there is not enough memory remaining to satisfy the request, then NULL is returned.
588
589 **/
590 VOID *
591 EFIAPI
592 AllocateAlignedPool (
593 IN UINTN AllocationSize,
594 IN UINTN Alignment
595 )
596 {
597 VOID *RawAddress;
598 UINTN AlignedAddress;
599 UINTN AlignmentMask;
600
601 //
602 // Alignment must be a power of two or zero.
603 //
604 ASSERT ((Alignment & (Alignment - 1)) == 0);
605
606 if (Alignment == 0) {
607 AlignmentMask = Alignment;
608 } else {
609 AlignmentMask = Alignment - 1;
610 }
611
612 RawAddress = AllocatePool (AllocationSize + AlignmentMask);
613
614 AlignedAddress = ((UINTN) RawAddress + AlignmentMask) & ~AlignmentMask;
615
616 return (VOID *) AlignedAddress;
617 }
618
619 /**
620 Allocates a buffer of type EfiRuntimeServicesData at a specified alignment.
621
622 @param AllocationSize The number of bytes to allocate.
623 @param Alignment The requested alignment of the allocation. Must be a power of two.
624 If Alignment is zero, then byte alignment is used.
625
626 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
627 If there is not enough memory remaining to satisfy the request, then NULL is returned.
628
629 **/
630 VOID *
631 EFIAPI
632 AllocateAlignedRuntimePool (
633 IN UINTN AllocationSize,
634 IN UINTN Alignment
635 )
636 {
637 return InternalAllocateAlignedPool (EfiRuntimeServicesData, AllocationSize, Alignment);
638 }
639
640 /**
641 Allocates a buffer of type EfiReservedMemoryType at a specified alignment.
642
643 @param AllocationSize The number of bytes to allocate.
644 @param Alignment The requested alignment of the allocation. Must be a power of two.
645 If Alignment is zero, then byte alignment is used.
646
647 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
648 If there is not enough memory remaining to satisfy the request, then NULL is returned.
649
650 **/
651 VOID *
652 EFIAPI
653 AllocateAlignedReservedPool (
654 IN UINTN AllocationSize,
655 IN UINTN Alignment
656 )
657 {
658 return InternalAllocateAlignedPool (EfiReservedMemoryType, AllocationSize, Alignment);
659 }
660
661 /**
662 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
663
664 @param PoolType The type of pool to allocate.
665 @param AllocationSize The number of bytes to allocate.
666 @param Alignment The requested alignment of the allocation. Must be a power of two.
667 If Alignment is zero, then byte alignment is used.
668
669 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
670 If there is not enough memory remaining to satisfy the request, then NULL is returned.
671
672 **/
673 VOID *
674 InternalAllocateAlignedZeroPool (
675 IN EFI_MEMORY_TYPE PoolType,
676 IN UINTN AllocationSize,
677 IN UINTN Alignment
678 )
679 {
680 VOID *Memory;
681
682 Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);
683 if (Memory != NULL) {
684 Memory = ZeroMem (Memory, AllocationSize);
685 }
686 return Memory;
687 }
688
689 /**
690 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
691
692 @param AllocationSize The number of bytes to allocate.
693 @param Alignment The requested alignment of the allocation. Must be a power of two.
694 If Alignment is zero, then byte alignment is used.
695
696 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
697 If there is not enough memory remaining to satisfy the request, then NULL is returned.
698
699 **/
700 VOID *
701 EFIAPI
702 AllocateAlignedZeroPool (
703 IN UINTN AllocationSize,
704 IN UINTN Alignment
705 )
706 {
707 VOID *Memory;
708
709 Memory = AllocateAlignedPool (AllocationSize, Alignment);
710 if (Memory != NULL) {
711 Memory = ZeroMem (Memory, AllocationSize);
712 }
713 return Memory;
714 }
715
716 /**
717 Allocates and zeros a buffer of type EfiRuntimeServicesData at a specified alignment.
718
719 @param AllocationSize The number of bytes to allocate.
720 @param Alignment The requested alignment of the allocation. Must be a power of two.
721 If Alignment is zero, then byte alignment is used.
722
723 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
724 If there is not enough memory remaining to satisfy the request, then NULL is returned.
725
726 **/
727 VOID *
728 EFIAPI
729 AllocateAlignedRuntimeZeroPool (
730 IN UINTN AllocationSize,
731 IN UINTN Alignment
732 )
733 {
734 return InternalAllocateAlignedZeroPool (EfiRuntimeServicesData, AllocationSize, Alignment);
735 }
736
737 /**
738 Allocates and zeros a buffer of type EfiReservedMemoryType at a specified alignment.
739
740 @param AllocationSize The number of bytes to allocate.
741 @param Alignment The requested alignment of the allocation. Must be a power of two.
742 If Alignment is zero, then byte alignment is used.
743
744 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
745 If there is not enough memory remaining to satisfy the request, then NULL is returned.
746
747 **/
748 VOID *
749 EFIAPI
750 AllocateAlignedReservedZeroPool (
751 IN UINTN AllocationSize,
752 IN UINTN Alignment
753 )
754 {
755 return InternalAllocateAlignedZeroPool (EfiReservedMemoryType, AllocationSize, Alignment);
756 }
757
758 /**
759 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
760
761 @param PoolType The type of pool to allocate.
762 @param AllocationSize The number of bytes to allocate.
763 @param Buffer The buffer to copy to the allocated buffer.
764 @param Alignment The requested alignment of the allocation. Must be a power of two.
765 If Alignment is zero, then byte alignment is used.
766
767 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
768 If there is not enough memory remaining to satisfy the request, then NULL is returned.
769
770 **/
771 VOID *
772 InternalAllocateAlignedCopyPool (
773 IN EFI_MEMORY_TYPE PoolType,
774 IN UINTN AllocationSize,
775 IN CONST VOID *Buffer,
776 IN UINTN Alignment
777 )
778 {
779 VOID *Memory;
780
781 Memory = InternalAllocateAlignedPool (PoolType, AllocationSize, Alignment);
782 if (Memory != NULL) {
783 Memory = CopyMem (Memory, Buffer, AllocationSize);
784 }
785 return Memory;
786 }
787
788 /**
789 Copies a buffer to an allocated buffer of type EfiBootServicesData at a specified alignment.
790
791 @param AllocationSize The number of bytes to allocate.
792 @param Buffer The buffer to copy to the allocated buffer.
793 @param Alignment The requested alignment of the allocation. Must be a power of two.
794 If Alignment is zero, then byte alignment is used.
795
796 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
797 If there is not enough memory remaining to satisfy the request, then NULL is returned.
798
799 **/
800 VOID *
801 EFIAPI
802 AllocateAlignedCopyPool (
803 IN UINTN AllocationSize,
804 IN CONST VOID *Buffer,
805 IN UINTN Alignment
806 )
807 {
808 VOID *Memory;
809
810 Memory = AllocateAlignedPool (AllocationSize, Alignment);
811 if (Memory != NULL) {
812 Memory = CopyMem (Memory, Buffer, AllocationSize);
813 }
814 return Memory;
815 }
816
817 /**
818 Copies a buffer to an allocated buffer of type EfiRuntimeServicesData at a specified alignment.
819
820 @param AllocationSize The number of bytes to allocate.
821 @param Buffer The buffer to copy to the allocated buffer.
822 @param Alignment The requested alignment of the allocation. Must be a power of two.
823 If Alignment is zero, then byte alignment is used.
824
825 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
826 If there is not enough memory remaining to satisfy the request, then NULL is returned.
827
828 **/
829 VOID *
830 EFIAPI
831 AllocateAlignedRuntimeCopyPool (
832 IN UINTN AllocationSize,
833 IN CONST VOID *Buffer,
834 IN UINTN Alignment
835 )
836 {
837 return InternalAllocateAlignedCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer, Alignment);
838 }
839
840 /**
841 Copies a buffer to an allocated buffer of type EfiReservedMemoryType at a specified alignment.
842
843 @param AllocationSize The number of bytes to allocate.
844 @param Buffer The buffer to copy to the allocated buffer.
845 @param Alignment The requested alignment of the allocation. Must be a power of two.
846 If Alignment is zero, then byte alignment is used.
847
848 @return A pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned.
849 If there is not enough memory remaining to satisfy the request, then NULL is returned.
850
851 **/
852 VOID *
853 EFIAPI
854 AllocateAlignedReservedCopyPool (
855 IN UINTN AllocationSize,
856 IN CONST VOID *Buffer,
857 IN UINTN Alignment
858 )
859 {
860 return InternalAllocateAlignedCopyPool (EfiReservedMemoryType, AllocationSize, Buffer, Alignment);
861 }
862
863 /**
864 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
865 in the Memory Allocation Library.
866
867 @param Buffer Pointer to the buffer to free.
868
869 **/
870 VOID
871 EFIAPI
872 FreeAlignedPool (
873 IN VOID *Buffer
874 )
875 {
876 //
877 // PEI phase does not support to free pool, so leave it as NOP.
878 //
879 }