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.

201 lines
4.7 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. utils.h
  5. Abstract:
  6. <abstract>
  7. --*/
  8. #ifndef _WBEMPERF_UTILS_H_
  9. #define _WBEMPERF_UTILS_H_
  10. #ifdef __cplusplus
  11. __inline wchar_t* Macro_CloneLPWSTR(wchar_t *x)
  12. {
  13. wchar_t *s;
  14. size_t cchSize;
  15. if (!x) return NULL;
  16. cchSize = wcslen(x) + 1;
  17. s = new wchar_t[cchSize];
  18. if (!s) return NULL;
  19. StringCchCopyW( s, cchSize, x );
  20. return s;
  21. }
  22. extern "C" {
  23. #endif
  24. #include <windows.h>
  25. #include <winperf.h>
  26. #ifndef _OUTPUT_DEBUG_STRINGS
  27. #define _OUTPUT_DEBUG_STRINGS 0
  28. #endif
  29. #ifndef _DEBUG_MUTEXES
  30. #define _DEBUG_MUTEXES 0 // for debugging locks
  31. #endif // _DEBUG_MUTEXES undefined
  32. extern HANDLE hEventLog;
  33. #define ALLOCMEM(heap, flags, size) HeapAlloc (heap, flags, size)
  34. #define REALLOCMEM(heap, flags, pointer, newsize) \
  35. HeapReAlloc(heap, flags, pointer, newsize)
  36. #define FREEMEM(heap, flags, pointer) if( NULL != pointer ){ HeapFree (heap, flags, pointer); }
  37. // convert mS to relative time
  38. #define MakeTimeOutValue(ms) ((LONGLONG)((LONG)(ms) * -10000L))
  39. #define CLOSE_WAIT_TIME 5000L // wait time for query mutex (in ms)
  40. #define QUERY_WAIT_TIME 2000L // wait time for query mutex (in ms)
  41. #define OPEN_PROC_WAIT_TIME 10000L // default wait time for open proc to finish (in ms)
  42. // query types
  43. #define QUERY_GLOBAL 1
  44. #define QUERY_ITEMS 2
  45. #define QUERY_FOREIGN 3
  46. #define QUERY_COSTLY 4
  47. #define QUERY_COUNTER 5
  48. #define QUERY_HELP 6
  49. #define QUERY_ADDCOUNTER 7
  50. #define QUERY_ADDHELP 8
  51. #define WBEMPERF_STRING_SIZE 1024
  52. extern const WCHAR GLOBAL_STRING[];
  53. extern const WCHAR COSTLY_STRING[];
  54. extern const DWORD VALUE_NAME_LENGTH;
  55. //
  56. // Utility macro. This is used to reserve a DWORD multiple of
  57. // bytes for Unicode strings embedded in the definitional data,
  58. // viz., object instance names.
  59. //
  60. // (assumes dword is 4 bytes, quadword is 8 bytes)
  61. #define DWORD_MULTIPLE(x) (((ULONG)(x) + ((4)-1)) & ~((ULONG)(4)-1))
  62. #define QWORD_MULTIPLE(x) (((ULONG)(x) + ((8)-1)) & ~((ULONG)(8)-1))
  63. #define ALIGN_ON_DWORD(x) \
  64. ((VOID *)(((ULONG_PTR)(x) + ((4)-1)) & ~((ULONG_PTR)(4)-1)))
  65. #define ALIGN_ON_QWORD(x) \
  66. ((VOID *)(((ULONG_PTR)(x) + ((8)-1)) & ~((ULONG_PTR)(8)-1)))
  67. //
  68. // function prototypes
  69. //
  70. BOOL
  71. MonBuildPerfDataBlock(
  72. PERF_DATA_BLOCK *pBuffer,
  73. PVOID *pBufferNext,
  74. DWORD NumObjectTypes,
  75. DWORD DefaultObject
  76. );
  77. BOOL
  78. MatchString (
  79. IN LPCWSTR lpValueArg,
  80. IN LPCWSTR lpNameArg
  81. );
  82. DWORD
  83. GetNextNumberFromList (
  84. IN LPWSTR szStartChar,
  85. IN LPWSTR *szNextChar
  86. );
  87. BOOL
  88. IsNumberInUnicodeList (
  89. IN DWORD dwNumber,
  90. IN LPWSTR lpwszUnicodeList
  91. );
  92. LPWSTR
  93. ConvertProcName(
  94. LPSTR strProcName,
  95. LPWSTR buffer,
  96. DWORD cchBuffer
  97. );
  98. #if _DEBUG_MUTEXES
  99. #include <stdio.h>
  100. __inline
  101. void
  102. PdhiLocalWaitForMutex (
  103. LPCSTR szSourceFileName,
  104. DWORD dwLineNo,
  105. HANDLE hMutex
  106. )
  107. {
  108. DWORD dwReturnValue;
  109. CHAR szOutputString[WBEMPERF_STRING_SIZE];
  110. FILETIME ft;
  111. if (hMutex != NULL) {
  112. GetSystemTimeAsFileTime (&ft);
  113. dwReturnValue = WaitForSingleObject (hMutex, 10000);
  114. sprintf (szOutputString, "\n[%8.8x] Mutex [%8.8x] %s by (%d) at: %s (%d)",
  115. ft.dwLowDateTime,
  116. (DWORD)hMutex,
  117. (dwReturnValue == 0 ? "Locked" : "Lock Failed"),
  118. GetCurrentThreadId(),
  119. szSourceFileName, dwLineNo);
  120. } else {
  121. sprintf (szOutputString, "\nLock of NULL Mutex attmpted at: %s (%d)",
  122. szSourceFileName, dwLineNo);
  123. }
  124. OutputDebugStringA (szOutputString);
  125. }
  126. #define WAIT_FOR_AND_LOCK_MUTEX(h) PdhiLocalWaitForMutex (__FILE__, __LINE__, h);
  127. __inline
  128. void
  129. PdhiLocalReleaseMutex (
  130. LPCSTR szSourceFileName,
  131. DWORD dwLineNo,
  132. HANDLE hMutex
  133. )
  134. {
  135. BOOL bSuccess;
  136. CHAR szOutputString[WBEMPERF_STRING_SIZE];
  137. LONG lPrevCount = 0;
  138. FILETIME ft;
  139. if (hMutex != NULL) {
  140. GetSystemTimeAsFileTime (&ft);
  141. bSuccess = ReleaseMutex (hMutex);
  142. sprintf (szOutputString, "\n[%8.8x] Mutex [%8.8x] %s by (%d) at: %s (%d)",
  143. ft.dwLowDateTime,
  144. (DWORD)hMutex,
  145. (bSuccess ? "Released" : "Release Failed"),
  146. GetCurrentThreadId(),
  147. szSourceFileName, dwLineNo);
  148. } else {
  149. sprintf (szOutputString, "\nRelease of NULL Mutex attempted at: %s (%d)",
  150. szSourceFileName, dwLineNo);
  151. }
  152. OutputDebugStringA (szOutputString);
  153. }
  154. #define RELEASE_MUTEX(h) PdhiLocalReleaseMutex (__FILE__, __LINE__, h);
  155. #else
  156. // 10 second wait timeout
  157. #define WAIT_FOR_AND_LOCK_MUTEX(h) (h != NULL ? WaitForSingleObject(h, 10000) : WAIT_TIMEOUT)
  158. #define RELEASE_MUTEX(h) (h != NULL ? ReleaseMutex(h) : FALSE)
  159. #endif
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif //_PERFLIB_UTILS_H_