TianoCore EDK2 master
Loading...
Searching...
No Matches
StandaloneMmMemoryAllocationLib.c
Go to the documentation of this file.
1
11#include <PiMm.h>
12
14#include <Library/DebugLib.h>
17
31VOID *
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 = gMmst->MmAllocatePages (AllocateAnyPages, 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{
72}
73
87VOID *
88EFIAPI
90 IN UINTN Pages
91 )
92{
94}
95
109VOID *
110EFIAPI
112 IN UINTN Pages
113 )
114{
115 return NULL;
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 = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);
146 ASSERT_EFI_ERROR (Status);
147}
148
167VOID *
169 IN EFI_MEMORY_TYPE MemoryType,
170 IN UINTN Pages,
171 IN UINTN Alignment
172 )
173{
174 EFI_STATUS Status;
176 UINTN AlignedMemory;
177 UINTN AlignmentMask;
178 UINTN UnalignedPages;
179 UINTN RealPages;
180
181 //
182 // Alignment must be a power of two or zero.
183 //
184 ASSERT ((Alignment & (Alignment - 1)) == 0);
185
186 if (Pages == 0) {
187 return NULL;
188 }
189
190 if (Alignment > EFI_PAGE_SIZE) {
191 //
192 // Calculate the total number of pages since alignment is larger than page size.
193 //
194 AlignmentMask = Alignment - 1;
195 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);
196 //
197 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
198 //
199 ASSERT (RealPages > Pages);
200
201 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, RealPages, &Memory);
202 if (EFI_ERROR (Status)) {
203 return NULL;
204 }
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 = gMmst->MmFreePages (Memory, UnalignedPages);
213 ASSERT_EFI_ERROR (Status);
214 }
215
216 Memory = (EFI_PHYSICAL_ADDRESS)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
217 UnalignedPages = RealPages - Pages - UnalignedPages;
218 if (UnalignedPages > 0) {
219 //
220 // Free last unaligned page(s).
221 //
222 Status = gMmst->MmFreePages (Memory, UnalignedPages);
223 ASSERT_EFI_ERROR (Status);
224 }
225 } else {
226 //
227 // Do not over-allocate pages in this case.
228 //
229 Status = gMmst->MmAllocatePages (AllocateAnyPages, MemoryType, Pages, &Memory);
230 if (EFI_ERROR (Status)) {
231 return NULL;
232 }
233
234 AlignedMemory = (UINTN)Memory;
235 }
236
237 return (VOID *)AlignedMemory;
238}
239
258VOID *
259EFIAPI
261 IN UINTN Pages,
262 IN UINTN Alignment
263 )
264{
265 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
266}
267
286VOID *
287EFIAPI
289 IN UINTN Pages,
290 IN UINTN Alignment
291 )
292{
293 return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);
294}
295
314VOID *
315EFIAPI
317 IN UINTN Pages,
318 IN UINTN Alignment
319 )
320{
321 return NULL;
322}
323
341VOID
342EFIAPI
344 IN VOID *Buffer,
345 IN UINTN Pages
346 )
347{
348 EFI_STATUS Status;
349
350 ASSERT (Pages != 0);
351 Status = gMmst->MmFreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, Pages);
352 ASSERT_EFI_ERROR (Status);
353}
354
368VOID *
370 IN EFI_MEMORY_TYPE MemoryType,
371 IN UINTN AllocationSize
372 )
373{
374 EFI_STATUS Status;
375 VOID *Memory;
376
377 Memory = NULL;
378
379 Status = gMmst->MmAllocatePool (MemoryType, AllocationSize, &Memory);
380 if (EFI_ERROR (Status)) {
381 Memory = NULL;
382 }
383
384 return Memory;
385}
386
399VOID *
400EFIAPI
402 IN UINTN AllocationSize
403 )
404{
405 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
406}
407
420VOID *
421EFIAPI
423 IN UINTN AllocationSize
424 )
425{
426 return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);
427}
428
441VOID *
442EFIAPI
444 IN UINTN AllocationSize
445 )
446{
447 return NULL;
448}
449
464VOID *
466 IN EFI_MEMORY_TYPE PoolType,
467 IN UINTN AllocationSize
468 )
469{
470 VOID *Memory;
471
472 Memory = InternalAllocatePool (PoolType, AllocationSize);
473 if (Memory != NULL) {
474 Memory = ZeroMem (Memory, AllocationSize);
475 }
476
477 return Memory;
478}
479
493VOID *
494EFIAPI
496 IN UINTN AllocationSize
497 )
498{
499 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
500}
501
515VOID *
516EFIAPI
518 IN UINTN AllocationSize
519 )
520{
521 return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);
522}
523
537VOID *
538EFIAPI
540 IN UINTN AllocationSize
541 )
542{
543 return NULL;
544}
545
563VOID *
565 IN EFI_MEMORY_TYPE PoolType,
566 IN UINTN AllocationSize,
567 IN CONST VOID *Buffer
568 )
569{
570 VOID *Memory;
571
572 ASSERT (Buffer != NULL);
573 ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN)Buffer + 1));
574
575 Memory = InternalAllocatePool (PoolType, AllocationSize);
576 if (Memory != NULL) {
577 Memory = CopyMem (Memory, Buffer, AllocationSize);
578 }
579
580 return Memory;
581}
582
600VOID *
601EFIAPI
603 IN UINTN AllocationSize,
604 IN CONST VOID *Buffer
605 )
606{
607 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
608}
609
627VOID *
628EFIAPI
630 IN UINTN AllocationSize,
631 IN CONST VOID *Buffer
632 )
633{
634 return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);
635}
636
654VOID *
655EFIAPI
657 IN UINTN AllocationSize,
658 IN CONST VOID *Buffer
659 )
660{
661 return NULL;
662}
663
686VOID *
688 IN EFI_MEMORY_TYPE PoolType,
689 IN UINTN OldSize,
690 IN UINTN NewSize,
691 IN VOID *OldBuffer OPTIONAL
692 )
693{
694 VOID *NewBuffer;
695
696 NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);
697 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
698 CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
699 FreePool (OldBuffer);
700 }
701
702 return NewBuffer;
703}
704
726VOID *
727EFIAPI
729 IN UINTN OldSize,
730 IN UINTN NewSize,
731 IN VOID *OldBuffer OPTIONAL
732 )
733{
734 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
735}
736
758VOID *
759EFIAPI
761 IN UINTN OldSize,
762 IN UINTN NewSize,
763 IN VOID *OldBuffer OPTIONAL
764 )
765{
766 return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);
767}
768
790VOID *
791EFIAPI
793 IN UINTN OldSize,
794 IN UINTN NewSize,
795 IN VOID *OldBuffer OPTIONAL
796 )
797{
798 return NULL;
799}
800
815VOID
816EFIAPI
818 IN VOID *Buffer
819 )
820{
821 EFI_STATUS Status;
822
823 Status = gMmst->MmFreePool (Buffer);
824 ASSERT_EFI_ERROR (Status);
825}
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)
#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
VOID *EFIAPI AllocateReservedZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
VOID *EFIAPI AllocateRuntimePool(IN UINTN AllocationSize)
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateAlignedPages(IN UINTN Pages, IN UINTN Alignment)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
VOID *EFIAPI ReallocateRuntimePool(IN UINTN OldSize, IN UINTN NewSize, IN VOID *OldBuffer OPTIONAL)
VOID *EFIAPI ReallocatePool(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 AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
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 AllocateRuntimePages(IN UINTN Pages)
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)
VOID * InternalAllocatePages(IN EFI_MEMORY_TYPE MemoryType, 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
@ EfiRuntimeServicesData
@ AllocateAnyPages
Definition: UefiSpec.h:33
EFI_ALLOCATE_POOL MmAllocatePool
Definition: PiMmCis.h:274