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.

316 lines
9.3 KiB

  1. /***************************************************************************\
  2. *
  3. * File: AutoUtil.h
  4. *
  5. * Description:
  6. * AutoUtil.h defines routinues common to most projects, including
  7. * - Macros
  8. * - Disabling known compiler warnings
  9. * - Debugging / Assert
  10. *
  11. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  12. *
  13. \***************************************************************************/
  14. #if !defined(INC__AutoUtil_h__INCLUDED)
  15. #define INC__AutoUtil_h__INCLUDED
  16. //
  17. // Ensure that DBG is defined for DEBUG builds. This is used throughout
  18. // DirectUser for DEBUG-only code, but is only defined (by default) in the
  19. // NT-BUILD environment. If we are compiling using the DSP's, we need to
  20. // ensure that it is defined.
  21. //
  22. #ifdef _DEBUG
  23. #ifndef DBG
  24. #define DBG 1
  25. #endif // !DBG
  26. #endif // !_DEBUG
  27. #include <crtdbg.h>
  28. #include <BaseTyps.h>
  29. #ifdef DUSER_EXPORTS
  30. #define AUTOUTIL_API
  31. #else // DUSER_EXPORTS
  32. #define AUTOUTIL_API __declspec(dllimport)
  33. #endif // DUSER_EXPORTS
  34. /***************************************************************************\
  35. *
  36. * Macros
  37. *
  38. \***************************************************************************/
  39. #define QUOTE(s) #s
  40. #define STRINGIZE(s) QUOTE(s)
  41. #define _countof(x) (sizeof(x) / sizeof(x[0]))
  42. /***************************************************************************\
  43. *
  44. * Warnings
  45. *
  46. \***************************************************************************/
  47. // warnings generated with common MFC/Windows code
  48. #pragma warning(disable: 4127) // constant expression for Trace/Assert
  49. #pragma warning(disable: 4134) // message map member fxn casts
  50. #pragma warning(disable: 4201) // nameless unions are part of C++
  51. #pragma warning(disable: 4511) // private copy constructors are good to have
  52. #pragma warning(disable: 4512) // private operator= are good to have
  53. #pragma warning(disable: 4514) // unreferenced inlines are common
  54. #pragma warning(disable: 4710) // private constructors are disallowed
  55. #pragma warning(disable: 4705) // statement has no effect in optimized code
  56. #pragma warning(disable: 4191) // pointer-to-function casting
  57. #pragma warning(disable: 4204) // initialize structures with non-constant members
  58. #pragma warning(disable: 4221) // initialize structures using address of automatic variable
  59. // warnings caused by normal optimizations
  60. #if DBG
  61. #else // DBG
  62. #pragma warning(disable: 4701) // local variable *may* be used without init
  63. #pragma warning(disable: 4702) // unreachable code caused by optimizations
  64. #pragma warning(disable: 4791) // loss of debugging info in release version
  65. #pragma warning(disable: 4189) // initialized but unused variable
  66. #pragma warning(disable: 4390) // empty controlled statement
  67. #endif // DBG
  68. #define UNREFERENCED_MSG_PARAMETERS(uMsg, wParam, lParam, bHandled)\
  69. UNREFERENCED_PARAMETER(uMsg); \
  70. UNREFERENCED_PARAMETER(wParam); \
  71. UNREFERENCED_PARAMETER(lParam); \
  72. UNREFERENCED_PARAMETER(bHandled)
  73. /***************************************************************************\
  74. *
  75. * Debugging
  76. *
  77. \***************************************************************************/
  78. #undef INTERFACE
  79. #define INTERFACE IDebug
  80. DECLARE_INTERFACE(IDebug)
  81. {
  82. STDMETHOD_(BOOL, AssertFailedLine)(THIS_ LPCSTR pszExpression, LPCSTR pszFileName, UINT idxLineNum) PURE;
  83. STDMETHOD_(BOOL, IsValidAddress)(THIS_ const void * lp, UINT nBytes, BOOL bReadWrite) PURE;
  84. STDMETHOD_(void, BuildStack)(THIS_ HGLOBAL * phStackData, UINT * pcCSEntries) PURE;
  85. STDMETHOD_(BOOL, Prompt)(THIS_ LPCSTR pszExpression, LPCSTR pszFileName, UINT idxLineNum, LPCSTR pszTitle) PURE;
  86. };
  87. EXTERN_C AUTOUTIL_API IDebug * WINAPI GetDebug();
  88. EXTERN_C AUTOUTIL_API void _cdecl AutoTrace(const char * pszFormat, ...);
  89. #if !defined(__cplusplus) || defined(CINTERFACE)
  90. #define IDebug_AssertFailedLine(p, a, b, c) (p)->lpVtbl->AssertFailedLine(p, a, b, c)
  91. #define IDebug_IsValidAddress(p, a, b, c) (p)->lpVtbl->IsValidAddress(p, a, b, c)
  92. #define IDebug_BuildStack(p, a, b) (p)->lpVtbl->BuildStack(p, a, b)
  93. #define IDebug_Prompt(p, a, b, c, d) (p)->lpVtbl->Prompt(p, a, b, c, d)
  94. #else
  95. #define IDebug_AssertFailedLine(p, a, b, c) (p)->AssertFailedLine(a, b, c)
  96. #define IDebug_IsValidAddress(p, a, b, c) (p)->IsValidAddress(a, b, c)
  97. #define IDebug_BuildStack(p, a, b) (p)->BuildStack(a, b)
  98. #define IDebug_Prompt(p, a, b, c, d) (p)->Prompt(a, b, c, d)
  99. #endif
  100. // Define AutoDebugBreak
  101. #ifndef AutoDebugBreak
  102. #define AutoDebugBreak() _CrtDbgBreak()
  103. #endif
  104. // Undefine previous definitions
  105. #ifdef ASSERT
  106. #undef ASSERT
  107. #endif
  108. #ifdef Assert
  109. #undef Assert
  110. #endif
  111. #ifdef AssertMsg
  112. #undef AssertMsg
  113. #endif
  114. #ifdef Verify
  115. #undef Verify
  116. #endif
  117. #ifdef VerifyMsg
  118. #undef VerifyMsg
  119. #endif
  120. #ifdef AssertHR
  121. #undef AssertHR
  122. #endif
  123. #ifdef AssertMsgHR
  124. #undef AssertMsgHR
  125. #endif
  126. #ifdef VerifyHR
  127. #undef VerifyHR
  128. #endif
  129. #ifdef VerifyMsgHR
  130. #undef VerifyMsgHR
  131. #endif
  132. #ifdef Trace
  133. #undef Trace
  134. #endif
  135. // Define Assert, Verify, etc.
  136. #if DBG
  137. // AutoDebug functions that are only available in DEBUG builds
  138. #define Assert(f) \
  139. do \
  140. { \
  141. if (!((f)) && IDebug_AssertFailedLine(GetDebug(), STRINGIZE((f)), __FILE__, __LINE__)) \
  142. AutoDebugBreak(); \
  143. } while (0) \
  144. #define AssertMsg(f, comment) \
  145. do \
  146. { \
  147. if (!((f)) && IDebug_AssertFailedLine(GetDebug(), STRINGIZE((f)) "\r\n" comment, __FILE__, __LINE__)) \
  148. AutoDebugBreak(); \
  149. } while (0) \
  150. #define AssertHR(f) \
  151. do \
  152. { \
  153. if (FAILED((f)) && IDebug_AssertFailedLine(GetDebug(), STRINGIZE((f)), __FILE__, __LINE__)) \
  154. AutoDebugBreak(); \
  155. } while (0) \
  156. #define AssertMsgHR(f, comment) \
  157. do \
  158. { \
  159. if (FAILED((f)) && IDebug_AssertFailedLine(GetDebug(), STRINGIZE((f)) "\r\n" comment, __FILE__, __LINE__)) \
  160. AutoDebugBreak(); \
  161. } while (0) \
  162. #define Verify(f) Assert((f))
  163. #define VerifyMsg(f, comment) AssertMsg((f), comment)
  164. #define VerifyHR(f) AssertHR((f))
  165. #define VerifyMsgHR(f, comment) AssertMsgHR((f), comment)
  166. #define DEBUG_ONLY(f) (f)
  167. #define ASSERT(f) Assert((f))
  168. #define Trace AutoTrace
  169. #define AssertReadPtr(p) \
  170. AssertMsg(IDebug_IsValidAddress(GetDebug(), p, sizeof(char *), FALSE), "Check pointer memory is valid"); \
  171. AssertMsg(p != NULL, "Check pointer is not NULL")
  172. #define AssertReadPtrSize(p, s) \
  173. AssertMsg(IDebug_IsValidAddress(GetDebug(), p, s, FALSE), "Check pointer memory is valid"); \
  174. AssertMsg(p != NULL, "Check pointer is not NULL")
  175. #define AssertWritePtr(p) \
  176. AssertMsg(IDebug_IsValidAddress(GetDebug(), p, sizeof(char *), TRUE), "Check pointer memory is valid"); \
  177. AssertMsg(p != NULL, "Check pointer is not NULL")
  178. #define AssertWritePtrSize(p, s) \
  179. AssertMsg(IDebug_IsValidAddress(GetDebug(), p, s, TRUE), "Check pointer memory is valid"); \
  180. AssertMsg(p != NULL, "Check pointer is not NULL")
  181. #define AssertIndex(idx, nMax) \
  182. AssertMsg((idx < nMax) && (idx >= 0), "Check pointer is not NULL")
  183. #define AssertHWND(hwnd) \
  184. AssertMsg(IsWindow(hwnd), "Check valid window")
  185. #define AssertHandle(h) \
  186. AssertMsg(h != NULL, "Check valid handle")
  187. #define AssertInstance(p) \
  188. do \
  189. { \
  190. AssertWritePtr(p); \
  191. p->DEBUG_AssertValid(); \
  192. } while (0)
  193. #define AssertString(s) \
  194. do \
  195. { \
  196. Assert(s != NULL); \
  197. } while (0)
  198. #else // DBG
  199. #define Assert(f) ((void) 0)
  200. #define AssertMsg(f, comment) ((void) 0)
  201. #define Verify(f) ((void)(f))
  202. #define VerifyMsg(f, comment) ((void)(f, comment))
  203. #define AssertHR(f) ((void) 0)
  204. #define AssertMsgHR(f, comment) ((void) 0)
  205. #define VerifyHR(f) ((void)(f))
  206. #define VerifyMsgHR(f, comment) ((void)(f, comment))
  207. #define DEBUG_ONLY(f) ((void) 0)
  208. #define ASSERT(f) ((void) 0)
  209. #define Trace 1 ? (void) 0 : AutoTrace
  210. #define AssertReadPtr(p) ((void) 0)
  211. #define AssertReadPtrSize(p, s) ((void) 0)
  212. #define AssertWritePtr(p) ((void) 0)
  213. #define AssertWritePtrSize(p, s) ((void) 0)
  214. #define AssertIndex(idx, nMax) ((void) 0)
  215. #define AssertHWND(hwnd) ((void) 0)
  216. #define AssertHandle(h) ((void) 0)
  217. #define AssertInstance(p) ((void) 0)
  218. #define AssertString(s) ((void) 0)
  219. #endif // DBG
  220. #if DBG_CHECK_CALLBACKS
  221. #define AlwaysPromptInvalid(comment) \
  222. do \
  223. { \
  224. if (IDebug_Prompt(GetDebug(), "Validation error:\r\n" comment, __FILE__, __LINE__, "DirectUser/Msg Notification")) \
  225. AutoDebugBreak(); \
  226. } while (0) \
  227. #endif // DBG_CHECK_CALLBACKS
  228. #define CHECK_VALID_READ_PTR(p) \
  229. do \
  230. { \
  231. AssertReadPtr(p); \
  232. if (p == NULL) \
  233. return E_POINTER; \
  234. } while (0)
  235. #define CHECK_VALID_WRITE_PTR(p) \
  236. do \
  237. { \
  238. AssertWritePtr(p); \
  239. if (p == NULL) \
  240. return E_POINTER; \
  241. } while (0)
  242. #define SUPPRESS(ClassName) \
  243. private: \
  244. ClassName(const ClassName & copy); \
  245. ClassName & operator=(const ClassName & rhs); \
  246. public:
  247. #endif // INC__AutoUtil_h__INCLUDED