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.

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