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.

239 lines
9.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. macros.h
  5. Abstract:
  6. Some macros for NDISPROT.
  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 NPROT_REF_OPEN(_pOpen) ndisprotDbgRefOpen(_pOpen, __FILENUMBER, __LINE__)
  17. #define NPROT_DEREF_OPEN(_pOpen) ndisprotDbgDerefOpen(_pOpen, __FILENUMBER, __LINE__)
  18. #else
  19. #define NPROT_REF_OPEN(_pOpen) ndisprotRefOpen(_pOpen)
  20. #define NPROT_DEREF_OPEN(_pOpen) ndisprotDerefOpen(_pOpen)
  21. #endif
  22. //
  23. // Spinlock macros
  24. //
  25. #if DBG_SPIN_LOCK
  26. #define NPROT_INIT_LOCK(_pLock) \
  27. ndisprotAllocateSpinLock(_pLock, __FILENUMBER, __LINE__)
  28. #define NPROT_ACQUIRE_LOCK(_pLock) \
  29. ndisprotAcquireSpinLock(_pLock, __FILENUMBER, __LINE__)
  30. #define NPROT_RELEASE_LOCK(_pLock) \
  31. ndisprotReleaseSpinLock(_pLock, __FILENUMBER, __LINE__)
  32. #else
  33. #define NPROT_INIT_LOCK(_pLock) NdisAllocateSpinLock(_pLock)
  34. #define NPROT_ACQUIRE_LOCK(_pLock) NdisAcquireSpinLock(_pLock)
  35. #define NPROT_RELEASE_LOCK(_pLock) NdisReleaseSpinLock(_pLock)
  36. #endif // DBG
  37. //
  38. // List manipulation.
  39. //
  40. #define NPROT_INIT_LIST_HEAD(_pList) InitializeListHead(_pList)
  41. #define NPROT_IS_LIST_EMPTY(_pList) IsListEmpty(_pList)
  42. #define NPROT_INSERT_HEAD_LIST(_pList, _pEnt) InsertHeadList(_pList, _pEnt)
  43. #define NPROT_INSERT_TAIL_LIST(_pList, _pEnt) InsertTailList(_pList, _pEnt)
  44. #define NPROT_REMOVE_ENTRY_LIST(_pEnt) RemoveEntryList(_pEnt)
  45. //
  46. // Receive packet queueing.
  47. //
  48. #define NPROT_LIST_ENTRY_TO_RCV_PKT(_pEnt) \
  49. CONTAINING_RECORD(CONTAINING_RECORD(_pEnt, NPROT_RECV_PACKET_RSVD, Link), NDIS_PACKET, ProtocolReserved)
  50. #define NPROT_RCV_PKT_TO_LIST_ENTRY(_pPkt) \
  51. (&((PNPROT_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 NPROT_RCV_PKT_TO_ORIGINAL_BUFFER(_pPkt) \
  60. (((PNPROT_RECV_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))->pOriginalBuffer)
  61. //
  62. // Send packet context.
  63. //
  64. #define NPROT_IRP_FROM_SEND_PKT(_pPkt) \
  65. (((PNPROT_SEND_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))->pIrp)
  66. #define NPROT_SEND_PKT_RSVD(_pPkt) \
  67. ((PNPROT_SEND_PACKET_RSVD)&((_pPkt)->ProtocolReserved[0]))
  68. #define NPROT_REF_SEND_PKT(_pPkt) \
  69. (VOID)NdisInterlockedIncrement((PLONG)&NPROT_SEND_PKT_RSVD(_pPkt)->RefCount)
  70. #define NPROT_DEREF_SEND_PKT(_pPkt) \
  71. { \
  72. if (NdisInterlockedDecrement((PLONG)&NPROT_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 NPROT_CANCEL_ID_LOW_MASK (((ULONG_PTR)-1) >> 8)
  83. #define NPROT_GET_NEXT_CANCEL_ID() \
  84. (PVOID)(Globals.PartialCancelId | \
  85. ((NdisInterlockedIncrement((PLONG)&Globals.LocalCancelId)) & NPROT_CANCEL_ID_LOW_MASK))
  86. #endif // NDIS51
  87. //
  88. // Memory allocation
  89. //
  90. #if DBG
  91. #define NPROT_ALLOC_MEM(_pVar, _Size) \
  92. (_pVar) = ndisprotAuditAllocMem( \
  93. (PVOID)&(_pVar), \
  94. _Size, \
  95. __FILENUMBER, \
  96. __LINE__);
  97. #define NPROT_FREE_MEM(_pMem) \
  98. ndisprotAuditFreeMem(_pMem);
  99. #else
  100. #define NPROT_ALLOC_MEM(_pVar, _Size) \
  101. NdisAllocateMemoryWithTag((PVOID *)(&_pVar), (_Size), NPROT_ALLOC_TAG)
  102. #define NPROT_FREE_MEM(_pMem) \
  103. NdisFreeMemory(_pMem, 0, 0)
  104. #endif // DBG
  105. #define NPROT_ZERO_MEM(_pMem, _ByteCount) \
  106. NdisZeroMemory(_pMem, _ByteCount)
  107. #define NPROT_COPY_MEM(_pDst, _pSrc, _ByteCount) \
  108. NdisMoveMemory(_pDst, _pSrc, _ByteCount)
  109. #define NPROT_MEM_CMP(_p1, _p2, _ByteCount) \
  110. NdisEqualMemory(_p1, _p2, _ByteCount)
  111. #define NPROT_SET_MEM(_pMem, _ByteVal, _ByteCount) \
  112. NdisFillMemory(_pMem, _ByteCount, _ByteVal)
  113. //
  114. // Events.
  115. //
  116. #define NPROT_INIT_EVENT(_pEvent) NdisInitializeEvent(_pEvent)
  117. #define NPROT_SIGNAL_EVENT(_pEvent) NdisSetEvent(_pEvent)
  118. #define NPROT_WAIT_EVENT(_pEvent, _MsToWait) NdisWaitEvent(_pEvent, _MsToWait)
  119. //
  120. // Flags
  121. //
  122. #define NPROT_SET_FLAGS(_FlagsVar, _Mask, _BitsToSet) \
  123. (_FlagsVar) = ((_FlagsVar) & ~(_Mask)) | (_BitsToSet)
  124. #define NPROT_TEST_FLAGS(_FlagsVar, _Mask, _BitsToCheck) \
  125. (((_FlagsVar) & (_Mask)) == (_BitsToCheck))
  126. //
  127. // Block the calling thread for the given duration:
  128. //
  129. #define NPROT_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