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.

372 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. atkmem.h
  5. Abstract:
  6. This module contains memory allocator routines for the stack
  7. Author:
  8. Jameel Hyder (jameelh@microsoft.com)
  9. Nikhil Kamkolkar (nikhilk@microsoft.com)
  10. Revision History:
  11. 23 Feb 1993 Initial Version
  12. Notes: Tab stop: 4
  13. --*/
  14. #ifndef _ATKMEM_
  15. #define _ATKMEM_
  16. #define DWORDSIZEBLOCK(Size) (((Size)+sizeof(ULONG)-1) & ~(sizeof(ULONG)-1))
  17. #define ATALK_MEMORY_SIGNATURE *(PULONG)"ATKM"
  18. #define ZEROED_MEMORY_TAG 0xF0000000
  19. #define ATALK_TAG *(PULONG)"Atk "
  20. //
  21. // Definitions for the block management package
  22. //
  23. typedef UCHAR BLKID;
  24. // Add a BLKID_xxx and an entry to atalkBlkSize for every block client
  25. #define BLKID_BUFFDESC (BLKID)0
  26. #define BLKID_AMT (BLKID)1
  27. #define BLKID_AMT_ROUTE (BLKID)2
  28. #define BLKID_BRE (BLKID)3
  29. #define BLKID_BRE_ROUTE (BLKID)4
  30. #define BLKID_ATPREQ (BLKID)5
  31. #define BLKID_ATPRESP (BLKID)6
  32. #define BLKID_ASPREQ (BLKID)7
  33. #define BLKID_ARAP_SMPKT (BLKID)8
  34. #define BLKID_ARAP_MDPKT (BLKID)9
  35. #define BLKID_ARAP_LGPKT (BLKID)10
  36. #define BLKID_ARAP_SNDPKT (BLKID)11
  37. #define BLKID_ARAP_LGBUF (BLKID)12
  38. #define BLKID_NEED_NDIS_INT BLKID_AARP // All ids above this needs Ndis Initialization
  39. // See AtalkBPAllocBlock
  40. #define BLKID_AARP (BLKID)13
  41. #define BLKID_DDPSM (BLKID)14
  42. #define BLKID_DDPLG (BLKID)15
  43. #define BLKID_SENDBUF (BLKID)16
  44. #define BLKID_MNP_SMSENDBUF (BLKID)17
  45. #define BLKID_MNP_LGSENDBUF (BLKID)18
  46. #define NUM_BLKIDS (BLKID)19
  47. //
  48. // if we need huge buffers, we just do an alloc ourselves (rather than using the
  49. // above BLKID mechanism. So that we know it is something we allocated, we use
  50. // this as the "block id". Now, make sure NUM_BLKIDS never exceeds 250!
  51. //
  52. #define ARAP_UNLMTD_BUFF_ID (NUM_BLKIDS+5)
  53. // BUFFER DESCRIPTORS
  54. // These will be used by callers into the DDP layer. They can be
  55. // chained together. They contain either an opaque (MDL on NT) or
  56. // a PBYTE buffer. All outside callers *must* pass in an MDL. Only
  57. // DDP/AARP will have the right to create a buffer descriptor which
  58. // will hold a PBYTE buffer.
  59. //
  60. // MODEL OF OPERATION FOR DDP:
  61. // DDP/AARP will call the link AllocBuildLinkHeader routine. This will
  62. // allocate the space that DDP/AARP says it needs. The link header will
  63. // then be built from the start of the buffer. A pointer to the beginning
  64. // and to the place where the caller can fill in their headers is returned.
  65. // DDP/AARP will then fill in its header, make a buffer descriptor for
  66. // this buffer, prepend to the buffer descriptor its received from its
  67. // client, and then call the packet out routines.
  68. #define BD_CHAR_BUFFER (USHORT)0x0001
  69. #define BD_FREE_BUFFER (USHORT)0x0002
  70. #define BD_SIGNATURE *((PULONG)"BDES")
  71. #if DBG
  72. #define VALID_BUFFDESC(pBuffDesc) \
  73. (((pBuffDesc) != NULL) && ((pBuffDesc)->bd_Signature == BD_SIGNATURE))
  74. #else
  75. #define VALID_BUFFDESC(pBuffDesc) ((pBuffDesc) != NULL)
  76. #endif
  77. typedef struct _BUFFER_DESC
  78. {
  79. #if DBG
  80. ULONG bd_Signature;
  81. #endif
  82. struct _BUFFER_DESC * bd_Next;
  83. USHORT bd_Flags;
  84. SHORT bd_Length;
  85. union
  86. {
  87. PAMDL bd_OpaqueBuffer;
  88. struct
  89. {
  90. // bd_FreeBuffer is the beginning of the allocated buffer.
  91. // bd_CharBuffer is from some offset (0 or >) within it
  92. // from where the data starts.
  93. PBYTE bd_CharBuffer;
  94. PBYTE bd_FreeBuffer;
  95. };
  96. };
  97. } BUFFER_DESC, *PBUFFER_DESC;
  98. #ifdef TRACK_MEMORY_USAGE
  99. #define AtalkAllocMemory(Size) AtalkAllocMem(Size, FILENUM | __LINE__)
  100. extern
  101. PVOID FASTCALL
  102. AtalkAllocMem(
  103. IN ULONG Size,
  104. IN ULONG FileLine
  105. );
  106. extern
  107. VOID
  108. AtalkTrackMemoryUsage(
  109. IN PVOID pMem,
  110. IN ULONG Size,
  111. IN BOOLEAN Alloc,
  112. IN ULONG FileLine
  113. );
  114. #else
  115. #define AtalkAllocMemory(Size) AtalkAllocMem(Size)
  116. #define AtalkTrackMemoryUsage(pMem, Size, Alloc, FileLine)
  117. extern
  118. PVOID FASTCALL
  119. AtalkAllocMem(
  120. IN ULONG Size
  121. );
  122. #endif // TRACK_MEMORY_USAGE
  123. #ifdef TRACK_BUFFDESC_USAGE
  124. #define AtalkAllocBuffDesc(Ptr, Length, Flags) \
  125. AtalkAllocBufferDesc(Ptr, Length, Flags, FILENUM | __LINE__)
  126. #define AtalkDescribeBuffDesc(DataPtr, FreePtr, Length, Flags) \
  127. AtalkDescribeBufferDesc(DataPtr, FreePtr, Length, Flags, FILENUM | __LINE__)
  128. extern
  129. VOID
  130. AtalkTrackBuffDescUsage(
  131. IN PVOID pBuffDesc,
  132. IN BOOLEAN Alloc,
  133. IN ULONG FileLine
  134. );
  135. extern
  136. PBUFFER_DESC
  137. AtalkAllocBufferDesc(
  138. IN PVOID Ptr, // Either a PAMDL or a PBYTE
  139. IN USHORT Length,
  140. IN USHORT Flags,
  141. IN ULONG FileLine
  142. );
  143. extern
  144. PBUFFER_DESC
  145. AtalkDescribeBufferDesc(
  146. IN PVOID DataPtr,
  147. IN PVOID FreePtr,
  148. IN USHORT Length,
  149. IN USHORT Flags,
  150. IN ULONG FileLine
  151. );
  152. #else
  153. #define AtalkAllocBuffDesc(Ptr, Length, Flags) \
  154. AtalkAllocBufferDesc(Ptr, Length, Flags)
  155. #define AtalkDescribeBuffDesc(DataPtr, FreePtr, Length, Flags) \
  156. AtalkDescribeBufferDesc(DataPtr, FreePtr, Length, Flags)
  157. #define AtalkTrackBuffDescUsage(pBuffDesc, Alloc, FileLine)
  158. extern
  159. PBUFFER_DESC
  160. AtalkAllocBufferDesc(
  161. IN PVOID Ptr, // Either a PAMDL or a PBYTE
  162. IN USHORT Length,
  163. IN USHORT Flags
  164. );
  165. extern
  166. PBUFFER_DESC
  167. AtalkDescribeBufferDesc(
  168. IN PVOID DataPtr,
  169. IN PVOID FreePtr,
  170. IN USHORT Length,
  171. IN USHORT Flags
  172. );
  173. #endif // TRACK_BUFFDESC_USAGE
  174. #define AtalkAllocZeroedMemory(Size) AtalkAllocMemory((Size) | ZEROED_MEMORY_TAG)
  175. extern
  176. VOID FASTCALL
  177. AtalkFreeMemory(
  178. IN PVOID pBuffer
  179. );
  180. extern
  181. VOID FASTCALL
  182. AtalkFreeBuffDesc(
  183. IN PBUFFER_DESC pBuffDesc
  184. );
  185. extern
  186. VOID
  187. AtalkCopyBuffDescToBuffer(
  188. IN PBUFFER_DESC pBuffDesc,
  189. IN LONG SrcOff,
  190. IN LONG BytesToCopy,
  191. IN PBYTE DstBuf
  192. );
  193. extern
  194. VOID
  195. AtalkCopyBufferToBuffDesc(
  196. IN PBYTE SrcBuf,
  197. IN LONG BytesToCopy,
  198. IN PBUFFER_DESC pBuffDesc,
  199. IN LONG DstOff
  200. );
  201. extern
  202. LONG FASTCALL
  203. AtalkSizeBuffDesc(
  204. IN PBUFFER_DESC pBuffDesc
  205. );
  206. extern
  207. VOID
  208. AtalkInitMemorySystem(
  209. VOID
  210. );
  211. extern
  212. VOID
  213. AtalkDeInitMemorySystem(
  214. VOID
  215. );
  216. // Macros
  217. #define GET_MDL_FROM_OPAQUE(x) ((PMDL)x)
  218. #define AtalkPrependBuffDesc(pNode, pList) \
  219. pNode->bd_Next = pList;
  220. #define AtalkAppendBuffDesc(pNode, pList) \
  221. { \
  222. PBUFFER_DESC _L = pList; \
  223. \
  224. if (_L == NULL) \
  225. { \
  226. pNode->bd_Next = NULL; \
  227. } \
  228. else \
  229. { \
  230. while (_L->bd_Next != NULL) \
  231. _L = _L->bd_Next; \
  232. \
  233. _L->bd_Next = pNode; \
  234. pNode->bd_Next = NULL; \
  235. } \
  236. }
  237. #define AtalkSizeOfBuffDescData(pBuf, pLen) \
  238. { \
  239. PBUFFER_DESC _B = (pBuf); \
  240. USHORT _L = 0; \
  241. \
  242. while (_B) \
  243. { \
  244. _L += _B->bd_Length; \
  245. _B = _B->bd_Next; \
  246. } \
  247. *(pLen) = _L; \
  248. }
  249. #define AtalkSetSizeOfBuffDescData(pBuf, Len) ((pBuf)->bd_Length = (Len))
  250. extern
  251. PAMDL
  252. AtalkAllocAMdl(
  253. IN PBYTE pBuffer OPTIONAL,
  254. IN LONG Size
  255. );
  256. extern
  257. PAMDL
  258. AtalkSubsetAmdl(
  259. IN PAMDL pStartingMdl,
  260. IN ULONG TotalOffset,
  261. IN ULONG DesiredLength);
  262. #define AtalkGetAddressFromMdl(pAMdl) MmGetSystemAddressForMdl(pAMdl)
  263. #define AtalkGetAddressFromMdlSafe(pAMdl, PagePriority) MmGetSystemAddressForMdlSafe(pAMdl, PagePriority)
  264. #ifdef PROFILING
  265. #define AtalkFreeAMdl(pAmdl) \
  266. { \
  267. PAMDL _N; \
  268. PAMDL _L = pAmdl; \
  269. while (_L != NULL) \
  270. { \
  271. _N = _L->Next; \
  272. ExInterlockedDecrementLong( \
  273. &AtalkStatistics.stat_CurMdlCount,\
  274. &AtalkKeStatsLock); \
  275. IoFreeMdl(_L); \
  276. ATALK_DBG_DEC_COUNT(AtalkDbgMdlsAlloced); \
  277. _L = _N; \
  278. } \
  279. }
  280. #else
  281. #define AtalkFreeAMdl(pAmdl) \
  282. { \
  283. PAMDL _N; \
  284. PAMDL _L = pAmdl; \
  285. while (_L != NULL) \
  286. { \
  287. _N = _L->Next; \
  288. IoFreeMdl(_L); \
  289. ATALK_DBG_DEC_COUNT(AtalkDbgMdlsAlloced); \
  290. _L = _N; \
  291. } \
  292. }
  293. #endif
  294. #define AtalkIsMdlFragmented(pMdl) ((pMdl)->Next != NULL)
  295. extern
  296. LONG FASTCALL
  297. AtalkSizeMdlChain(
  298. IN PAMDL pAMdlChain
  299. );
  300. PVOID FASTCALL
  301. AtalkBPAllocBlock(
  302. IN BLKID BlockId
  303. );
  304. VOID FASTCALL
  305. AtalkBPFreeBlock(
  306. IN PVOID pBlock
  307. );
  308. #endif // _ATKMEM_