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.

408 lines
14 KiB

  1. /*++
  2. Copyright (c) 1992-1996 Microsoft Corporation
  3. Module Name:
  4. rsvp.h
  5. Abstract:
  6. This code contains the macros definitions for rsvp.sys
  7. Author:
  8. Jim Stewart (JStew) June 12, 1996
  9. Environment:
  10. Kernel mode
  11. Revision History:
  12. --*/
  13. #ifndef __TCMACRO_H
  14. #define __TCMACRO_H
  15. #if DBG
  16. #define WSPRINT( stuff ) WsPrintf stuff
  17. #define SETUP_DEBUG_INFO SetupDebugInfo
  18. #define CLOSE_DEBUG CloseDbgFile
  19. #define DUMP_MEM_ALLOCATIONS DumpAllocatedMemory
  20. #define INIT_DBG_MEMORY InitDebugMemory
  21. #define DEINIT_DBG_MEMORY DeInitDebugMemory
  22. #define IF_DEBUG( _ErrLevel ) if ( (DebugMask & DEBUG_ ## _ErrLevel ) != 0 )
  23. #define TC_TRACE(_ErrLevel, String) if((DebugMask & DEBUG_ ## _ErrLevel) != 0) WsPrintf String
  24. #define IF_DEBUG_CHECK(_status,_ErrLevel) if ( (_status != NO_ERROR) && ((DebugMask & DEBUG_ ## _ErrLevel) != 0) )
  25. #define DEBUGBREAK DebugBreak
  26. #undef ASSERT
  27. /*++
  28. VOID
  29. Assert(
  30. IN Value
  31. )
  32. Routine Description:
  33. Checks if the Value passed in is zero. If its zero then assert.
  34. Arguments:
  35. Value - the parameter to check against zero
  36. Return Value:
  37. none
  38. --*/
  39. #define ASSERT( exp ) if ( !(exp) ) WsAssert( #exp, __FILE__,__LINE__ )
  40. /*++
  41. VOID
  42. AllocMem(
  43. OUT PVOID *Address,
  44. IN ULONG Length
  45. )
  46. Routine Description:
  47. Allocates memory and then writes a tag and length into the first two Ulongs. It
  48. then writes the same tag into the last Ulong.
  49. Arguments:
  50. Address - the return address of the memory
  51. Length - the length of the memory to allocate
  52. Return Value:
  53. none
  54. --*/
  55. #define CAllocMem(_num,_size) AllocMemory( _num*_size,TRUE, __FILE__, __LINE__ )
  56. #define AllocMem(_Address,_cb) *_Address = AllocMemory( _cb,FALSE,__FILE__, __LINE__ )
  57. #define ReAllocMem(_pv, _cb) ReAllocMemory(_pv,_cb,__FILE__,__LINE__ )
  58. #if 0
  59. #define AllocMem( _Address,_Length ) \
  60. { \
  61. PULONG _Addr; \
  62. ULONG _Len; \
  63. _Len = _Length + (sizeof(ULONG) << 2); \
  64. _Addr = malloc( _Len ); \
  65. IF_DEBUG(MEMORY_ALLOC) { \
  66. WSPRINT(( "AllocMemory %X, %d bytes %s %d\n",_Addr,_Length,__FILE__,__LINE__ ));\
  67. } \
  68. \
  69. if (_Addr) { \
  70. *(PULONG)_Addr++ = RSVP_TAG; \
  71. *(PULONG)_Addr++ = _Length; \
  72. *_Address = (PVOID)_Addr; \
  73. *(PULONG)((PUCHAR)_Addr + _Length) = RSVP_TAG; \
  74. } else { \
  75. *_Address = NULL; \
  76. } \
  77. }
  78. #endif
  79. /*++
  80. VOID
  81. FreeMem(
  82. IN PVOID Address
  83. )
  84. Routine Description:
  85. Frees non-paged pool. It checks if the tag value is still set at both the beginning
  86. and the end of the pool block and asserts if it's not.
  87. Arguments:
  88. Address - the address of the memory
  89. Return Value:
  90. none
  91. --*/
  92. #define FreeMem(_pv) FreeMemory( _pv,__FILE__,__LINE__ )
  93. #if 0
  94. #define FreeMem( _Address ) \
  95. { \
  96. PULONG _Addr; \
  97. _Addr = (PULONG)((PUCHAR)_Address - (sizeof(ULONG) << 1)); \
  98. if (( *_Addr++ != RSVP_TAG ) || \
  99. (( *(PULONG)((PUCHAR)_Addr + *_Addr + sizeof(ULONG)) ) != RSVP_TAG)) { \
  100. WSPRINT(("Bogus Address passed in - Addr = %X\n",_Address )); \
  101. ASSERT( 0 ); \
  102. } \
  103. _Addr--; \
  104. \
  105. IF_DEBUG(MEMORY_FREE) { \
  106. WSPRINT(( "FreeMemory %X, %d bytes %s %d\n",_Address,*(_Addr + 1),__FILE__,__LINE__ ));\
  107. } \
  108. *(_Addr + 1) = 0; \
  109. free( _Addr ); \
  110. }
  111. #endif
  112. /*++
  113. VOID
  114. CheckMemoryOwner(
  115. IN PVOID Address
  116. )
  117. Routine Description:
  118. Check for a tag in the memory block to ensure we own the memory
  119. Arguments:
  120. Address - the address of the memory
  121. Return Value:
  122. none
  123. --*/
  124. #define CheckMemoryOwner( _Address )
  125. #if 0
  126. #define CheckMemoryOwner( _Address ) \
  127. { \
  128. if (( *(PULONG)((PUCHAR)_Address - sizeof( ULONG ))) != RSVP_TAG ) { \
  129. WSPRINT(("Bogus Address passed in - Addr = %X\n",_Address )); \
  130. ASSERT( 0 ); \
  131. } \
  132. }
  133. #endif
  134. /*++
  135. ULONG
  136. LockedDecrement(
  137. IN PULONG _Count
  138. )
  139. Routine Description:
  140. Atomically decrement a counter and return an indication whether the count has gone to
  141. zero. The value returned will be zero if the count is zero after the decrement. However if
  142. the count is either greater or less than zero then this routine will not return the current
  143. count value, but rather some number that has the same sign as the real count.
  144. Arguments:
  145. _Count - the address of the memory to decrement
  146. Return Value:
  147. none
  148. --*/
  149. #define LockedDecrement( _Count ) \
  150. LockedDec( _Count )
  151. /*++
  152. ULONG
  153. LockedIncrement(
  154. IN PULONG _Count
  155. )
  156. Routine Description:
  157. Atomically increment a counter and return an indication whether the
  158. count has gone to zero. The value returned will be zero if the
  159. count is zero after the increment. However if the count is either
  160. greater or less than zero then this routine will not return the current
  161. count value, but rather some number that has the same sign
  162. as the real count.
  163. Arguments:
  164. _Count - the address of the memory to increment
  165. Return Value:
  166. none
  167. --*/
  168. #define LockedIncrement( _Count ) ++(*_Count)
  169. //
  170. // this macro is used to vector exceptions to the debugger on a DBG build and to
  171. // simply execute the exception handler on a free build
  172. //
  173. #define EXCEPTION_FILTER UnhandledExceptionFilter(GetExceptionInformation())
  174. #define InitLock( _s1 ) { \
  175. IF_DEBUG(LOCKS) WSPRINT(("INITLOCK %s [%d]\n", __FILE__,__LINE__)); \
  176. InitializeCriticalSection( &(_s1).Lock ); \
  177. (_s1).LockAcquired = 0; \
  178. strncpy((_s1).LastAcquireFile, strrchr(__FILE__, '\\')+1, 7); \
  179. (_s1).LastAcquireLine = __LINE__; \
  180. }
  181. #define GetLock(_s1) \
  182. { \
  183. EnterCriticalSection( &(_s1).Lock); \
  184. IF_DEBUG(LOCKS) WSPRINT(("GETLOCK[%X] %s [%d]\n", &(_s1).Lock, __FILE__,__LINE__)); \
  185. (_s1).LockAcquired++; \
  186. ASSERT((_s1).LockAcquired > 0); \
  187. (_s1).LastAcquireLine = __LINE__; \
  188. strncpy((_s1).LastAcquireFile, strrchr(__FILE__,'\\')+1, 7); \
  189. }
  190. #define SafeGetLock(_s1) \
  191. __try { \
  192. EnterCriticalSection( &(_s1).Lock); \
  193. IF_DEBUG(LOCKS) WSPRINT(("SGETLOCK %s [%d]\n", __FILE__,__LINE__));\
  194. (_s1).LockAcquired = TRUE; \
  195. (_s1).LastAcquireLine = __LINE__; \
  196. strncpy((_s1).LastAcquireFile, strrchr(__FILE__,'\\')+1, 7); \
  197. #define FreeLock(_s1) \
  198. { \
  199. IF_DEBUG(LOCKS) WSPRINT(("FREELOCK[%X] %s [%d]\n", &(_s1).Lock, __FILE__,__LINE__));\
  200. (_s1).LockAcquired--; \
  201. ASSERT((_s1).LockAcquired >= 0); \
  202. (_s1).LastReleaseLine = __LINE__; \
  203. strncpy((_s1).LastReleaseFile, strrchr(__FILE__,'\\')+1, 7); \
  204. LeaveCriticalSection( &(_s1).Lock); \
  205. }
  206. #define SafeFreeLock(_s1) \
  207. } __finally { \
  208. IF_DEBUG(LOCKS) WSPRINT(("SFREELOCK %s [%d]\n", __FILE__,__LINE__));\
  209. (_s1).LockAcquired = FALSE; \
  210. (_s1).LastReleaseLine = __LINE__; \
  211. strncpy((_s1).LastReleaseFile, strrchr(__FILE__,'\\')+1, 7); \
  212. LeaveCriticalSection( &(_s1).Lock); \
  213. }
  214. #define DeleteLock( _s1 ) { \
  215. IF_DEBUG(LOCKS) WSPRINT(("DELLOCK[%X] %s [%d]\n", &(_s1).Lock, __FILE__,__LINE__)); \
  216. (_s1).LockAcquired--; \
  217. ASSERT((_s1).LockAcquired == -1); \
  218. strncpy((_s1).LastReleaseFile, strrchr(__FILE__, '\\')+1, 7); \
  219. (_s1).LastReleaseLine = __LINE__; \
  220. DeleteCriticalSection(&(_s1).Lock); \
  221. }
  222. #define QUERY_STATE(_p) (_p).State
  223. #define SET_STATE(_p, _state) { \
  224. IF_DEBUG(STATES) { \
  225. DbgPrint("Setting Object to STATE [%s] (File:%s, Line%d)\n", TC_States[_state], __FILE__, __LINE__); \
  226. } \
  227. (_p).PreviousState = (_p).State; \
  228. (_p).PreviousStateLine = (_p).CurrentStateLine; \
  229. (_p).CurrentStateLine = __LINE__; \
  230. strncpy((_p).PreviousStateFile, (_p).CurrentStateFile, 7); \
  231. strncpy((_p).CurrentStateFile, strrchr(__FILE__, '\\')+1, 7); \
  232. (_p).State = _state; \
  233. }
  234. #else // DBG
  235. //
  236. // These are the NON debug versions of the macros
  237. //
  238. #define IF_DEBUG( _ErrLevel ) if (FALSE)
  239. #define IF_DEBUG_CHECK( _status,_ErrLevel ) if (FALSE)
  240. #ifndef ASSERT
  241. #define ASSERT(a)
  242. #endif
  243. #define WSPRINT(stuff)
  244. #define TC_TRACE(_ErrLevel, stuff)
  245. #define SETUP_DEBUG_INFO()
  246. #define CLOSE_DEBUG()
  247. #define DUMP_MEM_ALLOCATIONS()
  248. #define INIT_DBG_MEMORY()
  249. #define DEINIT_DBG_MEMORY()
  250. #define DEBUGBREAK()
  251. #define AllocMem( _Addr,_Len ) \
  252. *_Addr = malloc(_Len )
  253. #define FreeMem( _Address ) \
  254. free( _Address )
  255. #define CheckMemoryOwner( Address )
  256. #define LockedDecrement( _Count ) \
  257. CTEInterlockedDecrementLong( _Count )
  258. #define EXCEPTION_FILTER EXCEPTION_EXECUTE_HANDLER
  259. #define InitLock( _s1 ) InitializeCriticalSection( &(_s1).Lock)
  260. #define DeleteLock( _s1 ) DeleteCriticalSection( &(_s1).Lock)
  261. #define GetLock( _s1 ) EnterCriticalSection( &(_s1).Lock)
  262. #define SafeGetLock( _s1 ) __try { EnterCriticalSection( &(_s1).Lock);
  263. #define FreeLock(_s1) LeaveCriticalSection( &(_s1).Lock)
  264. #define SafeFreeLock( _s1 ) } __finally {LeaveCriticalSection( &(_s1).Lock);}
  265. #define SET_STATE(_p, _state) { (_p).State = _state; }
  266. #define QUERY_STATE(_p) (_p).State
  267. #endif // DBG
  268. /*++
  269. ULONG
  270. IS_LENGTH(
  271. IN ULONG _Length,
  272. )
  273. Routine Description:
  274. This calculates the number of 32 bit words in a length and returns that. It is used
  275. for Int Serv objects that require the size in 32 bit words.
  276. Arguments:
  277. _Length - Length
  278. Return Value:
  279. number of 32 bit words
  280. --*/
  281. #define IS_LENGTH( _Length ) \
  282. (_Length + 3)/sizeof(ULONG)
  283. //#define IS_INITIALIZED (Initialized)
  284. #define VERIFY_INITIALIZATION_STATUS \
  285. if (InitializationStatus != NO_ERROR) return InitializationStatus
  286. #define OffsetToPtr(Base, Offset) ((PBYTE) ((PBYTE)Base + Offset))
  287. #define ERROR_FAILED(_stat) (_stat!=NO_ERROR && _stat!=ERROR_SIGNAL_PENDING)
  288. #define ERROR_PENDING(_stat) (_stat==ERROR_SIGNAL_PENDING)
  289. #define MULTIPLE_OF_EIGHT(_x) (((_x)+7) & ~7)
  290. #endif // end of file