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