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.

404 lines
8.9 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. poolmem.h
  5. Abstract:
  6. Declares the pool memory interface. A pool of memory is a set of
  7. blocks (typically 8K each) that are used for several allocations,
  8. and then freed at the end of processing. See below for routines.
  9. Author:
  10. Marc R. Whitten (marcw) 02-Feb-1997
  11. Revision History:
  12. jimschm 04-Feb-1998 Named pools for tracking
  13. --*/
  14. #pragma once
  15. typedef PVOID POOLHANDLE;
  16. /*++
  17. Create and destroy routines:
  18. POOLHANDLE
  19. PoolMemInitPool (
  20. VOID
  21. );
  22. POOLHANDLE
  23. PoolMemInitNamedPool (
  24. IN PCSTR Name
  25. );
  26. VOID
  27. PoolMemDestroyPool (
  28. IN POOLHANDLE Handle
  29. );
  30. Primitive routines:
  31. PVOID
  32. PoolMemGetMemory (
  33. IN POOLHANDLE Handle,
  34. IN SIZE_T Size
  35. );
  36. PVOID
  37. PoolMemGetAlignedMemory (
  38. IN POOLHANDLE Handle,
  39. IN SIZE_T Size
  40. );
  41. VOID
  42. PoolMemReleaseMemory (
  43. IN POOLHANDLE Handle,
  44. IN PVOID Memory
  45. );
  46. Performance and debugging control:
  47. VOID
  48. PoolMemSetMinimumGrowthSize (
  49. IN POOLHANDLE Handle,
  50. IN SIZE_T GrowthSize
  51. );
  52. VOID
  53. PoolMemEmptyPool (
  54. IN POOLHANDLE Handle
  55. );
  56. VOID
  57. PoolMemDisableTracking (
  58. IN POOLHANDLE Handle
  59. );
  60. Allocation and duplication of data types:
  61. PCTSTR
  62. PoolMemCreateString (
  63. IN POOLHANDLE Handle,
  64. IN UINT TcharCount
  65. );
  66. PCTSTR
  67. PoolMemCreateDword (
  68. IN POOLHANDLE Handle
  69. );
  70. PBYTE
  71. PoolMemDuplicateMemory (
  72. IN POOLHANDLE Handle,
  73. IN PBYTE Data,
  74. IN UINT DataSize
  75. );
  76. PDWORD
  77. PoolMemDuplciateDword (
  78. IN POOLHANDLE Handle,
  79. IN DWORD Data
  80. );
  81. PTSTR
  82. PoolMemDuplicateString (
  83. IN POOLHANDLE Handle,
  84. IN PCTSTR String
  85. );
  86. PTSTR
  87. PoolMemDuplicateMultiSz (
  88. IN POOLHANDLE Handle,
  89. IN PCTSTR MultiSz
  90. );
  91. --*/
  92. //
  93. // Default size of memory pool blocks. This can be changed on a per-pool basis
  94. // by calling PoolMemSetMinimumGrowthSize().
  95. //
  96. #define POOLMEMORYBLOCKSIZE 8192
  97. //
  98. // if DEBUG is defined, poolmem keeps a tally of common statistics on all
  99. // pools. These include number of alloc and free requests, number of
  100. // actual allocations and frees, and various size measures.
  101. //
  102. // PoolMem also checks each PoolMemReleaseMemory() call to ensure that the
  103. // address passed is a valid poolmem address that has not yet been freed.
  104. //
  105. #ifdef DEBUG
  106. #define POOLMEMTRACKDEF LPCSTR File, DWORD Line,
  107. #define POOLMEMTRACKCALL g_TrackFile,g_TrackLine,
  108. #else
  109. #define POOLMEMTRACKDEF
  110. #define POOLMEMTRACKCALL
  111. #endif
  112. POOLHANDLE
  113. PoolMemInitPool (
  114. VOID
  115. );
  116. #ifdef DEBUG
  117. POOLHANDLE
  118. PoolMemInitNamedPool (
  119. IN PCSTR Name
  120. );
  121. #else
  122. #define PoolMemInitNamedPool(x) PoolMemInitPool()
  123. #endif
  124. VOID
  125. PoolMemDestroyPool (
  126. IN POOLHANDLE Handle
  127. );
  128. //
  129. // Callers should use PoolMemGetMemory or PoolMemGetAlignedMemory. These each decay into
  130. // PoolMemRealGetMemory.
  131. //
  132. #define PoolMemGetMemory(h,s) SETTRACKCOMMENT(PVOID,"PoolMemGetMemory",__FILE__,__LINE__)\
  133. PoolMemRealGetMemory((h),(s),0 /*,*/ ALLOCATION_TRACKING_CALL)\
  134. CLRTRACKCOMMENT
  135. #define PoolMemGetAlignedMemory(h,s) SETTRACKCOMMENT(PVOID,"PoolMemGetAlignedMemory",__FILE__,__LINE__)\
  136. PoolMemRealGetMemory((h),(s),sizeof(UINT_PTR) /*,*/ ALLOCATION_TRACKING_CALL)\
  137. CLRTRACKCOMMENT
  138. PVOID PoolMemRealGetMemory(IN POOLHANDLE Handle, IN SIZE_T Size, IN SIZE_T AlignSize /*,*/ ALLOCATION_TRACKING_DEF);
  139. VOID PoolMemReleaseMemory (IN POOLHANDLE Handle, IN PVOID Memory);
  140. VOID PoolMemSetMinimumGrowthSize(IN POOLHANDLE Handle, IN SIZE_T Size);
  141. VOID
  142. PoolMemEmptyPool (
  143. IN POOLHANDLE Handle
  144. );
  145. //
  146. // PoolMem created strings are always aligned on DWORD boundaries.
  147. //
  148. #define PoolMemCreateString(h,x) ((LPTSTR) PoolMemGetAlignedMemory((h),(x)*sizeof(TCHAR)))
  149. #define PoolMemCreateDword(h) ((PDWORD) PoolMemGetMemory((h),sizeof(DWORD)))
  150. __inline
  151. PBYTE
  152. PoolMemDuplicateMemory (
  153. IN POOLHANDLE Handle,
  154. IN PBYTE DataToCopy,
  155. IN SIZE_T SizeOfData
  156. )
  157. {
  158. PBYTE Data;
  159. PVOID p;
  160. p = PoolMemGetAlignedMemory ((PVOID) Handle, SizeOfData);
  161. Data = (PBYTE) p;
  162. if (Data) {
  163. CopyMemory (Data, DataToCopy, SizeOfData);
  164. }
  165. return Data;
  166. }
  167. __inline
  168. PDWORD PoolMemDuplicateDword (
  169. IN POOLHANDLE Handle,
  170. IN DWORD ValueToCopy
  171. )
  172. {
  173. PDWORD rWord;
  174. rWord = (PDWORD) PoolMemGetMemory (Handle, sizeof (ValueToCopy));
  175. if (rWord) {
  176. *rWord = ValueToCopy;
  177. }
  178. return rWord;
  179. }
  180. __inline
  181. PSTR
  182. RealPoolMemDuplicateStringA (
  183. IN POOLHANDLE Handle,
  184. IN PCSTR StringToCopy /*,*/
  185. ALLOCATION_TRACKING_DEF
  186. )
  187. {
  188. PSTR rString = (PSTR) PoolMemRealGetMemory(Handle,SizeOfStringA(StringToCopy),sizeof(WCHAR) /*,*/ ALLOCATION_INLINE_CALL);
  189. if (rString) {
  190. StringCopyA(rString, StringToCopy);
  191. }
  192. return rString;
  193. }
  194. __inline
  195. PWSTR
  196. RealPoolMemDuplicateStringW (
  197. IN POOLHANDLE Handle,
  198. IN PCWSTR StringToCopy /*,*/
  199. ALLOCATION_TRACKING_DEF
  200. )
  201. {
  202. PWSTR rString = (PWSTR) PoolMemRealGetMemory(Handle,SizeOfStringW(StringToCopy),sizeof(WCHAR) /*,*/ ALLOCATION_INLINE_CALL);
  203. if (rString) {
  204. StringCopyW(rString,StringToCopy);
  205. }
  206. return rString;
  207. }
  208. #define PoolMemDuplicateStringA(h,s) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringA",__FILE__,__LINE__)\
  209. RealPoolMemDuplicateStringA((h),(s) /*,*/ ALLOCATION_TRACKING_CALL)\
  210. CLRTRACKCOMMENT
  211. #define PoolMemDuplicateStringW(h,s) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringW",__FILE__,__LINE__)\
  212. RealPoolMemDuplicateStringW((h),(s) /*,*/ ALLOCATION_TRACKING_CALL)\
  213. CLRTRACKCOMMENT
  214. __inline
  215. PSTR
  216. RealPoolMemDuplicateStringABA (
  217. IN POOLHANDLE Handle,
  218. IN PCSTR StringStart,
  219. IN PCSTR End /*,*/
  220. ALLOCATION_TRACKING_DEF
  221. )
  222. {
  223. PSTR rString;
  224. MYASSERT (StringStart);
  225. MYASSERT (End);
  226. MYASSERT (StringStart <= End);
  227. rString = (PSTR) PoolMemRealGetMemory (
  228. Handle,
  229. (PBYTE) End - (PBYTE) StringStart + sizeof (CHAR),
  230. sizeof(WCHAR) /*,*/
  231. ALLOCATION_INLINE_CALL
  232. );
  233. if (rString) {
  234. StringCopyABA(rString,StringStart,End);
  235. }
  236. return rString;
  237. }
  238. __inline
  239. PWSTR
  240. RealPoolMemDuplicateStringABW (
  241. IN POOLHANDLE Handle,
  242. IN PCWSTR StringStart,
  243. IN PCWSTR End /*,*/
  244. ALLOCATION_TRACKING_DEF
  245. )
  246. {
  247. PWSTR rString;
  248. MYASSERT (StringStart);
  249. MYASSERT (End);
  250. MYASSERT (StringStart <= End);
  251. rString = (PWSTR) PoolMemRealGetMemory (
  252. Handle,
  253. (PBYTE) End - (PBYTE) StringStart + sizeof (WCHAR),
  254. sizeof(WCHAR) /*,*/
  255. ALLOCATION_INLINE_CALL
  256. );
  257. if (rString) {
  258. StringCopyABW(rString,StringStart,End);
  259. }
  260. return rString;
  261. }
  262. #define PoolMemDuplicateStringABA(h,s,e) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringABA",__FILE__,__LINE__)\
  263. RealPoolMemDuplicateStringABA((h),(s),(e) /*,*/ ALLOCATION_TRACKING_CALL)\
  264. CLRTRACKCOMMENT
  265. #define PoolMemDuplicateStringABW(h,s,e) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringABW",__FILE__,__LINE__)\
  266. RealPoolMemDuplicateStringABW((h),(s),(e) /*,*/ ALLOCATION_TRACKING_CALL)\
  267. CLRTRACKCOMMENT
  268. PSTR
  269. PoolMemDuplicateMultiSzA (
  270. IN POOLHANDLE Handle,
  271. IN PCSTR MultiSzToCopy
  272. );
  273. PWSTR
  274. PoolMemDuplicateMultiSzW (
  275. IN POOLHANDLE Handle,
  276. IN PCWSTR MultiSzToCopy
  277. );
  278. #ifdef UNICODE
  279. #define PoolMemDuplicateString PoolMemDuplicateStringW
  280. #define PoolMemDuplicateMultiSz PoolMemDuplicateMultiSzW
  281. #else
  282. #define PoolMemDuplicateString PoolMemDuplicateStringA
  283. #define PoolMemDuplicateMultiSz PoolMemDuplicateMultiSzA
  284. #endif
  285. #ifdef DEBUG
  286. VOID
  287. PoolMemDisableTracking (
  288. IN POOLHANDLE Handle
  289. );
  290. #else
  291. #define PoolMemDisableTracking(x)
  292. #endif