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.

355 lines
7.5 KiB

  1. /*
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. memory.h
  5. Abstract:
  6. This module contains the memory allocation routines.
  7. Author:
  8. Jameel Hyder (microsoft!jameelh)
  9. Revision History:
  10. 25 Apr 1992 Initial Version
  11. Notes: Tab stop: 4
  12. --*/
  13. #ifndef _AFPMEMORY_
  14. #define _AFPMEMORY_
  15. //
  16. // NOTE: The tag values below are designed to allocate/detect on free memory allocated.
  17. // Note that the callers free the memory simply via AfpFreeMemory and allocate
  18. // via AfpAllocMemory.
  19. //
  20. // via one of the three possible ways:
  21. // a, Non paged memory via ExAllocatePool
  22. // b, Paged memory via ExAllocatePool
  23. // c, Non paged memory via Io Pool
  24. //
  25. #define AFP_TAG *(PULONG)"Afp " // For ExAllocatePoolWithTag()
  26. #define PGD_MEM_TAG 0x11
  27. #define PAGED_MEMORY_TAG (PGD_MEM_TAG << 24)
  28. #define NPG_MEM_TAG 0x22
  29. #define NON_PAGED_MEMORY_TAG (NPG_MEM_TAG << 24)
  30. #define IO_POOL_TAG 0x44
  31. #define IO_POOL_MEMORY_TAG (IO_POOL_TAG << 24)
  32. #define ZEROED_MEM_TAG 0x88
  33. #define ZEROED_MEMORY_TAG (ZEROED_MEM_TAG << 24)
  34. #define MEMORY_TAG_MASK (PAGED_MEMORY_TAG | \
  35. NON_PAGED_MEMORY_TAG | \
  36. IO_POOL_MEMORY_TAG | \
  37. ZEROED_MEMORY_TAG)
  38. extern
  39. NTSTATUS
  40. AfpMemoryInit(
  41. VOID
  42. );
  43. extern
  44. VOID
  45. AfpMemoryDeInit(
  46. VOID
  47. );
  48. #ifdef TRACK_MEMORY_USAGE
  49. #define AfpAllocNonPagedMemory(_S) \
  50. AfpAllocMemory((_S) | NON_PAGED_MEMORY_TAG, FILENUM | __LINE__)
  51. #define AfpAllocZeroedNonPagedMemory(_S) \
  52. AfpAllocMemory((_S) | NON_PAGED_MEMORY_TAG | ZEROED_MEMORY_TAG, FILENUM | __LINE__)
  53. #define AfpAllocPANonPagedMemory(_S) \
  54. AfpAllocPAMemory((_S) | NON_PAGED_MEMORY_TAG, FILENUM | __LINE__)
  55. #define AfpAllocPagedMemory(_S) \
  56. AfpAllocMemory((_S) | PAGED_MEMORY_TAG, FILENUM | __LINE__)
  57. #define AfpAllocZeroedPagedMemory(_S) \
  58. AfpAllocMemory((_S) | PAGED_MEMORY_TAG | ZEROED_MEMORY_TAG, FILENUM | __LINE__)
  59. #define AfpAllocPAPagedMemory(_S) \
  60. AfpAllocPAMemory((_S) | PAGED_MEMORY_TAG, FILENUM | __LINE__)
  61. extern
  62. PBYTE FASTCALL
  63. AfpAllocMemory(
  64. IN LONG Size,
  65. IN DWORD FileLine
  66. );
  67. extern
  68. PBYTE FASTCALL
  69. AfpAllocNonPagedLowPriority(
  70. IN LONG Size,
  71. IN DWORD FileLine
  72. );
  73. extern
  74. PBYTE FASTCALL
  75. AfpAllocPAMemory(
  76. IN LONG Size,
  77. IN DWORD FileLine
  78. );
  79. extern
  80. VOID
  81. AfpTrackMemoryUsage(
  82. IN PVOID pMem,
  83. IN BOOLEAN Alloc,
  84. IN BOOLEAN Paged,
  85. IN DWORD FileLine
  86. );
  87. #else
  88. #define AfpAllocNonPagedMemory(_S) AfpAllocMemory((_S) | NON_PAGED_MEMORY_TAG)
  89. #define AfpAllocZeroedNonPagedMemory(_S) AfpAllocMemory((_S) | NON_PAGED_MEMORY_TAG | ZEROED_MEMORY_TAG)
  90. #define AfpAllocPANonPagedMemory(_S) AfpAllocPAMemory((_S) | NON_PAGED_MEMORY_TAG)
  91. #define AfpAllocPagedMemory(_S) AfpAllocMemory((_S) | PAGED_MEMORY_TAG)
  92. #define AfpAllocZeroedPagedMemory(_S) AfpAllocMemory((_S) | PAGED_MEMORY_TAG | ZEROED_MEMORY_TAG)
  93. #define AfpAllocPAPagedMemory(_S) AfpAllocPAMemory((_S) | PAGED_MEMORY_TAG)
  94. extern
  95. PBYTE FASTCALL
  96. AfpAllocMemory(
  97. IN LONG Size
  98. );
  99. extern
  100. PBYTE FASTCALL
  101. AfpAllocNonPagedLowPriority(
  102. IN LONG Size
  103. );
  104. extern
  105. PBYTE FASTCALL
  106. AfpAllocPAMemory(
  107. IN LONG Size
  108. );
  109. #endif
  110. #define AfpAllocIoMemory(Size) AfpIoAllocBuffer(Size)
  111. extern
  112. VOID FASTCALL
  113. AfpFreeMemory(
  114. IN PVOID pBuffer
  115. );
  116. #define AfpFreePAPagedMemory(_pBuf, _S) AfpFreePAMemory(_pBuf, (_S) | PAGED_MEMORY_TAG)
  117. #define AfpFreePANonPagedMemory(_pBuf, _S) AfpFreePAMemory(_pBuf, (_S) | NON_PAGED_MEMORY_TAG)
  118. extern
  119. VOID FASTCALL
  120. AfpFreePAMemory(
  121. IN PVOID pBuffer,
  122. IN DWORD Size
  123. );
  124. extern
  125. PBYTE FASTCALL
  126. AfpAllocateVirtualMemoryPage(
  127. IN VOID
  128. );
  129. extern
  130. VOID FASTCALL
  131. AfpFreeVirtualMemoryPage(
  132. IN PVOID pBuffer
  133. );
  134. extern
  135. AFPSTATUS FASTCALL
  136. AfpAllocReplyBuf(
  137. IN PSDA pSda
  138. );
  139. extern
  140. PBYTE FASTCALL
  141. AfpAllocStatusBuf(
  142. IN LONG Size
  143. );
  144. extern
  145. PIRP FASTCALL
  146. AfpAllocIrp(
  147. IN CCHAR StackSize
  148. );
  149. extern
  150. VOID FASTCALL
  151. AfpFreeIrp(
  152. IN PIRP pIrp
  153. );
  154. extern
  155. PMDL FASTCALL
  156. AfpAllocMdl(
  157. IN PVOID pBuffer,
  158. IN DWORD Size,
  159. IN PIRP pIrp
  160. );
  161. extern
  162. PMDL
  163. AfpAllocMdlForPagedPool(
  164. IN PVOID pBuffer,
  165. IN DWORD Size,
  166. IN PIRP pIrp
  167. );
  168. extern
  169. VOID FASTCALL
  170. AfpFreeMdl(
  171. IN PMDL pMdl
  172. );
  173. extern
  174. DWORD FASTCALL
  175. AfpMdlChainSize(
  176. IN PMDL pMdl
  177. );
  178. extern
  179. PVOID FASTCALL
  180. AfpIOAllocBuffer(
  181. IN DWORD BufSize
  182. );
  183. extern
  184. VOID FASTCALL
  185. AfpIOFreeBuffer(
  186. IN PVOID pBuffer
  187. );
  188. #define EQUAL_UNICODE_STRING(pUS1, pUS2, fIgnoreCase) \
  189. (((pUS1)->Length == (pUS2)->Length) && \
  190. RtlEqualUnicodeString(pUS1, pUS2, fIgnoreCase))
  191. #define EQUAL_STRING(pS1, pS2, fIgnoreCase) \
  192. (((pS1)->Length == (pS2)->Length) && \
  193. RtlEqualString(pS1, pS2, fIgnoreCase))
  194. // case sensitive unicode string compare
  195. #define EQUAL_UNICODE_STRING_CS(pUS1, pUS2) \
  196. (((pUS1)->Length == (pUS2)->Length) && \
  197. (memcmp((pUS1)->Buffer, (pUS2)->Buffer, (pUS1)->Length) == 0))
  198. //
  199. // AfpSetEmptyUnicodeString and AfpSetEmptyAnsiString are used in
  200. // situations where you have allocated your own pointer for the string
  201. // Buffer, and now you want to initialize all the fields of a counted
  202. // string, making it point to your buffer and setting its length fields
  203. // appropriately for an 'empty' string. Situations like this would
  204. // include data structures where you have allocated a large chunk of
  205. // memory that has included room for any required strings at the end of
  206. // the chunk. For example, the VolDesc structure includes several
  207. // counted strings, and we can just point the string buffers to the
  208. // end of the same chunk of memory that the VolDesc itself occupies.
  209. //
  210. // VOID
  211. // AfpSetEmptyUnicodeString(
  212. // OUT PUNICODE_STRING pstring,
  213. // IN USHORT buflen,
  214. // IN PWSTR pbuf
  215. // );
  216. //
  217. #define AfpSetEmptyUnicodeString(pstring,buflen,pbuf) \
  218. { \
  219. (pstring)->Length = 0; \
  220. (pstring)->MaximumLength = (USHORT)buflen; \
  221. (pstring)->Buffer = (PWSTR)(pbuf); \
  222. }
  223. //
  224. // VOID
  225. // AfpSetEmptyAnsiString(
  226. // OUT PANSI_STRING pstring,
  227. // IN USHORT buflen,
  228. // IN PCHAR pbuf
  229. // );
  230. //
  231. #define AfpSetEmptyAnsiString(pstring,buflen,pbuf) \
  232. { \
  233. (pstring)->Length = 0; \
  234. (pstring)->MaximumLength = (USHORT)buflen; \
  235. (pstring)->Buffer = (PCHAR)(pbuf); \
  236. }
  237. //
  238. // AfpInitUnicodeStringWithNonNullTerm initializes a unicode string with
  239. // a non-null terminated wide char string and its length.
  240. //
  241. // VOID
  242. // AfpInitUnicodeStringWithNonNullTerm(
  243. // OUT PUNICODE_STRING pstring,
  244. // IN USHORT buflen,
  245. // IN PWCHAR pbuf
  246. // );
  247. //
  248. #define AfpInitUnicodeStringWithNonNullTerm(pstring,buflen,pbuf) \
  249. { \
  250. (pstring)->Buffer = pbuf; \
  251. (pstring)->Length = (USHORT)buflen; \
  252. (pstring)->MaximumLength = (USHORT)buflen; \
  253. }
  254. //
  255. // AfpInitAnsiStringWithNonNullTerm initializes an Ansi string with
  256. // a non-null terminated char string and its length.
  257. //
  258. // VOID
  259. // AfpInitAnsiStringWithNonNullTerm(
  260. // OUT PANSI_STRING pstring,
  261. // IN USHORT buflen,
  262. // IN PCHAR pbuf
  263. // );
  264. //
  265. #define AfpInitAnsiStringWithNonNullTerm(pstring,buflen,pbuf) \
  266. { \
  267. (pstring)->Buffer = pbuf; \
  268. (pstring)->Length = (USHORT)buflen; \
  269. (pstring)->MaximumLength = (USHORT)buflen; \
  270. }
  271. #define AfpCopyUnicodeString(pDst, pSrc) \
  272. { \
  273. ASSERT((pDst)->MaximumLength >= (pSrc)->Length); \
  274. RtlCopyMemory((pDst)->Buffer, \
  275. (pSrc)->Buffer, \
  276. (pSrc)->Length); \
  277. (pDst)->Length = (pSrc)->Length; \
  278. }
  279. #define AfpCopyAnsiString(pDst, pSrc) \
  280. { \
  281. ASSERT((pDst)->MaximumLength >= (pSrc)->Length); \
  282. RtlCopyMemory((pDst)->Buffer, \
  283. (pSrc)->Buffer, \
  284. (pSrc)->Length); \
  285. (pDst)->Length = (pSrc)->Length; \
  286. }
  287. #endif // _AFPMEMORY_
  288.