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.

487 lines
16 KiB

  1. /*
  2. (C) Copyright 1998
  3. All rights reserved.
  4. Portions of this software are:
  5. (C) Copyright 1995 TriplePoint, Inc. -- http://www.TriplePoint.com
  6. License to use this software is granted under the same terms
  7. outlined in the Microsoft Windows Device Driver Development Kit.
  8. (C) Copyright 1992 Microsoft Corp. -- http://www.Microsoft.com
  9. License to use this software is granted under the terms outlined in
  10. the Microsoft Windows Device Driver Development Kit.
  11. @doc INTERNAL TpiMem TpiMem_c
  12. @module TpiMem.c |
  13. This module implements the interface to the memory allocation wrappers.
  14. @head3 Contents |
  15. @index class,mfunc,func,msg,mdata,struct,enum | TpiMem_c
  16. @end
  17. */
  18. #define __FILEID__ TPIMEMORY_OBJECT_TYPE
  19. // Unique file ID for error logging
  20. #include "TpiMem.h"
  21. #include "TpiDebug.h"
  22. DBG_STATIC ULONG g_MemoryAllocated = 0;
  23. DBG_STATIC ULONG g_MemoryFreed = 0;
  24. DBG_STATIC ULONG g_SharedMemoryAllocated = 0;
  25. DBG_STATIC ULONG g_SharedMemoryFreed = 0;
  26. /* @doc INTERNAL TpiMem TpiMem_c TpiAllocateMemory
  27. @func
  28. <f TpiAllocateMemory> provides a wrapper interface for standard memory
  29. allocation via <f NdisAllocateMemory>. This interface is used to help
  30. debug memory allocation problems. It can be used to keep track of how
  31. much memory has been allocated and freed by the Miniport, and can report
  32. the usage counters via the debugger.
  33. @comm
  34. This routine uses zero for the <p MemoryFlags> parameter when calling
  35. <f NdisAllocateMemory> (i.e. non-paged system memory). Do not use this
  36. routine to allocate continuous or non-cached memory.
  37. @rdesc
  38. <f TpiAllocateMemory> returns zero if it is successful.<nl>
  39. Otherwise, a non-zero return value indicates an error condition.
  40. */
  41. NDIS_STATUS TpiAllocateMemory(
  42. OUT PVOID * ppObject, // @parm
  43. // Points to a caller-defined memory location to which this function
  44. // writes the virtual address of the allocated memory. If memory of
  45. // the specified type is not available, the pointer value is NULL.
  46. IN ULONG dwSize, // @parm
  47. // Specifies the size, in bytes, of the requested memory.
  48. IN ULONG dwFileID, // @parm
  49. // __FILEID__ of the caller.
  50. IN LPSTR szFileName, // @parm
  51. // File name of the caller.
  52. IN ULONG dwLineNumber, // @parm
  53. // Line number of the file where called from.
  54. IN NDIS_HANDLE MiniportAdapterHandle // @parm
  55. // Specifies a handle identifying the miniport's NIC, which is assigned
  56. // by the NDIS library.
  57. )
  58. {
  59. DBG_FUNC("TpiAllocateMemory")
  60. NDIS_STATUS Status;
  61. // Holds the status result returned from an NDIS function call.
  62. ASSERT(ppObject);
  63. ASSERT(dwSize);
  64. ASSERT(szFileName);
  65. DBG_ENTER(DbgInfo);
  66. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  67. ("\n"
  68. "\t|ppObject=0x%X\n"
  69. "\t|dwSize=%d\n"
  70. "\t|dwFileID=0x%X\n"
  71. "\t|szFileName=%s\n"
  72. "\t|dwLineNumber=%d\n",
  73. ppObject,
  74. dwSize,
  75. dwFileID,
  76. szFileName,
  77. dwLineNumber
  78. ));
  79. /*
  80. // Allocate memory from NDIS.
  81. */
  82. #if !defined(NDIS50_MINIPORT)
  83. Status = NdisAllocateMemory(ppObject, dwSize, 0, g_HighestAcceptableAddress);
  84. #else // NDIS50_MINIPORT
  85. Status = NdisAllocateMemoryWithTag(ppObject, dwSize, dwFileID);
  86. #endif // NDIS50_MINIPORT
  87. if (Status == NDIS_STATUS_SUCCESS)
  88. {
  89. ASSERT(*ppObject);
  90. NdisZeroMemory(*ppObject, dwSize);
  91. g_MemoryAllocated += dwSize;
  92. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  93. ("Memory Allocated=%d Freed=%d -- Ptr=0x%X\n",
  94. g_MemoryAllocated, g_MemoryFreed, *ppObject));
  95. }
  96. else
  97. {
  98. DBG_ERROR(DbgInfo,("NdisAllocateMemory(Size=%d, File=%s, Line=%d) failed (Status=%X)\n",
  99. dwSize, szFileName, dwLineNumber, Status));
  100. /*
  101. // Log error message and return.
  102. */
  103. NdisWriteErrorLogEntry(
  104. MiniportAdapterHandle,
  105. NDIS_ERROR_CODE_OUT_OF_RESOURCES,
  106. 3,
  107. Status,
  108. dwFileID,
  109. dwLineNumber
  110. );
  111. *ppObject = NULL;
  112. }
  113. DBG_RETURN(DbgInfo, Status);
  114. return (Status);
  115. }
  116. /* @doc INTERNAL TpiMem TpiMem_c TpiFreeMemory
  117. @func
  118. <f TpiFreeMemory> provides a wrapper interface for <f NdisFreeMemory>.
  119. This interface is used to help debug memory allocation problems. It can
  120. be used to keep track of how much memory has been allocated and freed by
  121. the Miniport, and can report the usage counters via the debugger.
  122. <f TpiFreeMemory> provides a wrapper interface for standard memory free
  123. via <f NdisFreeMemory>. This interface is used to help debug memory
  124. allocation problems. It can be used to keep track of how much memory
  125. has been allocated and freed by the Miniport, and can report the usage
  126. counters via the debugger.
  127. @comm
  128. This routine uses zero for the <p MemoryFlags> parameter when calling
  129. <f NdisFreeMemory> (i.e. non-paged system memory). Do no use this
  130. routine to free continuous or non-cached memory.
  131. */
  132. void TpiFreeMemory(
  133. IN OUT PVOID * ppObject, // @parm
  134. // Points to a caller-defined memory location which this function
  135. // passes to <f NdisFreeMemory> and then writes NULL to.
  136. IN ULONG dwSize, // @parm
  137. // Specifies the size, in bytes, of the requested memory.
  138. IN ULONG dwFileID, // @parm
  139. // __FILEID__ of the caller.
  140. IN LPSTR szFileName, // @parm
  141. // File name of the caller.
  142. IN ULONG dwLineNumber // @parm
  143. // Line number of the file where called from.
  144. )
  145. {
  146. DBG_FUNC("TpiFreeMemory")
  147. ASSERT(dwSize);
  148. ASSERT(szFileName);
  149. DBG_ENTER(DbgInfo);
  150. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  151. ("\n"
  152. "\t|ppObject=0x%X\n"
  153. "\t|dwSize=%d\n"
  154. "\t|dwFileID=0x%X\n"
  155. "\t|szFileName=%s\n"
  156. "\t|dwLineNumber=%d\n",
  157. ppObject,
  158. dwSize,
  159. dwFileID,
  160. szFileName,
  161. dwLineNumber
  162. ));
  163. if (ppObject && *ppObject)
  164. {
  165. /*
  166. // Release memory to NDIS.
  167. */
  168. NdisFreeMemory(*ppObject, dwSize, 0);
  169. g_MemoryFreed += dwSize;
  170. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  171. ("Memory Allocated=%d Freed=%d -- Ptr=0x%X\n",
  172. g_MemoryAllocated, g_MemoryFreed, *ppObject));
  173. *ppObject = NULL;
  174. }
  175. else
  176. {
  177. DBG_ERROR(DbgInfo,("NULL POINTER (Size=%d, File=%s, Line=%d)\n",
  178. dwSize, szFileName, dwLineNumber));
  179. }
  180. DBG_LEAVE(DbgInfo);
  181. }
  182. /* @doc INTERNAL TpiMem TpiMem_c TpiAllocateSharedMemory
  183. @func
  184. <f TpiAllocateSharedMemory> provides a wrapper interface for shared memory
  185. allocation via <f NdisMAllocateSharedMemory>. This interface is used to help
  186. debug memory allocation problems. It can be used to keep track of how
  187. much memory has been allocated and freed by the Miniport, and can report
  188. the usage counters via the debugger.
  189. @comm
  190. This routine uses zero for the <p MemoryFlags> parameter when calling
  191. <f NdisMAllocateSharedMemory> (i.e. non-paged system memory). Do not
  192. use this routine to allocate continuous or non-cached memory.
  193. @rdesc
  194. <f TpiAllocateSharedMemory> returns zero if it is successful.<nl>
  195. Otherwise, a non-zero return value indicates an error condition.
  196. */
  197. NDIS_STATUS TpiAllocateSharedMemory(
  198. IN NDIS_HANDLE MiniportAdapterHandle, // @parm
  199. // Specifies a handle identifying the miniport's NIC, which is assigned
  200. // by the NDIS library.
  201. IN ULONG dwSize, // @parm
  202. // Specifies the size, in bytes, of the requested memory.
  203. IN BOOLEAN bCached, // @parm
  204. // Specifies whether the requested memory is cached or not.
  205. OUT PVOID * pVirtualAddress, // @parm
  206. // Points to a caller-defined memory location to which this function
  207. // writes the virtual address of the allocated memory. If memory of
  208. // the specified type is not available, the pointer value is NULL.
  209. OUT NDIS_PHYSICAL_ADDRESS * pPhysicalAddress, // @parm
  210. // Points to a caller-defined memory location to which this function
  211. // writes the physical address of the allocated memory. If memory of
  212. // the specified type is not available, the physical address is zero.
  213. IN ULONG dwFileID, // @parm
  214. // __FILEID__ of the caller.
  215. IN LPSTR szFileName, // @parm
  216. // File name of the caller.
  217. IN ULONG dwLineNumber // @parm
  218. // Line number of the file where called from.
  219. )
  220. {
  221. DBG_FUNC("TpiAllocateSharedMemory")
  222. NDIS_STATUS Status;
  223. // Holds the status result returned from an NDIS function call.
  224. ASSERT(pVirtualAddress);
  225. ASSERT(pPhysicalAddress);
  226. ASSERT(dwSize);
  227. ASSERT(szFileName);
  228. DBG_ENTER(DbgInfo);
  229. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  230. ("\n"
  231. "\t|pVirtualAddress=0x%X\n"
  232. "\t|dwSize=%d\n"
  233. "\t|bCached=%d\n"
  234. "\t|dwFileID=0x%X\n"
  235. "\t|szFileName=%s\n"
  236. "\t|dwLineNumber=%d\n",
  237. pVirtualAddress,
  238. dwSize,
  239. bCached,
  240. dwFileID,
  241. szFileName,
  242. dwLineNumber
  243. ));
  244. /*
  245. // Allocate memory from NDIS.
  246. */
  247. NdisMAllocateSharedMemory(MiniportAdapterHandle,
  248. dwSize,
  249. bCached,
  250. pVirtualAddress,
  251. pPhysicalAddress
  252. );
  253. if (*pVirtualAddress)
  254. {
  255. Status = NDIS_STATUS_SUCCESS;
  256. NdisZeroMemory(*pVirtualAddress, dwSize);
  257. g_SharedMemoryAllocated += dwSize;
  258. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  259. ("SharedMemory Allocated=%d Freed=%d -- Ptr=0x%X @0x%X\n",
  260. g_SharedMemoryAllocated, g_SharedMemoryFreed,
  261. *pVirtualAddress, pPhysicalAddress->LowPart));
  262. }
  263. else
  264. {
  265. Status = NDIS_STATUS_RESOURCES;
  266. DBG_ERROR(DbgInfo,("NdisAllocateSharedMemory(Size=%d, File=%s, Line=%d) failed (Status=%X)\n",
  267. dwSize, szFileName, dwLineNumber, Status));
  268. /*
  269. // Log error message and return.
  270. */
  271. NdisWriteErrorLogEntry(
  272. MiniportAdapterHandle,
  273. NDIS_ERROR_CODE_OUT_OF_RESOURCES,
  274. 3,
  275. Status,
  276. dwFileID,
  277. dwLineNumber
  278. );
  279. *pVirtualAddress = NULL;
  280. pPhysicalAddress->LowPart = 0;
  281. pPhysicalAddress->HighPart = 0;
  282. }
  283. DBG_RETURN(DbgInfo, Status);
  284. return (Status);
  285. }
  286. /* @doc INTERNAL TpiMem TpiMem_c TpiFreeSharedMemory
  287. @func
  288. <f TpiFreeSharedMemory> provides a wrapper interface for <f NdisFreeSharedMemory>.
  289. This interface is used to help debug memory allocation problems. It can
  290. be used to keep track of how much memory has been allocated and freed by
  291. the Miniport, and can report the usage counters via the debugger.
  292. <f TpiFreeSharedMemory> provides a wrapper interface for standard memory free
  293. via <f NdisFreeSharedMemory>. This interface is used to help debug memory
  294. allocation problems. It can be used to keep track of how much memory
  295. has been allocated and freed by the Miniport, and can report the usage
  296. counters via the debugger.
  297. @comm
  298. This routine uses zero for the <p MemoryFlags> parameter when calling
  299. <f NdisFreeSharedMemory> (i.e. non-paged system memory). Do no use this
  300. routine to free continuous or non-cached memory.
  301. */
  302. void TpiFreeSharedMemory(
  303. IN NDIS_HANDLE MiniportAdapterHandle, // @parm
  304. // Specifies a handle identifying the miniport's NIC, which is assigned
  305. // by the NDIS library.
  306. IN ULONG dwSize, // @parm
  307. // Specifies the size, in bytes, of the requested memory.
  308. IN BOOLEAN bCached, // @parm
  309. // Specifies whether the requested memory is cached or not.
  310. IN PVOID * pVirtualAddress, // @parm
  311. // Points to a caller-defined memory location to which this function
  312. // writes the virtual address of the allocated memory. If memory of
  313. // the specified type is not available, the pointer value is NULL.
  314. IN NDIS_PHYSICAL_ADDRESS * pPhysicalAddress, // @parm
  315. // Points to a caller-defined memory location to which this function
  316. // writes the physical address of the allocated memory. If memory of
  317. // the specified type is not available, the physical address is zero.
  318. IN ULONG dwFileID, // @parm
  319. // __FILEID__ of the caller.
  320. IN LPSTR szFileName, // @parm
  321. // File name of the caller.
  322. IN ULONG dwLineNumber // @parm
  323. // Line number of the file where called from.
  324. )
  325. {
  326. DBG_FUNC("TpiFreeSharedMemory")
  327. ASSERT(pVirtualAddress);
  328. ASSERT(pPhysicalAddress);
  329. ASSERT(dwSize);
  330. ASSERT(szFileName);
  331. DBG_ENTER(DbgInfo);
  332. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  333. ("\n"
  334. "\t|pVirtualAddress=0x%X\n"
  335. "\t|dwSize=%d\n"
  336. "\t|bCached=%d\n"
  337. "\t|dwFileID=0x%X\n"
  338. "\t|szFileName=%s\n"
  339. "\t|dwLineNumber=%d\n",
  340. pVirtualAddress,
  341. dwSize,
  342. bCached,
  343. dwFileID,
  344. szFileName,
  345. dwLineNumber
  346. ));
  347. if (pVirtualAddress && *pVirtualAddress)
  348. {
  349. /*
  350. // Release memory to NDIS.
  351. */
  352. NdisMFreeSharedMemory(MiniportAdapterHandle,
  353. dwSize,
  354. bCached,
  355. *pVirtualAddress,
  356. *pPhysicalAddress
  357. );
  358. g_SharedMemoryFreed += dwSize;
  359. DBG_FILTER(DbgInfo, DBG_MEMORY_ON,
  360. ("SharedMemory Allocated=%d Freed=%d -- Ptr=0x%X @0x%X\n",
  361. g_SharedMemoryAllocated, g_SharedMemoryFreed,
  362. *pVirtualAddress, pPhysicalAddress->LowPart));
  363. *pVirtualAddress = NULL;
  364. pPhysicalAddress->LowPart = 0;
  365. pPhysicalAddress->HighPart = 0;
  366. }
  367. else
  368. {
  369. DBG_ERROR(DbgInfo,("NULL POINTER (Size=%d, File=%s, Line=%d)\n",
  370. dwSize, szFileName, dwLineNumber));
  371. }
  372. DBG_LEAVE(DbgInfo);
  373. }