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.

245 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. common.h
  5. Abstract:
  6. Private header file for sputils
  7. Author:
  8. Jamie Hunter (JamieHun) Jun-27-2000
  9. Revision History:
  10. --*/
  11. //
  12. // internally we may use some definitions from these files
  13. //
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <windows.h>
  18. #include <windowsx.h>
  19. #include <stddef.h>
  20. #include <regstr.h>
  21. #include <tchar.h>
  22. #include <malloc.h> // for _resetstkoflw
  23. #include <setupapi.h>
  24. #include <spapip.h>
  25. #include "strtab.h"
  26. #include "locking.h"
  27. //
  28. // if a function is private to this library, we don't want to collide with functions
  29. // in other libraries etc
  30. // since C doesn't have namespaces, either make "static" or prefix _pSpUtils
  31. //
  32. #ifndef ASSERTS_ON
  33. #if DBG
  34. #define ASSERTS_ON 1
  35. #else
  36. #define ASSERTS_ON 0
  37. #endif
  38. #endif
  39. #if DBG
  40. #ifndef MEM_DBG
  41. #define MEM_DBG 1
  42. #endif
  43. #else
  44. #ifndef MEM_DBG
  45. #define MEM_DBG 0
  46. #endif
  47. #endif
  48. VOID
  49. _pSpUtilsAssertFail(
  50. IN PCSTR FileName,
  51. IN UINT LineNumber,
  52. IN PCSTR Condition
  53. );
  54. #if ASSERTS_ON
  55. #define MYASSERT(x) if(!(x)) { _pSpUtilsAssertFail(__FILE__,__LINE__,#x); }
  56. #define MYVERIFY(x) ((x)? TRUE : _pSpUtilsAssertFail(__FILE__,__LINE__,#x), FALSE)
  57. #else
  58. #define MYASSERT(x)
  59. #define MYVERIFY(x) ((x)? TRUE : FALSE)
  60. #endif
  61. #define ARRAYSIZE(x) (sizeof((x))/sizeof((x)[0]))
  62. #define SIZECHARS(x) ARRAYSIZE(x)
  63. #define CSTRLEN(x) (SIZECHARS(x)-1)
  64. DWORD
  65. __inline
  66. _pSpUtilsGetLastError(
  67. #if ASSERTS_ON
  68. IN PCSTR Filename,
  69. IN DWORD Line
  70. #else
  71. VOID
  72. #endif
  73. )
  74. /*++
  75. Routine Description:
  76. This inline routine retrieves a Win32 error, and guarantees that the error
  77. isn't NO_ERROR. This routine should not be called unless the preceding
  78. call failed, and GetLastError() is supposed to contain the problem's cause.
  79. Arguments:
  80. If asserts are turned on, this function takes the (ANSI) Filename of the
  81. source file that called the failing function, and also the DWORD Line
  82. number where the call was made. This makes it much easier to debug
  83. scenarios where the failing function didn't set last error when it was
  84. supposed to.
  85. Return Value:
  86. Win32 error code retrieved via GetLastError(), or ERROR_UNIDENTIFIED_ERROR
  87. if GetLastError() returned NO_ERROR.
  88. --*/
  89. {
  90. DWORD Err = GetLastError();
  91. #if ASSERTS_ON
  92. if(Err == NO_ERROR) {
  93. _pSpUtilsAssertFail(Filename,
  94. Line,
  95. "GetLastError() != NO_ERROR"
  96. );
  97. }
  98. #endif
  99. return ((Err == NO_ERROR) ? ERROR_UNIDENTIFIED_ERROR : Err);
  100. }
  101. //
  102. // Macro to simplify calling of a function that reports error status via
  103. // GetLastError(). This macro allows the caller to specify what Win32 error
  104. // code should be returned if the function reports success. (If the default of
  105. // NO_ERROR is desired, use the GLE_FN_CALL macro instead.)
  106. //
  107. // The "prototype" of this macro is as follows:
  108. //
  109. // DWORD
  110. // GLE_FN_CALL_WITH_SUCCESS(
  111. // SuccessfulStatus, // Win32 error code to return if function succeeded
  112. // FailureIndicator, // value returned by function to indicate failure (e.g., FALSE, NULL, INVALID_HANDLE_VALUE)
  113. // FunctionCall // actual call to the function
  114. // );
  115. //
  116. #if ASSERTS_ON
  117. #define GLE_FN_CALL_WITH_SUCCESS(SuccessfulStatus, \
  118. FailureIndicator, \
  119. FunctionCall) \
  120. \
  121. (SetLastError(NO_ERROR), \
  122. (((FunctionCall) != (FailureIndicator)) \
  123. ? (SuccessfulStatus) \
  124. : _pSpUtilsGetLastError(__FILE__, __LINE__)))
  125. #else
  126. #define GLE_FN_CALL_WITH_SUCCESS(SuccessfulStatus, \
  127. FailureIndicator, \
  128. FunctionCall) \
  129. \
  130. (SetLastError(NO_ERROR), \
  131. (((FunctionCall) != (FailureIndicator)) \
  132. ? (SuccessfulStatus) \
  133. : _pSpUtilsGetLastError()))
  134. #endif
  135. //
  136. // Macro to simplify calling of a function that reports error status via
  137. // GetLastError(). If the function call is successful, NO_ERROR is returned.
  138. // (To specify an alternate value returned upon success, use the
  139. // GLE_FN_CALL_WITH_SUCCESS macro instead.)
  140. //
  141. // The "prototype" of this macro is as follows:
  142. //
  143. // DWORD
  144. // GLE_FN_CALL(
  145. // FailureIndicator, // value returned by function to indicate failure (e.g., FALSE, NULL, INVALID_HANDLE_VALUE)
  146. // FunctionCall // actual call to the function
  147. // );
  148. //
  149. #define GLE_FN_CALL(FailureIndicator, FunctionCall) \
  150. GLE_FN_CALL_WITH_SUCCESS(NO_ERROR, FailureIndicator, FunctionCall)
  151. VOID
  152. _pSpUtilsExceptionHandler(
  153. IN DWORD ExceptionCode,
  154. IN DWORD AccessViolationError,
  155. OUT PDWORD Win32ErrorCode OPTIONAL
  156. );
  157. LONG
  158. _pSpUtilsExceptionFilter(
  159. DWORD ExceptionCode
  160. );
  161. BOOL
  162. _pSpUtilsMemoryInitialize(
  163. VOID
  164. );
  165. BOOL
  166. _pSpUtilsMemoryUninitialize(
  167. VOID
  168. );
  169. VOID
  170. _pSpUtilsDebugPrintEx(
  171. DWORD Level,
  172. PCTSTR format,
  173. ... OPTIONAL
  174. );
  175. //
  176. // internally turn on the extra memory debug code if requested
  177. //
  178. #if MEM_DBG
  179. #undef pSetupCheckedMalloc
  180. #undef pSetupCheckInternalHeap
  181. #undef pSetupMallocWithTag
  182. #define pSetupCheckedMalloc(Size) pSetupDebugMalloc(Size,__FILE__,__LINE__)
  183. #define pSetupCheckInternalHeap() pSetupHeapCheck()
  184. #define pSetupMallocWithTag(Size,Tag) pSetupDebugMallocWithTag(Size,__FILE__,__LINE__,Tag)
  185. #endif
  186. //
  187. // internal tags
  188. //
  189. #ifdef UNICODE
  190. #define MEMTAG_STATICSTRINGTABLE (0x5353484a) // JHSS
  191. #define MEMTAG_STRINGTABLE (0x5453484a) // JHST
  192. #define MEMTAG_STRINGDATA (0x4453484a) // JHSD
  193. #else
  194. #define MEMTAG_STATICSTRINGTABLE (0x7373686a) // jhss
  195. #define MEMTAG_STRINGTABLE (0x7473686a) // jhst
  196. #define MEMTAG_STRINGDATA (0x6473686a) // jhsd
  197. #endif