Leaked source code of windows server 2003
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.

238 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. macros.h
  5. Abstract:
  6. Some macros for NDISUIO.
  7. Environment:
  8. Kernel mode only.
  9. Revision History:
  10. arvindm 4/5/2000 Created
  11. --*/
  12. #ifndef MIN
  13. #define MIN(_a, _b) ((_a) < (_b)? (_a): (_b))
  14. #endif
  15. #if DBG
  16. #define NUIO_REF_OPEN(_pOpen) ndisuioDbgRefOpen(_pOpen, __FILENUMBER, __LINE__)
  17. #define NUIO_DEREF_OPEN(_pOpen) ndisuioDbgDerefOpen(_pOpen, __FILENUMBER, __LINE__)
  18. #else
  19. #define NUIO_REF_OPEN(_pOpen) ndisuioRefOpen(_pOpen)
  20. #define NUIO_DEREF_OPEN(_pOpen) ndisuioDerefOpen(_pOpen)
  21. #endif
  22. //
  23. // Spinlock macros
  24. //
  25. #if DBG_SPIN_LOCK
  26. #define NUIO_INIT_LOCK(_pLock) \
  27. ndisuioAllocateSpinLock(_pLock, __FILENUMBER, __LINE__)
  28. #define NUIO_ACQUIRE_LOCK(_pLock) \
  29. ndisuioAcquireSpinLock(_pLock, __FILENUMBER, __LINE__)
  30. #define NUIO_RELEASE_LOCK(_pLock) \
  31. ndisuioReleaseSpinLock(_pLock, __FILENUMBER, __LINE__)
  32. #else
  33. #define NUIO_INIT_LOCK(_pLock) NdisAllocateSpinLock(_pLock)
  34. #define NUIO_ACQUIRE_LOCK(_pLock) NdisAcquireSpinLock(_pLock)
  35. #define NUIO_RELEASE_LOCK(_pLock) NdisReleaseSpinLock(_pLock)
  36. #endif // DBG
  37. //
  38. // List manipulation.
  39. //
  40. #define NUIO_INIT_LIST_HEAD(_pList) InitializeListHead(_pList)
  41. #define NUIO_IS_LIST_EMPTY(_pList) IsListEmpty(_pList)
  42. #define NUIO_INSERT_HEAD_LIST(_pList, _pEnt) InsertHeadList(_pList, _pEnt)
  43. #define NUIO_INSERT_TAIL_LIST(_pList, _pEnt) InsertTailList(_pList, _pEnt)
  44. #define NUIO_REMOVE_ENTRY_LIST(_pEnt) RemoveEntryList(_pEnt)
  45. //
  46. // Receive packet queueing.
  47. //
  48. #define NUIO_LIST_ENTRY_TO_RCV_PKT(_pEnt) \
  49. CONTAINING_RECORD(CONTAINING_RECORD(_pEnt, NUIO_RECV_PACKET_RSVD, Link), NDIS_PACKET, ProtocolReserved)
  50. #define NUIO_RCV_PKT_TO_LIST_ENTRY(_pPkt) \
  51. (&((PNUIO_RECV_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))->Link)
  52. //
  53. // In case we allocate a receive packet of our own to copy and queue
  54. // received data, we might have to also allocate an auxiliary NDIS_BUFFER
  55. // to map part of the receive buffer (skipping the header bytes), so as
  56. // to satisfy NdisTransferData. In such cases, we keep a pointer to the
  57. // fully mapped receive buffer in the packet reserved space:
  58. //
  59. #define NUIO_RCV_PKT_TO_ORIGINAL_BUFFER(_pPkt) \
  60. (((PNUIO_RECV_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))->pOriginalBuffer)
  61. //
  62. // Send packet context.
  63. //
  64. #define NUIO_IRP_FROM_SEND_PKT(_pPkt) \
  65. (((PNUIO_SEND_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))->pIrp)
  66. #define NUIO_SEND_PKT_RSVD(_pPkt) \
  67. ((PNUIO_SEND_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))
  68. #define NUIO_REF_SEND_PKT(_pPkt) \
  69. (VOID)NdisInterlockedIncrement((PLONG)&NUIO_SEND_PKT_RSVD(_pPkt)->RefCount)
  70. #define NUIO_DEREF_SEND_PKT(_pPkt) \
  71. { \
  72. if (NdisInterlockedDecrement((PLONG)&NUIO_SEND_PKT_RSVD(_pPkt)->RefCount) == 0) \
  73. { \
  74. NdisFreePacket(_pPkt); \
  75. } \
  76. }
  77. #ifdef NDIS51
  78. //
  79. // Cancel IDs are generated by using the partial cancel ID we got from
  80. // NDIS ORed with a monotonically increasing locally generated ID.
  81. //
  82. #define NUIO_CANCEL_ID_LOW_MASK (((ULONG_PTR)-1) >> 8)
  83. #define NUIO_GET_NEXT_CANCEL_ID() \
  84. (PVOID)(Globals.PartialCancelId | \
  85. ((NdisInterlockedIncrement((PLONG)&Globals.LocalCancelId)) & NUIO_CANCEL_ID_LOW_MASK))
  86. #endif // NDIS51
  87. //
  88. // Memory allocation
  89. //
  90. #if DBG
  91. #define NUIO_ALLOC_MEM(_pVar, _Size) \
  92. (_pVar) = ndisuioAuditAllocMem( \
  93. (PVOID)&(_pVar), \
  94. _Size, \
  95. __FILENUMBER, \
  96. __LINE__);
  97. #define NUIO_FREE_MEM(_pMem) \
  98. ndisuioAuditFreeMem(_pMem);
  99. #else
  100. #define NUIO_ALLOC_MEM(_pVar, _Size) \
  101. NdisAllocateMemoryWithTag((PVOID *)(&_pVar), (_Size), NUIO_ALLOC_TAG)
  102. #define NUIO_FREE_MEM(_pMem) \
  103. NdisFreeMemory(_pMem, 0, 0)
  104. #endif // DBG
  105. #define NUIO_ZERO_MEM(_pMem, _ByteCount) \
  106. NdisZeroMemory(_pMem, _ByteCount)
  107. #define NUIO_COPY_MEM(_pDst, _pSrc, _ByteCount) \
  108. NdisMoveMemory(_pDst, _pSrc, _ByteCount)
  109. #define NUIO_MEM_CMP(_p1, _p2, _ByteCount) \
  110. NdisEqualMemory(_p1, _p2, _ByteCount)
  111. #define NUIO_SET_MEM(_pMem, _ByteVal, _ByteCount) \
  112. NdisFillMemory(_pMem, _ByteCount, _ByteVal)
  113. //
  114. // Events.
  115. //
  116. #define NUIO_INIT_EVENT(_pEvent) NdisInitializeEvent(_pEvent)
  117. #define NUIO_SIGNAL_EVENT(_pEvent) NdisSetEvent(_pEvent)
  118. #define NUIO_WAIT_EVENT(_pEvent, _MsToWait) NdisWaitEvent(_pEvent, _MsToWait)
  119. //
  120. // Flags
  121. //
  122. #define NUIO_SET_FLAGS(_FlagsVar, _Mask, _BitsToSet) \
  123. (_FlagsVar) = ((_FlagsVar) & ~(_Mask)) | (_BitsToSet)
  124. #define NUIO_TEST_FLAGS(_FlagsVar, _Mask, _BitsToCheck) \
  125. (((_FlagsVar) & (_Mask)) == (_BitsToCheck))
  126. //
  127. // Block the calling thread for the given duration:
  128. //
  129. #define NUIO_SLEEP(_Seconds) \
  130. { \
  131. NDIS_EVENT _SleepEvent; \
  132. NdisInitializeEvent(&_SleepEvent); \
  133. (VOID)NdisWaitEvent(&_SleepEvent, _Seconds*1000); \
  134. }
  135. #define NDIS_STATUS_TO_NT_STATUS(_NdisStatus, _pNtStatus) \
  136. { \
  137. /* \
  138. * The following NDIS status codes map directly to NT status codes. \
  139. */ \
  140. if (((NDIS_STATUS_SUCCESS == (_NdisStatus)) || \
  141. (NDIS_STATUS_PENDING == (_NdisStatus)) || \
  142. (NDIS_STATUS_BUFFER_OVERFLOW == (_NdisStatus)) || \
  143. (NDIS_STATUS_FAILURE == (_NdisStatus)) || \
  144. (NDIS_STATUS_RESOURCES == (_NdisStatus)) || \
  145. (NDIS_STATUS_NOT_SUPPORTED == (_NdisStatus)))) \
  146. { \
  147. *(_pNtStatus) = (NTSTATUS)(_NdisStatus); \
  148. } \
  149. else if (NDIS_STATUS_BUFFER_TOO_SHORT == (_NdisStatus)) \
  150. { \
  151. /* \
  152. * The above NDIS status codes require a little special casing. \
  153. */ \
  154. *(_pNtStatus) = STATUS_BUFFER_TOO_SMALL; \
  155. } \
  156. else if (NDIS_STATUS_INVALID_LENGTH == (_NdisStatus)) \
  157. { \
  158. *(_pNtStatus) = STATUS_INVALID_BUFFER_SIZE; \
  159. } \
  160. else if (NDIS_STATUS_INVALID_DATA == (_NdisStatus)) \
  161. { \
  162. *(_pNtStatus) = STATUS_INVALID_PARAMETER; \
  163. } \
  164. else if (NDIS_STATUS_ADAPTER_NOT_FOUND == (_NdisStatus)) \
  165. { \
  166. *(_pNtStatus) = STATUS_NO_MORE_ENTRIES; \
  167. } \
  168. else if (NDIS_STATUS_ADAPTER_NOT_READY == (_NdisStatus)) \
  169. { \
  170. *(_pNtStatus) = STATUS_DEVICE_NOT_READY; \
  171. } \
  172. else \
  173. { \
  174. *(_pNtStatus) = STATUS_UNSUCCESSFUL; \
  175. } \
  176. }
  177. #ifdef WIN9X
  178. #undef NdisGetPoolFromPacket
  179. #define NdisGetPoolFromPacket(_Pkt) ((_Pkt)->Private.Pool)
  180. #endif