MyAlloc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /** @file
  2. Header file for memory allocation tracking functions.
  3. Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #ifndef _MYALLOC_H_
  7. #define _MYALLOC_H_
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <Common/BaseTypes.h>
  12. //
  13. // Default operation is to use the memory allocation tracking functions.
  14. // To over-ride add "#define USE_MYALLOC 0" to your program header and/or
  15. // source files as needed. Or, just do not include this header file in
  16. // your project.
  17. //
  18. #ifndef USE_MYALLOC
  19. #define USE_MYALLOC 1
  20. #endif
  21. #if USE_MYALLOC
  22. //
  23. // Replace C library allocation routines with MyAlloc routines.
  24. //
  25. #define malloc(size) MyAlloc ((size), __FILE__, __LINE__)
  26. #define calloc(count, size) MyAlloc ((count) * (size), __FILE__, __LINE__)
  27. #define realloc(ptr, size) MyRealloc ((ptr), (size), __FILE__, __LINE__)
  28. #define free(ptr) MyFree ((ptr), __FILE__, __LINE__)
  29. #define alloc_check(final) MyCheck ((final), __FILE__, __LINE__)
  30. //
  31. // Structure for checking/tracking memory allocations.
  32. //
  33. typedef struct MyAllocStruct {
  34. UINTN Cksum;
  35. struct MyAllocStruct *Next;
  36. UINTN Line;
  37. UINTN Size;
  38. UINT8 *File;
  39. UINT8 *Buffer;
  40. } MY_ALLOC_STRUCT;
  41. //
  42. // Cksum := (UINTN)This + (UINTN)Next + Line + Size + (UINTN)File +
  43. // (UINTN)Buffer;
  44. //
  45. // Next := Pointer to next allocation structure in the list.
  46. //
  47. // Line := __LINE__
  48. //
  49. // Size := Size of allocation request.
  50. //
  51. // File := Pointer to __FILE__ string stored immediately following
  52. // MY_ALLOC_STRUCT in memory.
  53. //
  54. // Buffer := Pointer to UINT32 aligned storage immediately following
  55. // the NULL terminated __FILE__ string. This is UINT32
  56. // aligned because the underflow signature is 32-bits and
  57. // this will place the first caller address on a 64-bit
  58. // boundary.
  59. //
  60. //
  61. // Signatures used to check for buffer overflow/underflow conditions.
  62. //
  63. #define MYALLOC_HEAD_MAGIK 0xBADFACED
  64. #define MYALLOC_TAIL_MAGIK 0xDEADBEEF
  65. VOID
  66. MyCheck (
  67. BOOLEAN Final,
  68. UINT8 File[],
  69. UINTN Line
  70. )
  71. ;
  72. //
  73. // *++
  74. // Description:
  75. //
  76. // Check for corruptions in the allocated memory chain. If a corruption
  77. // is detection program operation stops w/ an exit(1) call.
  78. //
  79. // Parameters:
  80. //
  81. // Final := When FALSE, MyCheck() returns if the allocated memory chain
  82. // has not been corrupted. When TRUE, MyCheck() returns if there
  83. // are no un-freed allocations. If there are un-freed allocations,
  84. // they are displayed and exit(1) is called.
  85. //
  86. //
  87. // File := Set to __FILE__ by macro expansion.
  88. //
  89. // Line := Set to __LINE__ by macro expansion.
  90. //
  91. // Returns:
  92. //
  93. // n/a
  94. //
  95. // --*/
  96. //
  97. VOID *
  98. MyAlloc (
  99. UINTN Size,
  100. UINT8 File[],
  101. UINTN Line
  102. )
  103. ;
  104. //
  105. // *++
  106. // Description:
  107. //
  108. // Allocate a new link in the allocation chain along with enough storage
  109. // for the File[] string, requested Size and alignment overhead. If
  110. // memory cannot be allocated or the allocation chain has been corrupted,
  111. // exit(1) will be called.
  112. //
  113. // Parameters:
  114. //
  115. // Size := Number of bytes (UINT8) requested by the called.
  116. // Size cannot be zero.
  117. //
  118. // File := Set to __FILE__ by macro expansion.
  119. //
  120. // Line := Set to __LINE__ by macro expansion.
  121. //
  122. // Returns:
  123. //
  124. // Pointer to the caller's buffer.
  125. //
  126. // --*/
  127. //
  128. VOID *
  129. MyRealloc (
  130. VOID *Ptr,
  131. UINTN Size,
  132. UINT8 File[],
  133. UINTN Line
  134. )
  135. ;
  136. //
  137. // *++
  138. // Description:
  139. //
  140. // This does a MyAlloc(), memcpy() and MyFree(). There is no optimization
  141. // for shrinking or expanding buffers. An invalid parameter will cause
  142. // MyRealloc() to fail with a call to exit(1).
  143. //
  144. // Parameters:
  145. //
  146. // Ptr := Pointer to the caller's buffer to be re-allocated.
  147. // Ptr cannot be NULL.
  148. //
  149. // Size := Size of new buffer. Size cannot be zero.
  150. //
  151. // File := Set to __FILE__ by macro expansion.
  152. //
  153. // Line := Set to __LINE__ by macro expansion.
  154. //
  155. // Returns:
  156. //
  157. // Pointer to new caller's buffer.
  158. //
  159. // --*/
  160. //
  161. VOID
  162. MyFree (
  163. VOID *Ptr,
  164. UINT8 File[],
  165. UINTN Line
  166. )
  167. ;
  168. //
  169. // *++
  170. // Description:
  171. //
  172. // Release a previously allocated buffer. Invalid parameters will cause
  173. // MyFree() to fail with an exit(1) call.
  174. //
  175. // Parameters:
  176. //
  177. // Ptr := Pointer to the caller's buffer to be freed.
  178. // A NULL pointer will be ignored.
  179. //
  180. // File := Set to __FILE__ by macro expansion.
  181. //
  182. // Line := Set to __LINE__ by macro expansion.
  183. //
  184. // Returns:
  185. //
  186. // n/a
  187. //
  188. // --*/
  189. //
  190. #else /* USE_MYALLOC */
  191. //
  192. // Nothing to do when USE_MYALLOC is zero.
  193. //
  194. #define alloc_check(final)
  195. #endif /* USE_MYALLOC */
  196. #endif /* _MYALLOC_H_ */
  197. /* eof - MyAlloc.h */