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.

202 lines
8.0 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. macros.h
  5. Abstract:
  6. Some macros for TUNMP.
  7. Environment:
  8. Kernel mode only.
  9. Revision History:
  10. alid 10/22/2001 copied from macros.h for ndisuio
  11. arvindm 4/5/2000 Created
  12. --*/
  13. #ifndef MIN
  14. #define MIN(_a, _b) ((_a) < (_b)? (_a): (_b))
  15. #endif
  16. #if DBG
  17. #define TUN_REF_ADAPTER(_pAdapter) TunMpDbgRefAdapter(_pAdapter, __FILENUMBER, __LINE__)
  18. #define TUN_DEREF_ADAPTER(_pAdapter) TunMpDbgDerefAdapter(_pAdapter, __FILENUMBER, __LINE__)
  19. #else
  20. #define TUN_REF_ADAPTER(_pAdapter) TunMpRefAdapter(_pAdapter)
  21. #define TUN_DEREF_ADAPTER(_pAdapter) TunMpDerefAdapter(_pAdapter)
  22. #endif
  23. //
  24. // Spinlock macros
  25. //
  26. #if DBG_SPIN_LOCK
  27. #define TUN_INIT_LOCK(_pLock) \
  28. TunAllocateSpinLock(_pLock, __FILENUMBER, __LINE__)
  29. #define TUN_ACQUIRE_LOCK(_pLock) \
  30. TunAcquireSpinLock(_pLock, __FILENUMBER, __LINE__)
  31. #define TUN_RELEASE_LOCK(_pLock) \
  32. TunReleaseSpinLock(_pLock, __FILENUMBER, __LINE__)
  33. #else
  34. #define TUN_INIT_LOCK(_pLock) NdisAllocateSpinLock(_pLock)
  35. #define TUN_ACQUIRE_LOCK(_pLock) NdisAcquireSpinLock(_pLock)
  36. #define TUN_RELEASE_LOCK(_pLock) NdisReleaseSpinLock(_pLock)
  37. #endif // DBG
  38. //
  39. // List manipulation.
  40. //
  41. #define TUN_INIT_LIST_HEAD(_pList) InitializeListHead(_pList)
  42. #define TUN_IS_LIST_EMPTY(_pList) IsListEmpty(_pList)
  43. #define TUN_INSERT_HEAD_LIST(_pList, _pEnt) InsertHeadList(_pList, _pEnt)
  44. #define TUN_INSERT_TAIL_LIST(_pList, _pEnt) InsertTailList(_pList, _pEnt)
  45. #define TUN_REMOVE_ENTRY_LIST(_pEnt) RemoveEntryList(_pEnt)
  46. //
  47. // Receive packet queueing.
  48. //
  49. #define TUN_LIST_ENTRY_TO_RCV_PKT(_pEnt) \
  50. CONTAINING_RECORD(CONTAINING_RECORD(_pEnt, TUN_RECV_PACKET_RSVD, Link), NDIS_PACKET, MiniportReserved)
  51. #define TUN_RCV_PKT_TO_LIST_ENTRY(_pPkt) \
  52. (&((PTUN_RECV_PACKET_RSVD)&((_pPkt)->MiniportReserved[0]))->Link)
  53. //
  54. // Send packet context.
  55. //
  56. #define TUN_IRP_FROM_SEND_PKT(_pPkt) \
  57. (((PTUN_SEND_PACKET_RSVD)&((_pPkt)->MiniportReserved[0]))->pIrp)
  58. #define TUN_SEND_PKT_RSVD(_pPkt) \
  59. ((PTUN_SEND_PACKET_RSVD)&((_pPkt)->MiniportReserved[0]))
  60. #define TUN_REF_SEND_PKT(_pPkt) \
  61. (VOID)NdisInterlockedIncrement(&TUN_SEND_PKT_RSVD(_pPkt)->RefCount)
  62. #define TUN_DEREF_SEND_PKT(_pPkt) \
  63. { \
  64. if (NdisInterlockedDecrement(&TUN_SEND_PKT_RSVD(_pPkt)->RefCount) == 0) \
  65. { \
  66. NdisFreePacket(_pPkt); \
  67. } \
  68. }
  69. //
  70. // Memory allocation
  71. //
  72. #if DBG
  73. #define TUN_ALLOC_MEM(_pVar, _Size) \
  74. (_pVar) = TunAuditAllocMem( \
  75. (PVOID)&(_pVar), \
  76. _Size, \
  77. __FILENUMBER, \
  78. __LINE__);
  79. #define TUN_FREE_MEM(_pMem) \
  80. TunAuditFreeMem(_pMem);
  81. #else
  82. #define TUN_ALLOC_MEM(_pVar, _Size) \
  83. NdisAllocateMemoryWithTag((PVOID *)(&_pVar), (_Size), TUN_ALLOC_TAG)
  84. #define TUN_FREE_MEM(_pMem) \
  85. NdisFreeMemory(_pMem, 0, 0)
  86. #endif // DBG
  87. #define TUN_ZERO_MEM(_pMem, _ByteCount) \
  88. NdisZeroMemory(_pMem, _ByteCount)
  89. #define TUN_COPY_MEM(_pDst, _pSrc, _ByteCount) \
  90. NdisMoveMemory(_pDst, _pSrc, _ByteCount)
  91. #define TUN_MEM_CMP(_p1, _p2, _ByteCount) \
  92. NdisEqualMemory(_p1, _p2, _ByteCount)
  93. #define TUN_SET_MEM(_pMem, _ByteVal, _ByteCount) \
  94. NdisFillMemory(_pMem, _ByteCount, _ByteVal)
  95. //
  96. // Events.
  97. //
  98. #define TUN_INIT_EVENT(_pEvent) NdisInitializeEvent(_pEvent)
  99. #define TUN_SIGNAL_EVENT(_pEvent) NdisSetEvent(_pEvent)
  100. #define TUN_WAIT_EVENT(_pEvent, _MsToWait) NdisWaitEvent(_pEvent, _MsToWait)
  101. //
  102. // Block the calling thread for the given duration:
  103. //
  104. #define TUN_SLEEP(_Seconds) \
  105. { \
  106. NDIS_EVENT _SleepEvent; \
  107. NdisInitializeEvent(&_SleepEvent); \
  108. (VOID)NdisWaitEvent(&_SleepEvent, _Seconds*1000); \
  109. }
  110. #define NDIS_STATUS_TO_NT_STATUS(_NdisStatus, _pNtStatus) \
  111. { \
  112. /* \
  113. * The following NDIS status codes map directly to NT status codes. \
  114. */ \
  115. if (((NDIS_STATUS_SUCCESS == (_NdisStatus)) || \
  116. (NDIS_STATUS_PENDING == (_NdisStatus)) || \
  117. (NDIS_STATUS_BUFFER_OVERFLOW == (_NdisStatus)) || \
  118. (NDIS_STATUS_FAILURE == (_NdisStatus)) || \
  119. (NDIS_STATUS_RESOURCES == (_NdisStatus)) || \
  120. (NDIS_STATUS_NOT_SUPPORTED == (_NdisStatus)))) \
  121. { \
  122. *(_pNtStatus) = (NTSTATUS)(_NdisStatus); \
  123. } \
  124. else if (NDIS_STATUS_BUFFER_TOO_SHORT == (_NdisStatus)) \
  125. { \
  126. /* \
  127. * The above NDIS status codes require a little special casing. \
  128. */ \
  129. *(_pNtStatus) = STATUS_BUFFER_TOO_SMALL; \
  130. } \
  131. else if (NDIS_STATUS_INVALID_LENGTH == (_NdisStatus)) \
  132. { \
  133. *(_pNtStatus) = STATUS_INVALID_BUFFER_SIZE; \
  134. } \
  135. else if (NDIS_STATUS_INVALID_DATA == (_NdisStatus)) \
  136. { \
  137. *(_pNtStatus) = STATUS_INVALID_PARAMETER; \
  138. } \
  139. else if (NDIS_STATUS_ADAPTER_NOT_FOUND == (_NdisStatus)) \
  140. { \
  141. *(_pNtStatus) = STATUS_NO_MORE_ENTRIES; \
  142. } \
  143. else if (NDIS_STATUS_ADAPTER_NOT_READY == (_NdisStatus)) \
  144. { \
  145. *(_pNtStatus) = STATUS_DEVICE_NOT_READY; \
  146. } \
  147. else \
  148. { \
  149. *(_pNtStatus) = STATUS_UNSUCCESSFUL; \
  150. } \
  151. }