Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. memory.h
  5. Abstract:
  6. Implements macros and declares functions for basic allocation functions.
  7. Author:
  8. Marc R. Whitten (marcw) 09-Sep-1999
  9. Revision History:
  10. jimschm 25-Jul-2001 Updated for consistent coding conventions
  11. --*/
  12. #pragma once
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. //
  17. // Constants
  18. //
  19. #define INVALID_PTR ((PVOID)-1)
  20. #undef INITIALIZE_MEMORY_CODE
  21. #define INITIALIZE_MEMORY_CODE if (!MemInitialize()) { __leave; }
  22. //
  23. // Globals
  24. //
  25. extern HANDLE g_hHeap;
  26. //
  27. // Function Prototypes
  28. //
  29. BOOL
  30. MemInitialize (
  31. VOID
  32. );
  33. //
  34. // Reusable memory alloc, kind of like a GROWBUFFER but more simple. Here is
  35. // an example of how it might be used:
  36. //
  37. // buffer = NULL;
  38. //
  39. // while (pGetAnItemIndex (&i)) {
  40. // size = pComputeBufferSizeForThisItem (i);
  41. // buffer = (PTSTR) MemReuseAlloc (g_hHeap, ptr, size);
  42. // pProcessSomething (i, buffer);
  43. // }
  44. //
  45. // MemReuseFree (buffer);
  46. //
  47. // Allocations are always rounded up to the next 1K boundary, and allocations
  48. // occur only when the buffer is too small or hasn't been allocated.
  49. //
  50. PVOID
  51. MemReuseAlloc (
  52. IN HANDLE Heap,
  53. IN PVOID OldPtr, OPTIONAL
  54. IN DWORD SizeNeeded
  55. );
  56. VOID
  57. MemReuseFree (
  58. IN HANDLE Heap,
  59. IN PVOID Ptr
  60. );
  61. #ifdef DEBUG
  62. //
  63. // Fast allocation routines (tracked versions)
  64. //
  65. PVOID
  66. DbgFastAlloc (
  67. IN PCSTR SourceFile,
  68. IN DWORD Line,
  69. IN SIZE_T Size
  70. );
  71. PVOID
  72. DbgFastReAlloc (
  73. IN PCSTR SourceFile,
  74. IN DWORD Line,
  75. IN PCVOID OldBlock,
  76. IN SIZE_T Size
  77. );
  78. BOOL
  79. DbgFastFree (
  80. IN PCSTR SourceFile,
  81. IN DWORD Line,
  82. IN PCVOID Block
  83. );
  84. PVOID
  85. DbgFastAllocNeverFail (
  86. IN PCSTR SourceFile,
  87. IN DWORD Line,
  88. IN SIZE_T Size
  89. );
  90. PVOID
  91. DbgFastReAllocNeverFail (
  92. IN PCSTR SourceFile,
  93. IN DWORD Line,
  94. IN PCVOID OldBlock,
  95. IN SIZE_T Size
  96. );
  97. #define MemFastAlloc(size) DbgFastAlloc(__FILE__,__LINE__,size)
  98. #define MemFastReAlloc(oldblock,size) DbgFastReAlloc(__FILE__,__LINE__,oldblock,size)
  99. #define MemFastFree(block) DbgFastFree(__FILE__,__LINE__,block)
  100. #define MemFastAllocNeverFail(size) DbgFastAllocNeverFail(__FILE__,__LINE__,size)
  101. #define MemFastReAllocNeverFail(oldblock,size) DbgFastReAllocNeverFail(__FILE__,__LINE__,oldblock,size)
  102. //
  103. // Regular heap access (tracked versions)
  104. //
  105. PVOID
  106. DbgHeapAlloc (
  107. IN PCSTR SourceFile,
  108. IN DWORD Line,
  109. IN HANDLE Heap,
  110. IN DWORD Flags,
  111. IN SIZE_T Size
  112. );
  113. PVOID
  114. DbgHeapReAlloc (
  115. IN PCSTR SourceFile,
  116. IN DWORD Line,
  117. IN HANDLE Heap,
  118. IN DWORD Flags,
  119. IN PCVOID Mem,
  120. IN SIZE_T Size
  121. );
  122. PVOID
  123. DbgHeapAllocNeverFail (
  124. IN PCSTR SourceFile,
  125. IN DWORD Line,
  126. IN HANDLE Heap,
  127. IN DWORD Flags,
  128. IN SIZE_T Size
  129. );
  130. PVOID
  131. DbgHeapReAllocNeverFail (
  132. IN PCSTR SourceFile,
  133. IN DWORD Line,
  134. IN HANDLE Heap,
  135. IN DWORD Flags,
  136. IN PCVOID Mem,
  137. IN SIZE_T Size
  138. );
  139. BOOL
  140. DbgHeapFree (
  141. IN PCSTR SourceFile,
  142. IN DWORD Line,
  143. IN HANDLE Heap,
  144. IN DWORD Flags,
  145. IN PCVOID Mem
  146. );
  147. #define MemAllocNeverFail(heap,flags,size) DbgHeapAllocNeverFail(__FILE__,__LINE__,heap,flags,size)
  148. #define MemReAllocNeverFail(heap,flags,oldblock,size) DbgHeapReAllocNeverFail(__FILE__,__LINE__,heap,flags,oldblock,size)
  149. #define MemAlloc(heap,flags,size) DbgHeapAlloc(__FILE__,__LINE__,heap,flags,size)
  150. #define MemReAlloc(heap,flags,oldblock,size) DbgHeapReAlloc(__FILE__,__LINE__,heap,flags,oldblock,size)
  151. #define MemFree(heap,flags,block) DbgHeapFree(__FILE__,__LINE__,heap,flags,block)
  152. //
  153. // Aides for debugging memory corruption
  154. //
  155. VOID
  156. DbgHeapCheck (
  157. IN PCSTR SourceFile,
  158. IN DWORD Line,
  159. IN HANDLE Heap
  160. );
  161. #define MemHeapCheck(heap) DbgHeapCheck(__FILE__,__LINE__,heap)
  162. VOID
  163. DbgDumpHeapStats (
  164. VOID
  165. );
  166. VOID
  167. DbgDumpHeapLeaks (
  168. VOID
  169. );
  170. SIZE_T
  171. DbgHeapValidatePtr (
  172. IN HANDLE Heap,
  173. IN PCVOID CallerPtr,
  174. IN PCSTR File,
  175. IN DWORD Line
  176. );
  177. #define MemCheckPtr(heap,ptr) (DbgHeapValidatePtr(heap,ptr,__FILE__,__LINE__) != INVALID_PTR)
  178. #else // !DEBUG
  179. //
  180. // Fast allocation routines
  181. //
  182. PVOID
  183. MemFastAlloc (
  184. IN SIZE_T Size
  185. );
  186. PVOID
  187. MemFastReAlloc (
  188. IN PCVOID OldBlock,
  189. IN SIZE_T Size
  190. );
  191. BOOL
  192. MemFastFree (
  193. IN PCVOID Block
  194. );
  195. PVOID
  196. MemFastAllocNeverFail (
  197. IN SIZE_T Size
  198. );
  199. PVOID
  200. MemFastReAllocNeverFail (
  201. IN PVOID OldBlock,
  202. IN SIZE_T Size
  203. );
  204. //
  205. // Fail-proof memory allocators
  206. //
  207. PVOID
  208. MemAllocNeverFail (
  209. IN HANDLE Heap,
  210. IN DWORD Flags,
  211. IN SIZE_T Size
  212. );
  213. PVOID
  214. MemReAllocNeverFail (
  215. IN HANDLE Heap,
  216. IN DWORD Flags,
  217. IN PVOID OldBlock,
  218. IN SIZE_T Size
  219. );
  220. #define MemAlloc(heap,flags,size) HeapAlloc(heap,flags,size)
  221. #define MemReAlloc(heap,flags,oldblock,size) HeapReAlloc(heap,flags,oldblock,size)
  222. #define MemFree(x,y,z) HeapFree(x,y,(PVOID)(z))
  223. //
  224. // Stub macros
  225. //
  226. #define DbgDumpHeapStats()
  227. #define DbgDumpHeapLeaks()
  228. #define MemHeapCheck(heap) (1)
  229. #define MemCheckPtr(heap,ptr) (1)
  230. #endif
  231. PVOID
  232. MemFastAllocAndZero (
  233. IN SIZE_T Size
  234. );
  235. PVOID
  236. MemFastReAllocAndZero (
  237. IN PCVOID Ptr,
  238. IN SIZE_T Size
  239. );
  240. //
  241. // Wrapper macros
  242. //
  243. #define FAST_MALLOC_UNINIT(size) MemFastAlloc (size)
  244. #define FAST_MALLOC_ZEROED(size) MemFastAllocAndZero (size)
  245. #define FAST_MALLOC(size) FAST_MALLOC_UNINIT (size)
  246. #define FAST_REALLOC_UNINIT(ptr,size) MemFastReAlloc (ptr, size)
  247. #define FAST_REALLOC_ZEROED(ptr,size) MemFastReAllocAndZero (ptr, size)
  248. #define FAST_REALLOC(ptr,size) REALLOC_UNINIT (ptr, size)
  249. #define FAST_FREE(ptr) MemFastFree ((PVOID)(ptr))
  250. #define MALLOC_UNINIT(size) MemAlloc (g_hHeap, 0, size)
  251. #define MALLOC_ZEROED(size) MemAlloc (g_hHeap, HEAP_ZERO_MEMORY, size)
  252. #define MALLOC(size) MALLOC_UNINIT (size)
  253. #define REALLOC_UNINIT(ptr,size) MemReAlloc (g_hHeap, 0, ptr, size)
  254. #define REALLOC_ZEROED(ptr,size) MemReAlloc (g_hHeap, HEAP_ZERO_MEMORY, ptr, size)
  255. #define REALLOC(ptr,size) REALLOC_UNINIT (ptr, size)
  256. #define FREE(ptr) MemFree (g_hHeap, 0, (PVOID)(ptr))
  257. #ifdef __cplusplus
  258. }
  259. #endif