MemoryAllocation.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /** @file
  2. Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
  3. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "UefiPayloadEntry.h"
  7. /**
  8. Allocates one or more pages of type EfiBootServicesData.
  9. Allocates the number of pages of MemoryType and returns a pointer to the
  10. allocated buffer. The buffer returned is aligned on a 4KB boundary.
  11. If Pages is 0, then NULL is returned.
  12. If there is not enough memory availble to satisfy the request, then NULL
  13. is returned.
  14. @param Pages The number of 4 KB pages to allocate.
  15. @return A pointer to the allocated buffer or NULL if allocation fails.
  16. **/
  17. VOID *
  18. EFIAPI
  19. AllocatePages (
  20. IN UINTN Pages
  21. )
  22. {
  23. EFI_PEI_HOB_POINTERS Hob;
  24. EFI_PHYSICAL_ADDRESS Offset;
  25. EFI_HOB_HANDOFF_INFO_TABLE *HobTable;
  26. Hob.Raw = GetHobList ();
  27. HobTable = Hob.HandoffInformationTable;
  28. if (Pages == 0) {
  29. return NULL;
  30. }
  31. // Make sure allocation address is page alligned.
  32. Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
  33. if (Offset != 0) {
  34. HobTable->EfiFreeMemoryTop -= Offset;
  35. }
  36. //
  37. // Check available memory for the allocation
  38. //
  39. if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
  40. return NULL;
  41. }
  42. HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
  43. BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, EfiBootServicesData);
  44. return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
  45. }
  46. /**
  47. Frees one or more 4KB pages that were previously allocated with one of the page allocation
  48. functions in the Memory Allocation Library.
  49. Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
  50. must have been allocated on a previous call to the page allocation services of the Memory
  51. Allocation Library. If it is not possible to free allocated pages, then this function will
  52. perform no actions.
  53. If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
  54. then ASSERT().
  55. If Pages is zero, then ASSERT().
  56. @param Buffer Pointer to the buffer of pages to free.
  57. @param Pages The number of 4 KB pages to free.
  58. **/
  59. VOID
  60. EFIAPI
  61. FreePages (
  62. IN VOID *Buffer,
  63. IN UINTN Pages
  64. )
  65. {
  66. }
  67. /**
  68. Allocates one or more pages of type EfiBootServicesData at a specified alignment.
  69. Allocates the number of pages specified by Pages of type EfiBootServicesData with an
  70. alignment specified by Alignment.
  71. If Pages is 0, then NULL is returned.
  72. If Alignment is not a power of two and Alignment is not zero, then ASSERT().
  73. If there is no enough memory at the specified alignment available to satisfy the
  74. request, then NULL is returned.
  75. @param Pages The number of 4 KB pages to allocate.
  76. @param Alignment The requested alignment of the allocation.
  77. @return A pointer to the allocated buffer or NULL if allocation fails.
  78. **/
  79. VOID *
  80. EFIAPI
  81. AllocateAlignedPages (
  82. IN UINTN Pages,
  83. IN UINTN Alignment
  84. )
  85. {
  86. VOID *Memory;
  87. UINTN AlignmentMask;
  88. //
  89. // Alignment must be a power of two or zero.
  90. //
  91. ASSERT ((Alignment & (Alignment - 1)) == 0);
  92. if (Pages == 0) {
  93. return NULL;
  94. }
  95. //
  96. // Check overflow.
  97. //
  98. ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
  99. Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
  100. if (Memory == NULL) {
  101. return NULL;
  102. }
  103. if (Alignment == 0) {
  104. AlignmentMask = Alignment;
  105. } else {
  106. AlignmentMask = Alignment - 1;
  107. }
  108. return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
  109. }
  110. /**
  111. Allocates a buffer of type EfiBootServicesData.
  112. Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
  113. pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
  114. returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
  115. @param AllocationSize The number of bytes to allocate.
  116. @return A pointer to the allocated buffer or NULL if allocation fails.
  117. **/
  118. VOID *
  119. EFIAPI
  120. AllocatePool (
  121. IN UINTN AllocationSize
  122. )
  123. {
  124. EFI_HOB_MEMORY_POOL *Hob;
  125. if (AllocationSize > 0x4000) {
  126. // Please use AllocatePages for big allocations
  127. return NULL;
  128. }
  129. Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + AllocationSize));
  130. return (VOID *)(Hob + 1);
  131. }
  132. /**
  133. Allocates and zeros a buffer of type EfiBootServicesData.
  134. Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
  135. buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
  136. valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
  137. request, then NULL is returned.
  138. @param AllocationSize The number of bytes to allocate and zero.
  139. @return A pointer to the allocated buffer or NULL if allocation fails.
  140. **/
  141. VOID *
  142. EFIAPI
  143. AllocateZeroPool (
  144. IN UINTN AllocationSize
  145. )
  146. {
  147. VOID *Buffer;
  148. Buffer = AllocatePool (AllocationSize);
  149. if (Buffer == NULL) {
  150. return NULL;
  151. }
  152. ZeroMem (Buffer, AllocationSize);
  153. return Buffer;
  154. }