MemoryAllocationLib.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*++ @file
  2. Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "Base.h"
  6. #include "Library/BaseMemoryLib.h"
  7. #include "Library/MemoryAllocationLib.h"
  8. #include <stdlib.h>
  9. /**
  10. Allocates a buffer of type EfiBootServicesData.
  11. Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
  12. pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
  13. returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
  14. @param AllocationSize The number of bytes to allocate.
  15. @return A pointer to the allocated buffer or NULL if allocation fails.
  16. **/
  17. VOID *
  18. EFIAPI
  19. AllocatePool (
  20. IN UINTN AllocationSize
  21. )
  22. {
  23. return (VOID *)malloc (AllocationSize);
  24. }
  25. /**
  26. Allocates and zeros a buffer of type EfiBootServicesData.
  27. Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
  28. buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
  29. valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
  30. request, then NULL is returned.
  31. @param AllocationSize The number of bytes to allocate and zero.
  32. @return A pointer to the allocated buffer or NULL if allocation fails.
  33. **/
  34. VOID *
  35. EFIAPI
  36. AllocateZeroPool (
  37. IN UINTN AllocationSize
  38. )
  39. {
  40. VOID *Buffer;
  41. Buffer = AllocatePool (AllocationSize);
  42. if (Buffer == NULL) {
  43. return NULL;
  44. }
  45. ZeroMem (Buffer, AllocationSize);
  46. return Buffer;
  47. }
  48. /**
  49. Reallocates a buffer of type EfiBootServicesData.
  50. Allocates and zeros the number bytes specified by NewSize from memory of type
  51. EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and
  52. NewSize bytes are copied from OldBuffer to the newly allocated buffer, and
  53. OldBuffer is freed. A pointer to the newly allocated buffer is returned.
  54. If NewSize is 0, then a valid buffer of 0 size is returned. If there is not
  55. enough memory remaining to satisfy the request, then NULL is returned.
  56. If the allocation of the new buffer is successful and the smaller of NewSize and OldSize
  57. is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().
  58. @param OldSize The size, in bytes, of OldBuffer.
  59. @param NewSize The size, in bytes, of the buffer to reallocate.
  60. @param OldBuffer The buffer to copy to the allocated buffer. This is an optional
  61. parameter that may be NULL.
  62. @return A pointer to the allocated buffer or NULL if allocation fails.
  63. **/
  64. VOID *
  65. EFIAPI
  66. ReallocatePool (
  67. IN UINTN OldSize,
  68. IN UINTN NewSize,
  69. IN VOID *OldBuffer OPTIONAL
  70. )
  71. {
  72. VOID *NewBuffer;
  73. NewBuffer = AllocatePool (NewSize);
  74. if (NewBuffer == NULL) {
  75. return NULL;
  76. }
  77. if (OldBuffer != NULL) {
  78. if (OldSize > 0) {
  79. CopyMem (NewBuffer, OldBuffer, OldSize);
  80. }
  81. FreePool (OldBuffer);
  82. }
  83. return NewBuffer;
  84. }
  85. /**
  86. Frees a buffer that was previously allocated with one of the pool allocation functions in the
  87. Memory Allocation Library.
  88. Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
  89. pool allocation services of the Memory Allocation Library. If it is not possible to free pool
  90. resources, then this function will perform no actions.
  91. If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
  92. then ASSERT().
  93. @param Buffer Pointer to the buffer to free.
  94. **/
  95. VOID
  96. EFIAPI
  97. FreePool (
  98. IN VOID *Buffer
  99. )
  100. {
  101. free ((void *)Buffer);
  102. }