TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryAllocationLib.c
Go to the documentation of this file.
1
10#include <PiPei.h>
11
15#include <Library/DebugLib.h>
16#include <Library/HobLib.h>
17
31VOID *
32InternalAllocatePages (
33 IN EFI_MEMORY_TYPE MemoryType,
34 IN UINTN Pages
35 )
36{
37 EFI_STATUS Status;
39
40 if (Pages == 0) {
41 return NULL;
42 }
43
44 Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);
45 if (EFI_ERROR (Status)) {
46 return NULL;
47 }
48
49 return (VOID *)(UINTN)Memory;
50}
51
65VOID *
66EFIAPI
68 IN UINTN Pages
69 )
70{
71 return InternalAllocatePages (EfiBootServicesData, Pages);
72}
73
87VOID *
88EFIAPI
90 IN UINTN Pages
91 )
92{
93 return InternalAllocatePages (EfiRuntimeServicesData, Pages);
94}
95
109VOID *
110EFIAPI
112 IN UINTN Pages
113 )
114{
115 return InternalAllocatePages (EfiReservedMemoryType, Pages);
116}
117
135VOID
136EFIAPI
138 IN VOID *Buffer,
139 IN UINTN Pages
140 )
141{
142 EFI_STATUS Status;
143
144 ASSERT (Pages != 0);
145 Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);
146 ASSERT_EFI_ERROR (Status);
147}
148
168VOID *
170 IN EFI_MEMORY_TYPE MemoryType,
171 IN UINTN Pages,
172 IN UINTN Alignment
173 )
174{
175 EFI_STATUS Status;
177 UINTN AlignedMemory;
178 UINTN AlignmentMask;
179 UINTN UnalignedPages;
180 UINTN RealPages;
181
182 //
183 // Alignment must be a power of two or zero.
184 //
185 ASSERT ((Alignment & (Alignment - 1)) == 0);
186
187 if (Pages == 0) {
188 return NULL;
189 }
190
191 if (Alignment > EFI_PAGE_SIZE) {
192 //
193 // Calculate 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 = PeiServicesAllocatePages (MemoryType, RealPages, &Memory);
203 if (EFI_ERROR (Status)) {
204 return NULL;
205 }
206
207 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;
208 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);
209 if (UnalignedPages > 0) {
210 //
211 // Free first unaligned page(s).
212 //
213 Status = PeiServicesFreePages (Memory, UnalignedPages);
214 ASSERT_EFI_ERROR (Status);
215 }
216
217 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);
218 UnalignedPages = RealPages - Pages - UnalignedPages;
219 if (UnalignedPages > 0) {
220 //
221 // Free last unaligned page(s).
222 //
223 Status = PeiServicesFreePages (Memory, UnalignedPages);
224 ASSERT_EFI_ERROR (Status);
225 }
226 } else {
227 //
228 // Do not over-allocate pages in this case.
229 //
230 Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);
231 if (EFI_ERROR (Status)) {
232 return NULL;
233 }
234
235 AlignedMemory = (UINTN)Memory;
236 }
237
238 return (VOID *)AlignedMemory;
239}
240
260VOID *
261EFIAPI
263 IN UINTN Pages,
264 IN UINTN Alignment
265 )
266{
267 return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);
268}
269
289VOID *
290EFIAPI
292 IN UINTN Pages,
293 IN UINTN Alignment
294 )
295{
296 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
297}
298
318VOID *
319EFIAPI
321 IN UINTN Pages,
322 IN UINTN Alignment
323 )
324{
325 return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);
326}
327
345VOID
346EFIAPI
348 IN VOID *Buffer,
349 IN UINTN Pages
350 )
351{
352 EFI_STATUS Status;
353
354 ASSERT (Pages != 0);
355 Status = PeiServicesFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);
356 ASSERT_EFI_ERROR (Status);
357}
358
372VOID *
374 IN EFI_MEMORY_TYPE MemoryType,
375 IN UINTN AllocationSize
376 )
377{
378 //
379 // If we need lots of small runtime/reserved memory type from PEI in the future,
380 // we can consider providing a more complex algorithm that allocates runtime pages and
381 // provide pool allocations from those pages.
382 //
383 return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));
384}
385
398VOID *
399EFIAPI
401 IN UINTN AllocationSize
402 )
403{
404 EFI_STATUS Status;
405 VOID *Buffer;
406
407 Status = PeiServicesAllocatePool (AllocationSize, &Buffer);
408 if (EFI_ERROR (Status)) {
409 Buffer = NULL;
410 }
411
412 return Buffer;
413}
414
427VOID *
428EFIAPI
430 IN UINTN AllocationSize
431 )
432{
433 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
434}
435
448VOID *
449EFIAPI
451 IN UINTN AllocationSize
452 )
453{
454 return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);
455}
456
471VOID *
473 IN EFI_MEMORY_TYPE PoolType,
474 IN UINTN AllocationSize
475 )
476{
477 VOID *Memory;
478
479 Memory = InternalAllocatePool (PoolType, AllocationSize);
480 if (Memory != NULL) {
481 Memory = ZeroMem (Memory, AllocationSize);
482 }
483
484 return Memory;
485}
486
500VOID *
501EFIAPI
503 IN UINTN AllocationSize
504 )
505{
506 VOID *Memory;
507
508 Memory = AllocatePool (AllocationSize);
509 if (Memory != NULL) {
510 Memory = ZeroMem (Memory, AllocationSize);
511 }
512
513 return Memory;
514}
515
529VOID *
530EFIAPI
532 IN UINTN AllocationSize
533 )
534{
535 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
536}
537
551VOID *
552EFIAPI
554 IN UINTN AllocationSize
555 )
556{
557 return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);
558}
559
577VOID *
579 IN EFI_MEMORY_TYPE PoolType,
580 IN UINTN AllocationSize,
581 IN CONST VOID *Buffer
582 )
583{
584 VOID *Memory;
585
586 ASSERT (Buffer != NULL);
587 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));
588
589 Memory = InternalAllocatePool (PoolType, AllocationSize);
590 if (Memory != NULL) {
591 Memory = CopyMem (Memory, Buffer, AllocationSize);
592 }
593
594 return Memory;
595}
596
614VOID *
615EFIAPI
617 IN UINTN AllocationSize,
618 IN CONST VOID *Buffer
619 )
620{
621 VOID *Memory;
622
623 ASSERT (Buffer != NULL);
624 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));
625
626 Memory = AllocatePool (AllocationSize);
627 if (Memory != NULL) {
628 Memory = CopyMem (Memory, Buffer, AllocationSize);
629 }
630
631 return Memory;
632}
633
651VOID *
652EFIAPI
654 IN UINTN AllocationSize,
655 IN CONST VOID *Buffer
656 )
657{
658 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
659}
660
678VOID *
679EFIAPI
681 IN UINTN AllocationSize,
682 IN CONST VOID *Buffer
683 )
684{
685 return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);
686}
687
710VOID *
712 IN EFI_MEMORY_TYPE PoolType,
713 IN UINTN OldSize,
714 IN UINTN NewSize,
715 IN VOID *OldBuffer OPTIONAL
716 )
717{
718 VOID *NewBuffer;
719
720 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
721 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
722 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
723 FreePool (OldBuffer);
724 }
725
726 return NewBuffer;
727}
728
750VOID *
751EFIAPI
753 IN UINTN OldSize,
754 IN UINTN NewSize,
755 IN VOID *OldBuffer OPTIONAL
756 )
757{
758 return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);
759}
760
782VOID *
783EFIAPI
785 IN UINTN OldSize,
786 IN UINTN NewSize,
787 IN VOID *OldBuffer OPTIONAL
788 )
789{
790 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
791}
792
814VOID *
815EFIAPI
817 IN UINTN OldSize,
818 IN UINTN NewSize,
819 IN VOID *OldBuffer OPTIONAL
820 )
821{
822 return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);
823}
824
839VOID
840EFIAPI
842 IN VOID *Buffer
843 )
844{
845 //
846 // PEI phase does not support to free pool, so leave it as NOP.
847 //
848}
UINT64 UINTN
#define MAX_ADDRESS
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateAlignedPages(IN UINTN Pages, IN UINTN Alignment)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
VOID *EFIAPI ReallocatePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
VOID *EFIAPI AllocateRuntimePages(IN UINTN Pages)
VOID *EFIAPI AllocateReservedZeroPool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateRuntimePool(IN UINTN AllocationSize)
VOID *EFIAPI ReallocateRuntimePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID * InternalReallocatePool(IN EFI_MEMORY_TYPE PoolType, IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI AllocateAlignedReservedPages(IN UINTN Pages, IN UINTN Alignment)
VOID *EFIAPI AllocateRuntimeCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID *EFIAPI ReallocateReservedPool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID * InternalAllocateCopyPool(IN EFI_MEMORY_TYPE PoolType, IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID *EFIAPI AllocateReservedPool(IN UINTN AllocationSize)
VOID EFIAPI FreeAlignedPages(IN VOID *Buffer, IN UINTN Pages)
VOID *EFIAPI AllocateAlignedRuntimePages(IN UINTN Pages, IN UINTN Alignment)
VOID *EFIAPI AllocateReservedPages(IN UINTN Pages)
VOID * InternalAllocatePool(IN EFI_MEMORY_TYPE MemoryType, IN UINTN AllocationSize)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID *EFIAPI AllocateRuntimeZeroPool(IN UINTN AllocationSize)
VOID * InternalAllocateAlignedPages(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, IN UINTN Alignment)
VOID * InternalAllocateZeroPool(IN EFI_MEMORY_TYPE PoolType, IN UINTN AllocationSize)
VOID *EFIAPI AllocateReservedCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_STATUS EFIAPI PeiServicesAllocatePool(IN UINTN Size, OUT VOID **Buffer)
EFI_STATUS EFIAPI PeiServicesAllocatePages(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT EFI_PHYSICAL_ADDRESS *Memory)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define MIN(a, b)
Definition: Base.h:1007
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
EFI_STATUS EFIAPI PeiServicesFreePages(IN EFI_PHYSICAL_ADDRESS Memory, IN UINTN Pages)
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
EFI_MEMORY_TYPE
@ EfiBootServicesData
@ EfiReservedMemoryType
@ EfiRuntimeServicesData