Source code of Windows XP (NT5)
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.

401 lines
8.5 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 DWORD Size
  35. );
  36. PVOID
  37. PoolMemGetAlignedMemory (
  38. IN POOLHANDLE Handle,
  39. IN DWORD 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 DWORD 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(DWORD) /*,*/ ALLOCATION_TRACKING_CALL)\
  137. CLRTRACKCOMMENT
  138. PVOID PoolMemRealGetMemory(IN POOLHANDLE Handle, IN DWORD Size, IN DWORD AlignSize /*,*/ ALLOCATION_TRACKING_DEF);
  139. VOID PoolMemReleaseMemory (IN POOLHANDLE Handle, IN PVOID Memory);
  140. VOID PoolMemSetMinimumGrowthSize(IN POOLHANDLE Handle, IN DWORD 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 UINT SizeOfData
  156. )
  157. {
  158. PBYTE Data;
  159. Data = (PBYTE) PoolMemGetAlignedMemory (Handle, SizeOfData);
  160. if (Data) {
  161. CopyMemory (Data, DataToCopy, SizeOfData);
  162. }
  163. return Data;
  164. }
  165. __inline
  166. PDWORD PoolMemDuplicateDword (
  167. IN POOLHANDLE Handle,
  168. IN DWORD ValueToCopy
  169. )
  170. {
  171. PDWORD rWord;
  172. rWord = (PDWORD) PoolMemGetMemory (Handle, sizeof (ValueToCopy));
  173. if (rWord) {
  174. *rWord = ValueToCopy;
  175. }
  176. return rWord;
  177. }
  178. __inline
  179. PSTR
  180. RealPoolMemDuplicateStringA (
  181. IN POOLHANDLE Handle,
  182. IN PCSTR StringToCopy /*,*/
  183. ALLOCATION_TRACKING_DEF
  184. )
  185. {
  186. PSTR rString = (PSTR) PoolMemRealGetMemory(Handle,SizeOfStringA(StringToCopy),sizeof(WCHAR) /*,*/ ALLOCATION_INLINE_CALL);
  187. if (rString) {
  188. StringCopyA((unsigned char *) rString, (const unsigned char *) StringToCopy);
  189. }
  190. return rString;
  191. }
  192. __inline
  193. PWSTR
  194. RealPoolMemDuplicateStringW (
  195. IN POOLHANDLE Handle,
  196. IN PCWSTR StringToCopy /*,*/
  197. ALLOCATION_TRACKING_DEF
  198. )
  199. {
  200. PWSTR rString = (PWSTR) PoolMemRealGetMemory(Handle,SizeOfStringW(StringToCopy),sizeof(WCHAR) /*,*/ ALLOCATION_INLINE_CALL);
  201. if (rString) {
  202. StringCopyW(rString,StringToCopy);
  203. }
  204. return rString;
  205. }
  206. #define PoolMemDuplicateStringA(h,s) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringA",__FILE__,__LINE__)\
  207. RealPoolMemDuplicateStringA((h),(s) /*,*/ ALLOCATION_TRACKING_CALL)\
  208. CLRTRACKCOMMENT
  209. #define PoolMemDuplicateStringW(h,s) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringW",__FILE__,__LINE__)\
  210. RealPoolMemDuplicateStringW((h),(s) /*,*/ ALLOCATION_TRACKING_CALL)\
  211. CLRTRACKCOMMENT
  212. __inline
  213. PSTR
  214. RealPoolMemDuplicateStringABA (
  215. IN POOLHANDLE Handle,
  216. IN PCSTR StringStart,
  217. IN PCSTR End /*,*/
  218. ALLOCATION_TRACKING_DEF
  219. )
  220. {
  221. PSTR rString;
  222. MYASSERT (StringStart);
  223. MYASSERT (End);
  224. MYASSERT (StringStart <= End);
  225. rString = (PSTR) PoolMemRealGetMemory (
  226. Handle,
  227. (PBYTE) End - (PBYTE) StringStart + sizeof (CHAR),
  228. sizeof(WCHAR) /*,*/
  229. ALLOCATION_INLINE_CALL
  230. );
  231. if (rString) {
  232. StringCopyABA(rString,StringStart,End);
  233. }
  234. return rString;
  235. }
  236. __inline
  237. PWSTR
  238. RealPoolMemDuplicateStringABW (
  239. IN POOLHANDLE Handle,
  240. IN PCWSTR StringStart,
  241. IN PCWSTR End /*,*/
  242. ALLOCATION_TRACKING_DEF
  243. )
  244. {
  245. PWSTR rString;
  246. MYASSERT (StringStart);
  247. MYASSERT (End);
  248. MYASSERT (StringStart <= End);
  249. rString = (PWSTR) PoolMemRealGetMemory (
  250. Handle,
  251. (PBYTE) End - (PBYTE) StringStart + sizeof (WCHAR),
  252. sizeof(WCHAR) /*,*/
  253. ALLOCATION_INLINE_CALL
  254. );
  255. if (rString) {
  256. StringCopyABW(rString,StringStart,End);
  257. }
  258. return rString;
  259. }
  260. #define PoolMemDuplicateStringABA(h,s,e) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringABA",__FILE__,__LINE__)\
  261. RealPoolMemDuplicateStringABA((h),(s),(e) /*,*/ ALLOCATION_TRACKING_CALL)\
  262. CLRTRACKCOMMENT
  263. #define PoolMemDuplicateStringABW(h,s,e) SETTRACKCOMMENT(PVOID,"PoolMemDuplicateStringABW",__FILE__,__LINE__)\
  264. RealPoolMemDuplicateStringABW((h),(s),(e) /*,*/ ALLOCATION_TRACKING_CALL)\
  265. CLRTRACKCOMMENT
  266. PSTR
  267. PoolMemDuplicateMultiSzA (
  268. IN POOLHANDLE Handle,
  269. IN PCSTR MultiSzToCopy
  270. );
  271. PWSTR
  272. PoolMemDuplicateMultiSzW (
  273. IN POOLHANDLE Handle,
  274. IN PCWSTR MultiSzToCopy
  275. );
  276. #ifdef UNICODE
  277. #define PoolMemDuplicateString PoolMemDuplicateStringW
  278. #define PoolMemDuplicateMultiSz PoolMemDuplicateMultiSzW
  279. #else
  280. #define PoolMemDuplicateString PoolMemDuplicateStringA
  281. #define PoolMemDuplicateMultiSz PoolMemDuplicateMultiSzA
  282. #endif
  283. #ifdef DEBUG
  284. VOID
  285. PoolMemDisableTracking (
  286. IN POOLHANDLE Handle
  287. );
  288. #else
  289. #define PoolMemDisableTracking(x)
  290. #endif