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.

422 lines
8.2 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. /*++
  16. Create and destroy routines:
  17. PMHANDLE
  18. PmCreatePoolEx (
  19. IN DWORD BlockSize OPTIONAL
  20. );
  21. PMHANDLE
  22. PmCreateNamedPoolEx (
  23. IN PCSTR Name,
  24. IN DWORD BlockSize OPTIONAL
  25. );
  26. VOID
  27. PmDestroyPool (
  28. IN PMHANDLE Handle
  29. );
  30. Primitive routines:
  31. PVOID
  32. PmGetMemory (
  33. IN PMHANDLE Handle,
  34. IN DWORD Size
  35. );
  36. PVOID
  37. PmGetAlignedMemory (
  38. IN PMHANDLE Handle,
  39. IN DWORD Size
  40. );
  41. VOID
  42. PmReleaseMemory (
  43. IN PMHANDLE Handle,
  44. IN PCVOID Memory
  45. );
  46. Performance and debugging control:
  47. VOID
  48. PmSetMinimumGrowthSize (
  49. IN PMHANDLE Handle,
  50. IN DWORD GrowthSize
  51. );
  52. VOID
  53. PmEmptyPool (
  54. IN PMHANDLE Handle
  55. );
  56. VOID
  57. PmDisableTracking (
  58. IN PMHANDLE Handle
  59. );
  60. VOID
  61. PmDumpStatistics (
  62. VOID
  63. );
  64. Allocation and duplication of data types:
  65. PCTSTR
  66. PmCreateString (
  67. IN PMHANDLE Handle,
  68. IN UINT TcharCount
  69. );
  70. PCTSTR
  71. PmCreateDword (
  72. IN PMHANDLE Handle
  73. );
  74. PBYTE
  75. PmDuplicateMemory (
  76. IN PMHANDLE Handle,
  77. IN PBYTE Data,
  78. IN UINT DataSize
  79. );
  80. PDWORD
  81. PmDuplciateDword (
  82. IN PMHANDLE Handle,
  83. IN DWORD Data
  84. );
  85. PTSTR
  86. PmDuplicateString (
  87. IN PMHANDLE Handle,
  88. IN PCTSTR String
  89. );
  90. PTSTR
  91. PmDuplicateMultiSz (
  92. IN PMHANDLE Handle,
  93. IN PCTSTR MultiSz
  94. );
  95. --*/
  96. //
  97. // Default size of memory pool blocks. This can be changed on a per-pool basis
  98. // by calling PmSetMinimumGrowthSize().
  99. //
  100. #define POOLMEMORYBLOCKSIZE 8192
  101. //
  102. // if DEBUG is defined, poolmem keeps a tally of common statistics on all
  103. // pools. These include number of alloc and free requests, number of
  104. // actual allocations and frees, and various size measures.
  105. //
  106. // PoolMem also checks each PmReleaseMemory() call to ensure that the
  107. // address passed is a valid poolmem address that has not yet been freed.
  108. //
  109. PMHANDLE
  110. RealPmCreatePoolEx (
  111. IN DWORD BlockSize OPTIONAL
  112. );
  113. #define PmCreatePoolEx(b) TRACK_BEGIN(PMHANDLE, PmCreatePoolEx)\
  114. RealPmCreatePoolEx(b)\
  115. TRACK_END()
  116. #define PmCreatePool() PmCreatePoolEx(0)
  117. #ifdef DEBUG
  118. PMHANDLE
  119. RealPmCreateNamedPoolEx (
  120. IN PCSTR Name,
  121. IN DWORD BlockSize OPTIONAL
  122. );
  123. #define PmCreateNamedPoolEx(n,b) TRACK_BEGIN(PMHANDLE, PmCreateNamedPoolEx)\
  124. RealPmCreateNamedPoolEx(n,b)\
  125. TRACK_END()
  126. #define PmCreateNamedPool(n) PmCreateNamedPoolEx(n,0)
  127. #else
  128. #define PmCreateNamedPoolEx(n,b) PmCreatePoolEx(b)
  129. #define PmCreateNamedPool(n) PmCreatePoolEx(0)
  130. #endif
  131. VOID
  132. PmDestroyPool (
  133. IN PMHANDLE Handle
  134. );
  135. //
  136. // Callers should use PmGetMemory or PmGetAlignedMemory. These each decay into
  137. // RealPmGetMemory.
  138. //
  139. PVOID
  140. RealPmGetMemory (
  141. IN PMHANDLE Handle,
  142. IN SIZE_T Size,
  143. IN DWORD AlignSize
  144. );
  145. #define PmGetMemory(h,s) TRACK_BEGIN(PVOID, PmGetMemory)\
  146. RealPmGetMemory((h),(s),0)\
  147. TRACK_END()
  148. #define PmGetAlignedMemory(h,s) TRACK_BEGIN(PVOID, PmGetAlignedMemory)\
  149. RealPmGetMemory((h),(s),sizeof(DWORD))\
  150. TRACK_END()
  151. VOID PmReleaseMemory (IN PMHANDLE Handle, IN PCVOID Memory);
  152. VOID PmSetMinimumGrowthSize(IN PMHANDLE Handle, IN SIZE_T Size);
  153. VOID
  154. PmEmptyPool (
  155. IN PMHANDLE Handle
  156. );
  157. //
  158. // PoolMem created strings are always aligned on DWORD boundaries.
  159. //
  160. #define PmCreateString(h,x) ((LPTSTR) PmGetAlignedMemory((h),(x)*sizeof(TCHAR)))
  161. #define PmCreateDword(h) ((PDWORD) PmGetMemory((h),sizeof(DWORD)))
  162. __inline
  163. PBYTE
  164. PmDuplicateMemory (
  165. IN PMHANDLE Handle,
  166. IN PCBYTE DataToCopy,
  167. IN UINT SizeOfData
  168. )
  169. {
  170. PBYTE Data;
  171. Data = (PBYTE) PmGetAlignedMemory (Handle, SizeOfData);
  172. if (Data) {
  173. CopyMemory (Data, DataToCopy, SizeOfData);
  174. }
  175. return Data;
  176. }
  177. __inline
  178. PDWORD
  179. PmDuplicateDword (
  180. IN PMHANDLE Handle,
  181. IN DWORD ValueToCopy
  182. )
  183. {
  184. PDWORD rWord;
  185. rWord = (PDWORD) PmGetMemory (Handle, sizeof (ValueToCopy));
  186. if (rWord) {
  187. *rWord = ValueToCopy;
  188. }
  189. return rWord;
  190. }
  191. __inline
  192. PSTR
  193. RealPmDuplicateStringA (
  194. IN PMHANDLE Handle,
  195. IN PCSTR StringToCopy
  196. )
  197. {
  198. PSTR rString = RealPmGetMemory (
  199. Handle,
  200. SizeOfStringA (StringToCopy),
  201. sizeof(WCHAR)
  202. );
  203. if (rString) {
  204. StringCopyA (rString, StringToCopy);
  205. }
  206. return rString;
  207. }
  208. #define PmDuplicateStringA(h,s) TRACK_BEGIN(PSTR, PmDuplicateStringA)\
  209. RealPmDuplicateStringA(h,s)\
  210. TRACK_END()
  211. __inline
  212. PWSTR
  213. RealPmDuplicateStringW (
  214. IN PMHANDLE Handle,
  215. IN PCWSTR StringToCopy
  216. )
  217. {
  218. PWSTR rString = RealPmGetMemory (
  219. Handle,
  220. SizeOfStringW (StringToCopy),
  221. sizeof(WCHAR)
  222. );
  223. if (rString) {
  224. StringCopyW (rString, StringToCopy);
  225. }
  226. return rString;
  227. }
  228. #define PmDuplicateStringW(h,s) TRACK_BEGIN(PWSTR, PmDuplicateStringA)\
  229. RealPmDuplicateStringW(h,s)\
  230. TRACK_END()
  231. __inline
  232. PSTR
  233. RealPmDuplicateStringABA (
  234. IN PMHANDLE Handle,
  235. IN PCSTR StringStart,
  236. IN PCSTR End
  237. )
  238. {
  239. PSTR rString;
  240. MYASSERT (StringStart);
  241. MYASSERT (End);
  242. MYASSERT (StringStart <= End);
  243. rString = RealPmGetMemory (
  244. Handle,
  245. // cast is OK, we don't expenct pointers to be far away from each other
  246. (DWORD)((UBINT) End - (UBINT) StringStart) + sizeof (CHAR),
  247. sizeof(WCHAR)
  248. );
  249. if (rString) {
  250. StringCopyABA (rString, StringStart, End);
  251. }
  252. return rString;
  253. }
  254. #define PmDuplicateStringABA(h,s,e) TRACK_BEGIN(PSTR, PmDuplicateStringABA)\
  255. RealPmDuplicateStringABA(h,s,e)\
  256. TRACK_END()
  257. __inline
  258. PWSTR
  259. RealPmDuplicateStringABW (
  260. IN PMHANDLE Handle,
  261. IN PCWSTR StringStart,
  262. IN PCWSTR End
  263. )
  264. {
  265. PWSTR rString;
  266. MYASSERT (StringStart);
  267. MYASSERT (End);
  268. MYASSERT (StringStart <= End);
  269. rString = RealPmGetMemory (
  270. Handle,
  271. (DWORD)((UBINT) End - (UBINT) StringStart) + sizeof (WCHAR),
  272. sizeof(WCHAR)
  273. );
  274. if (rString) {
  275. StringCopyABW (rString,StringStart,End);
  276. }
  277. return rString;
  278. }
  279. #define PmDuplicateStringABW(h,s,e) TRACK_BEGIN(PSTR, PmDuplicateStringABW)\
  280. RealPmDuplicateStringABW(h,s,e)\
  281. TRACK_END()
  282. PSTR
  283. PmDuplicateMultiSzA (
  284. IN PMHANDLE Handle,
  285. IN PCSTR MultiSzToCopy
  286. );
  287. PWSTR
  288. PmDuplicateMultiSzW (
  289. IN PMHANDLE Handle,
  290. IN PCWSTR MultiSzToCopy
  291. );
  292. #ifdef UNICODE
  293. #define PmDuplicateString PmDuplicateStringW
  294. #define PmDuplicateMultiSz PmDuplicateMultiSzW
  295. #else
  296. #define PmDuplicateString PmDuplicateStringA
  297. #define PmDuplicateMultiSz PmDuplicateMultiSzA
  298. #endif
  299. #ifdef DEBUG
  300. VOID
  301. PmDisableTracking (
  302. IN PMHANDLE Handle
  303. );
  304. VOID
  305. PmDumpStatistics (
  306. VOID
  307. );
  308. #else
  309. #define PmDisableTracking(x)
  310. #define PmDumpStatistics()
  311. #endif