TianoCore EDK2 master
Loading...
Searching...
No Matches
MemoryAllocationLibPosix.c
Go to the documentation of this file.
1
10#include <stdlib.h>
11#include <string.h>
12
13#include <Uefi.h>
15#include <Library/DebugLib.h>
16
21#define PAGE_HEAD_PRIVATE_SIGNATURE SIGNATURE_32 ('P', 'H', 'D', 'R')
22
28typedef struct {
29 UINT32 Signature;
30 VOID *AllocatedBufffer;
31 UINTN TotalPages;
32 VOID *AlignedBuffer;
33 UINTN AlignedPages;
34} PAGE_HEAD;
35
49VOID *
50EFIAPI
52 IN UINTN Pages
53 )
54{
55 return AllocateAlignedPages (Pages, SIZE_4KB);
56}
57
71VOID *
72EFIAPI
74 IN UINTN Pages
75 )
76{
77 return AllocatePages (Pages);
78}
79
93VOID *
94EFIAPI
96 IN UINTN Pages
97 )
98{
99 return AllocatePages (Pages);
100}
101
119VOID
120EFIAPI
122 IN VOID *Buffer,
123 IN UINTN Pages
124 )
125{
126 FreeAlignedPages (Buffer, Pages);
127}
128VOID *
147EFIAPI
149 IN UINTN Pages,
150 IN UINTN Alignment
151 )
152{
153 PAGE_HEAD PageHead;
154 PAGE_HEAD *PageHeadPtr;
155 UINTN AlignmentMask;
156
157 ASSERT ((Alignment & (Alignment - 1)) == 0);
158
159 if (Alignment < SIZE_4KB) {
160 Alignment = SIZE_4KB;
161 }
162
163 AlignmentMask = Alignment - 1;
164
165 //
166 // We need reserve Alignment pages for PAGE_HEAD, as meta data.
167 //
168 PageHead.Signature = PAGE_HEAD_PRIVATE_SIGNATURE;
169 PageHead.TotalPages = Pages + EFI_SIZE_TO_PAGES (Alignment) * 2;
170 PageHead.AlignedPages = Pages;
171 PageHead.AllocatedBufffer = malloc (EFI_PAGES_TO_SIZE (PageHead.TotalPages));
172 if (PageHead.AllocatedBufffer == NULL) {
173 return NULL;
174 }
175
176 PageHead.AlignedBuffer = (VOID *)(((UINTN)PageHead.AllocatedBufffer + AlignmentMask) & ~AlignmentMask);
177 if ((UINTN)PageHead.AlignedBuffer - (UINTN)PageHead.AllocatedBufffer < sizeof (PAGE_HEAD)) {
178 PageHead.AlignedBuffer = (VOID *)((UINTN)PageHead.AlignedBuffer + Alignment);
179 }
180
181 PageHeadPtr = (VOID *)((UINTN)PageHead.AlignedBuffer - sizeof (PAGE_HEAD));
182 memcpy (PageHeadPtr, &PageHead, sizeof (PAGE_HEAD));
183
184 return PageHead.AlignedBuffer;
185}
186
205VOID *
206EFIAPI
208 IN UINTN Pages,
209 IN UINTN Alignment
210 )
211{
212 return AllocateAlignedPages (Pages, Alignment);
213}
214
233VOID *
234EFIAPI
236 IN UINTN Pages,
237 IN UINTN Alignment
238 )
239{
240 return AllocateAlignedPages (Pages, Alignment);
241}
242
260VOID
261EFIAPI
263 IN VOID *Buffer,
264 IN UINTN Pages
265 )
266{
267 PAGE_HEAD *PageHeadPtr;
268
269 //
270 // NOTE: Partial free is not supported. Just keep it.
271 //
272 PageHeadPtr = (VOID *)((UINTN)Buffer - sizeof (PAGE_HEAD));
273 if (PageHeadPtr->Signature != PAGE_HEAD_PRIVATE_SIGNATURE) {
274 return;
275 }
276
277 if (PageHeadPtr->AlignedPages != Pages) {
278 return;
279 }
280
281 PageHeadPtr->Signature = 0;
282 free (PageHeadPtr->AllocatedBufffer);
283}
284VOID *
297EFIAPI
299 IN UINTN AllocationSize
300 )
301{
302 return malloc (AllocationSize);
303}
304
317VOID *
318EFIAPI
320 IN UINTN AllocationSize
321 )
322{
323 return AllocatePool (AllocationSize);
324}
325
338VOID *
339EFIAPI
341 IN UINTN AllocationSize
342 )
343{
344 return AllocatePool (AllocationSize);
345}
346
360VOID *
361EFIAPI
363 IN UINTN AllocationSize
364 )
365{
366 VOID *Buffer;
367
368 Buffer = malloc (AllocationSize);
369 if (Buffer == NULL) {
370 return NULL;
371 }
372
373 memset (Buffer, 0, AllocationSize);
374 return Buffer;
375}
376
390VOID *
391EFIAPI
393 IN UINTN AllocationSize
394 )
395{
396 return AllocateZeroPool (AllocationSize);
397}
398
412VOID *
413EFIAPI
415 IN UINTN AllocationSize
416 )
417{
418 return AllocateZeroPool (AllocationSize);
419}
420
438VOID *
439EFIAPI
441 IN UINTN AllocationSize,
442 IN CONST VOID *Buffer
443 )
444{
445 VOID *Memory;
446
447 Memory = malloc (AllocationSize);
448 if (Memory == NULL) {
449 return NULL;
450 }
451
452 memcpy (Memory, Buffer, AllocationSize);
453 return Memory;
454}
455
473VOID *
474EFIAPI
476 IN UINTN AllocationSize,
477 IN CONST VOID *Buffer
478 )
479{
480 return AllocateCopyPool (AllocationSize, Buffer);
481}
482
500VOID *
501EFIAPI
503 IN UINTN AllocationSize,
504 IN CONST VOID *Buffer
505 )
506{
507 return AllocateCopyPool (AllocationSize, Buffer);
508}
509
531VOID *
532EFIAPI
534 IN UINTN OldSize,
535 IN UINTN NewSize,
536 IN VOID *OldBuffer OPTIONAL
537 )
538{
539 VOID *NewBuffer;
540
541 NewBuffer = malloc (NewSize);
542 if ((NewBuffer != NULL) && (OldBuffer != NULL)) {
543 memcpy (NewBuffer, OldBuffer, MIN (OldSize, NewSize));
544 }
545
546 if (OldBuffer != NULL) {
547 FreePool (OldBuffer);
548 }
549
550 return NewBuffer;
551}
552
574VOID *
575EFIAPI
577 IN UINTN OldSize,
578 IN UINTN NewSize,
579 IN VOID *OldBuffer OPTIONAL
580 )
581{
582 return ReallocatePool (OldSize, NewSize, OldBuffer);
583}
584
606VOID *
607EFIAPI
609 IN UINTN OldSize,
610 IN UINTN NewSize,
611 IN VOID *OldBuffer OPTIONAL
612 )
613{
614 return ReallocatePool (OldSize, NewSize, OldBuffer);
615}
616
631VOID
632EFIAPI
634 IN VOID *Buffer
635 )
636{
637 free (Buffer);
638}
UINT64 UINTN
#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
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 *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 *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 *EFIAPI AllocateRuntimePages(IN UINTN Pages)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
VOID *EFIAPI AllocateRuntimeZeroPool(IN UINTN AllocationSize)
VOID *EFIAPI AllocateReservedCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
#define PAGE_HEAD_PRIVATE_SIGNATURE
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200