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.

531 lines
15 KiB

  1. #ifndef _UTILW32_H
  2. #define _UTILW32_H
  3. // Redirect Asserts in inline code to seem to fire from this file
  4. #define szAssertFilename __FILE__
  5. #ifdef DEBUG
  6. ERR ErrERRCheck( ERR err );
  7. #else
  8. #define ErrERRCheck( err ) ( err )
  9. #endif
  10. // Init / Term
  11. ERR ErrUtilInit(void);
  12. VOID UtilTerm(void);
  13. // Miscellaneous
  14. STATIC INLINE DWORD DwUtilGetLastError( void ) { return GetLastError(); }
  15. STATIC INLINE ULONG UlUtilGetTickCount( VOID ) { return GetTickCount(); }
  16. STATIC INLINE QWORD QwUtilPerfCount( VOID )
  17. {
  18. QWORDX qwx;
  19. if ( !QueryPerformanceCounter( (LARGE_INTEGER *) &qwx.qw ) )
  20. {
  21. qwx.h = GetTickCount();
  22. qwx.l = 0;
  23. }
  24. return qwx.qw;
  25. }
  26. STATIC INLINE QWORD QwUtilPerfCountFrq( VOID )
  27. {
  28. QWORDX qwx;
  29. if ( !QueryPerformanceFrequency( (LARGE_INTEGER *) &qwx.qw ) )
  30. {
  31. qwx.h = 1000;
  32. qwx.l = 0;
  33. }
  34. return qwx.qw;
  35. }
  36. STATIC INLINE double DblUtilPerfElapsedTime( QWORD qwStartCount )
  37. {
  38. return (signed __int64) ( QwUtilPerfCount() - qwStartCount )
  39. / (double) (signed __int64) QwUtilPerfCountFrq();
  40. }
  41. STATIC INLINE double DblUtilPerfElapsedTime2( QWORD qwStartCount, QWORD qwEndCount )
  42. {
  43. return (signed __int64) ( qwEndCount - qwStartCount )
  44. / (double) (signed __int64) QwUtilPerfCountFrq();
  45. }
  46. CHAR *GetDebugEnvValue( CHAR *szEnvVar );
  47. ERR ErrUtilCloseHandle( HANDLE hf );
  48. // for system information such as virtual page size and malloc granularity
  49. extern SYSTEM_INFO siSystemConfig;
  50. // Event Logging
  51. typedef unsigned long MessageId;
  52. #include "jetmsg.h"
  53. extern int fNoWriteAssertEvent;
  54. VOID UtilReportEvent( WORD fwEventType, WORD fwCategory, DWORD IDEvent, WORD cStrings, LPCTSTR *plpszStrings );
  55. VOID UtilReportEventOfError( WORD fwCategory, DWORD IDEvent, ERR err );
  56. // Registry Support
  57. ERR ErrUtilRegInit(void);
  58. ERR ErrUtilRegTerm(void);
  59. ERR ErrUtilRegReadConfig(void);
  60. extern HKEY hkeyHiveRoot;
  61. ERR ErrUtilRegOpenKeyEx( HKEY hkeyRoot, LPCTSTR lpszSubKey, PHKEY phkResult );
  62. ERR ErrUtilRegCloseKeyEx( HKEY hkey );
  63. ERR ErrUtilRegCreateKeyEx( HKEY hkeyRoot,
  64. LPCTSTR lpszSubKey,
  65. PHKEY phkResult,
  66. LPDWORD lpdwDisposition );
  67. ERR ErrUtilRegDeleteKeyEx( HKEY hkeyRoot, LPCTSTR lpszSubKey );
  68. ERR ErrUtilRegDeleteValueEx( HKEY hkey, LPTSTR lpszValue );
  69. ERR ErrUtilRegSetValueEx( HKEY hkey,
  70. LPCTSTR lpszValue,
  71. DWORD fdwType,
  72. CONST BYTE *lpbData,
  73. DWORD cbData );
  74. ERR ErrUtilRegQueryValueEx( HKEY hkey,
  75. LPTSTR lpszValue,
  76. LPDWORD lpdwType,
  77. LPBYTE *lplpbData );
  78. // DLL Support
  79. STATIC INLINE BOOL FUtilLoadLibrary(const char *pszLibrary, ULONG_PTR *phmod)
  80. {
  81. HANDLE hmod;
  82. hmod = LoadLibrary((LPTSTR) pszLibrary);
  83. // restore original error mode
  84. *phmod = (ULONG_PTR) hmod;
  85. return(hmod != NULL);
  86. }
  87. STATIC INLINE PFN PfnUtilGetProcAddress(ULONG_PTR hmod, unsigned ordinal)
  88. {
  89. return((PFN) GetProcAddress((HANDLE) hmod, MAKEINTRESOURCE(ordinal)));
  90. }
  91. STATIC INLINE void UtilFreeLibrary( unsigned hmod ) { FreeLibrary( LongToHandle(hmod)); }
  92. // Date and Time
  93. void UtilGetDateTime(DATESERIAL *pdt);
  94. void UtilGetDateTime2(_JET_DATETIME *pdt);
  95. char *SzUtilSerialToDate(JET_DATESERIAL dt);
  96. // File Operations
  97. typedef OVERLAPPED OLP;
  98. ERR ErrUtilCreateDirectory( char *szDirName );
  99. ERR ErrUtilRemoveDirectory( char *szDirName );
  100. ERR ErrUtilGetFileAttributes( CHAR *szFileName, BOOL *pfReadOnly );
  101. ERR ErrUtilFindFirstFile( CHAR *szFind, HANDLE *phandleFind, CHAR *szFound );
  102. ERR ErrUtilFindNextFile( HANDLE handleFind, CHAR *szFound );
  103. VOID UtilFindClose( HANDLE handleFind );
  104. BOOL FUtilFileExists( CHAR *szFilePathName );
  105. ERR ErrUtilOpenFile( char *szFileName, HANDLE *phf, ULONG ulFileSize, BOOL fReadOnly, BOOL fOverlapped );
  106. ERR ErrUtilOpenReadFile( char *szFileName, HANDLE *phf );
  107. ERR ErrUtilDeleteFile( char *szFileName );
  108. ERR ErrUtilNewSize( HANDLE hf, ULONG ulFileSize, ULONG ulFileSizeHigh, BOOL fOverlapped );
  109. ERR ErrUtilMove( char *szFrom, char *szTo );
  110. ERR ErrUtilCopy( char *szFrom, char *szTo, BOOL fFailIfExists );
  111. ERR ErrUtilReadFile( HANDLE hf, VOID *pvBuf, UINT cbBuf, UINT *pcbRead );
  112. ERR ErrUtilReadBlock( HANDLE hf, VOID *pvBuf, UINT cbBuf, UINT *pcbRead );
  113. ERR ErrUtilReadBlockOverlapped( HANDLE hf, VOID *pvBuf, UINT cbBuf,
  114. DWORD *pcbRead, OLP *polp );
  115. ERR ErrUtilReadBlockEx( HANDLE hf, VOID *pvBuf, UINT cbBuf, OLP *polp, VOID *pfnCompletion);
  116. ERR ErrUtilWriteBlock( HANDLE hf, VOID *pvBuf, UINT cbBuf, UINT *pcbWritten );
  117. ERR ErrUtilWriteBlockOverlapped( HANDLE hf, VOID *pvBuf, UINT cbBuf,
  118. DWORD *pcbWrite, OLP *polp );
  119. ERR ErrUtilWriteBlockEx( HANDLE hf, VOID *pvBuf, UINT cbBuf, OLP *polp, VOID *pfnCompletion);
  120. ERR ErrUtilGetOverlappedResult( HANDLE hf, OLP *polp, UINT *pcb,
  121. BOOL fWait );
  122. STATIC INLINE ERR ErrUtilCloseFile( HANDLE hf ) { return ErrUtilCloseHandle( hf ); }
  123. ERR ErrUtilReadShadowedHeader( CHAR *szFileName, BYTE *pbHeader, INT cbHeader );
  124. ERR ErrUtilWriteShadowedHeader( CHAR *szFileName, BYTE *pbHeader, INT cbHeader );
  125. #define ulChecksumMagicNumber 0x89abcdef
  126. ULONG UlUtilChecksum( const BYTE *pb, INT cb );
  127. //+api------------------------------------------------------
  128. //
  129. // VOID UtilChgFilePtr( HANDLE hf, LONG lRel, LONG *plRelHigh, ULONG ulRef, ULONG *pul )
  130. // =========================================================
  131. //
  132. // Changes file hf pointer to position lRef relative to position :
  133. //
  134. // wRef FILE_BEGIN file beginnging
  135. //
  136. //----------------------------------------------------------
  137. STATIC INLINE VOID UtilChgFilePtr( HANDLE hf, LONG lRel, LONG *plRelHigh, ULONG ulRef, ULONG *pul )
  138. {
  139. Assert( sizeof(HANDLE) == sizeof(HFILE) );
  140. *pul = SetFilePointer( (HANDLE)hf, lRel, plRelHigh, ulRef );
  141. Assert( ulRef != FILE_BEGIN || *pul == (ULONG)lRel );
  142. return;
  143. }
  144. STATIC INLINE ERR ErrUtilGetDiskFreeSpace( char *szRoot,
  145. ULONG *pSecPerClust,
  146. ULONG *pcbSec,
  147. ULONG *pcFreeClust,
  148. ULONG *pcTotClust )
  149. {
  150. if ( GetDiskFreeSpace( szRoot, pSecPerClust, pcbSec, pcFreeClust, pcTotClust ) )
  151. return JET_errSuccess;
  152. else
  153. return ErrERRCheck( JET_errDiskIO );
  154. }
  155. VOID UtilDebugBreak();
  156. // Physical Memory Allocation
  157. #if defined( DEBUG ) || defined( RFS2 )
  158. void *SAlloc( unsigned long );
  159. void OSSFree( void * );
  160. void *LAlloc( unsigned long, unsigned short );
  161. void OSLFree( void * );
  162. STATIC INLINE VOID SFree( void *pv ) { OSSFree( pv ); }
  163. STATIC INLINE VOID LFree( void *pv ) { OSLFree( pv ); }
  164. #else // !DEBUG && !RFS2
  165. #include <stdlib.h>
  166. STATIC INLINE VOID *SAlloc( ULONG cb ) { return GlobalAlloc( 0, cb ); }
  167. STATIC INLINE VOID SFree( VOID *pv ) { GlobalFree( pv ); }
  168. STATIC INLINE VOID *LAlloc( ULONG c, USHORT cb ) { return GlobalAlloc( 0, c * cb ); }
  169. STATIC INLINE VOID LFree( VOID *pv ) { GlobalFree( pv ); }
  170. #endif // DEBUG || RFS2
  171. // Virtual Memory Allocation
  172. #if defined( DEBUG ) || defined( RFS2 )
  173. VOID *PvUtilAlloc( ULONG dwSize );
  174. VOID *PvUtilCommit( VOID *pv, ULONG dwSize );
  175. VOID UtilFree( VOID *pv );
  176. VOID UtilDecommit( VOID *pv, ULONG dwSize );
  177. #else // !DEBUG && !RFS2
  178. STATIC INLINE VOID *PvUtilAlloc( ULONG dwSize )
  179. {
  180. return VirtualAlloc( NULL, dwSize, MEM_RESERVE, PAGE_READWRITE );
  181. }
  182. STATIC INLINE VOID *PvUtilCommit( VOID *pv, ULONG ulSize )
  183. {
  184. return VirtualAlloc( pv, ulSize, MEM_COMMIT, PAGE_READWRITE );
  185. }
  186. STATIC INLINE VOID UtilFree( VOID *pv ) { VirtualFree( pv, 0, MEM_RELEASE ); }
  187. STATIC INLINE VOID UtilDecommit( VOID *pv, ULONG dwSize ) { VirtualFree( pv, dwSize, MEM_DECOMMIT ); }
  188. #endif // DEBUG || RFS2
  189. VOID *PvUtilAllocAndCommit( ULONG dwSize );
  190. // Critical Sections
  191. #ifdef SPIN_LOCK
  192. void UtilEnterNestableCriticalSection(void *pv);
  193. void UtilLeaveNestableCriticalSection(void *pv);
  194. void UtilEnterCriticalSection(void *pv);
  195. void UtilLeaveCriticalSection(void *pv);
  196. ERR ErrUtilInitializeCriticalSection(void **ppv);
  197. void UtilDeleteCriticalSection(void *pv);
  198. #else // !SPIN_LOCK
  199. #ifdef DEBUG
  200. void UtilEnterNestableCriticalSection(void *pv);
  201. void UtilLeaveNestableCriticalSection(void *pv);
  202. void UtilEnterCriticalSection(void *pv);
  203. void UtilLeaveCriticalSection(void *pv);
  204. ERR ErrUtilInitializeCriticalSection(void **ppv);
  205. void UtilDeleteCriticalSection(void *pv);
  206. #else // !DEBUG
  207. STATIC INLINE ERR ErrUtilInitializeCriticalSection(void **ppv)
  208. {
  209. if ( !( *ppv = SAlloc( sizeof( CRITICAL_SECTION ) ) ) )
  210. return ErrERRCheck( JET_errOutOfMemory );
  211. InitializeCriticalSection( (LPCRITICAL_SECTION) *ppv );
  212. return JET_errSuccess;
  213. }
  214. STATIC INLINE VOID UtilDeleteCriticalSection(void *pv)
  215. {
  216. DeleteCriticalSection( (LPCRITICAL_SECTION) pv );
  217. SFree( pv );
  218. }
  219. STATIC INLINE VOID UtilEnterCriticalSection(void *pv)
  220. {
  221. EnterCriticalSection( (LPCRITICAL_SECTION) pv );
  222. }
  223. STATIC INLINE VOID UtilLeaveCriticalSection(void *pv)
  224. {
  225. LeaveCriticalSection( (LPCRITICAL_SECTION) pv );
  226. }
  227. STATIC INLINE VOID UtilEnterNestableCriticalSection( void *pv ) { UtilEnterCriticalSection( pv ); }
  228. STATIC INLINE VOID UtilLeaveNestableCriticalSection( void *pv ) { UtilLeaveCriticalSection( pv ); }
  229. #endif // !DEBUG
  230. #endif // !SPIN_LOCK
  231. #ifdef RETAIL
  232. #define UtilAssertCrit( pv ) 0
  233. #define UtilAssertNotInCrit( pv ) 0
  234. #define UtilHoldCriticalSection( pv ) 0
  235. #define UtilReleaseCriticalSection( pv ) 0
  236. #else // !RETAIL
  237. void UtilAssertCrit(void *pv);
  238. void UtilAssertNotInCrit(void *pv);
  239. void UtilHoldCriticalSection(void *pv);
  240. void UtilReleaseCriticalSection(void *pv);
  241. #endif // !RETAIL
  242. // Signals
  243. typedef HANDLE SIG;
  244. #define sigNil ( (SIG) 0 )
  245. STATIC INLINE ERR ErrUtilSignalCreate( void **ppv, const char *szSig )
  246. {
  247. *((SIG *) ppv) = CreateEvent( NULL, fTrue, fFalse, szSig );
  248. return ( *ppv == sigNil ) ? ErrERRCheck( JET_errOutOfMemory ) : JET_errSuccess;
  249. }
  250. STATIC INLINE ERR ErrUtilSignalCreateAutoReset( void **ppv, const char *szSig )
  251. {
  252. *((SIG *) ppv) = CreateEvent( NULL, fFalse, fFalse, szSig );
  253. return ( *ppv == sigNil ) ? ErrERRCheck( JET_errOutOfMemory ) : JET_errSuccess;
  254. }
  255. STATIC INLINE void UtilSignalReset( void *pv )
  256. {
  257. BOOL rc;
  258. rc = ResetEvent( (SIG) pv );
  259. Assert( rc != FALSE );
  260. }
  261. STATIC INLINE void UtilSignalSend( void *pv )
  262. {
  263. BOOL rc;
  264. rc = SetEvent( (SIG) pv );
  265. Assert( rc != FALSE );
  266. }
  267. STATIC INLINE DWORD UtilSignalWait( void *pv, long lTimeOut )
  268. {
  269. DWORD rc;
  270. UtilAssertNotInCrit( critJet );
  271. rc = WaitForSingleObject( (SIG) pv, lTimeOut < 0 ? -1L : lTimeOut );
  272. return rc;
  273. }
  274. STATIC INLINE DWORD UtilSignalWaitEx( void *pv, long lTimeOut, BOOL fAlertable )
  275. {
  276. DWORD rc;
  277. UtilAssertNotInCrit( critJet );
  278. rc = WaitForSingleObjectEx( (SIG) pv, lTimeOut < 0 ? -1L : lTimeOut, fAlertable );
  279. return rc;
  280. }
  281. STATIC INLINE void UtilMultipleSignalWait( int csig, void *pv, BOOL fWaitAll, long lTimeOut )
  282. {
  283. DWORD rc;
  284. UtilAssertNotInCrit( critJet );
  285. rc = WaitForMultipleObjects( csig, (SIG*) pv, fWaitAll, lTimeOut < 0 ? -1L : lTimeOut );
  286. }
  287. void UtilCloseSignal(void *pv);
  288. // Semaphore
  289. typedef HANDLE SEM;
  290. #define semNil ( (SEM) 0 )
  291. STATIC INLINE ERR ErrUtilSemaphoreCreate( SEM *psem, LONG lInitialCount, LONG lMaximumCount )
  292. {
  293. *psem = ( SEM ) CreateSemaphore( NULL, lInitialCount, lMaximumCount, NULL );
  294. return ( *psem == semNil ) ? ErrERRCheck( JET_errOutOfMemory ) : JET_errSuccess;
  295. }
  296. STATIC INLINE DWORD UtilSemaphoreWait( SEM sem, long lTimeOut )
  297. {
  298. DWORD rc;
  299. rc = WaitForSingleObject( (HANDLE) sem, lTimeOut < 0 ? -1L : lTimeOut );
  300. return rc;
  301. }
  302. STATIC INLINE DWORD UtilSemaphoreRelease( SEM sem, LONG cReleaseCount )
  303. {
  304. DWORD rc;
  305. rc = ReleaseSemaphore( (HANDLE) sem, cReleaseCount, NULL );
  306. Assert( rc != 0 );
  307. return rc;
  308. }
  309. void UtilCloseSemaphore(void *pv);
  310. // Threads and Processes
  311. #define cmsecSleepMax (60*1000)
  312. VOID UtilSleepEx( ULONG ulTime, BOOL fAlert );
  313. VOID UtilSleep( ULONG ulTime );
  314. ERR ErrUtilCreateThread( ULONG (*pulfn)(), ULONG cbStack, LONG lThreadPriority, HANDLE *phandle );
  315. ULONG UtilEndThread( HANDLE hThread, SIG sigEnd );
  316. #define lThreadPriorityNormal 0
  317. #define lThreadPriorityHigh 1
  318. void UtilSetThreadPriority( HANDLE hThread, LONG lThreadPriority );
  319. STATIC INLINE HANDLE UtilGetCurrentTask( VOID ) { return LongToHandle(GetCurrentProcessId()); }
  320. STATIC INLINE DWORD DwUtilGetCurrentThreadId( VOID ) { return GetCurrentThreadId(); }
  321. // text normalization
  322. ERR ErrUtilCheckLangid( LANGID *plangid );
  323. VOID UtilNormText( char *rgchText, INT cchText, BYTE *rgchNorm, INT cbNorm, INT *pbNorm );
  324. ERR ErrUtilNormText(
  325. const BYTE *pbField,
  326. INT cbField,
  327. INT cbKeyBufLeft,
  328. BYTE *pbSeg,
  329. INT *pcbSeg );
  330. #define BINARY_NAMES 1
  331. #ifdef BINARY_NAMES
  332. #define UtilCmpName( sz1, sz2 ) \
  333. ( _stricmp( sz1, sz2 ) )
  334. #define UtilStringCompare( pb1, cb1, pb2, cb2, sort, plResult ) \
  335. { \
  336. *plResult = strncmp( pb1, pb2, min( cb1, cb2 ) ); \
  337. if ( !*plResult ) \
  338. *plResult = cb1 > cb2; \
  339. }
  340. #else
  341. INT UtilCmpName( const char *sz1, const char *sz2 );
  342. VOID UtilStringCompare( char *pb1, unsigned long cb1,
  343. char *pb2, unsigned long cb2, unsigned long sort,
  344. long *plResult );
  345. #endif
  346. // Unicode Support
  347. ERR ErrUtilMapString(LANGID langid, BYTE *pbField, INT cbField, BYTE *rgbSeg,
  348. int cbBufLeft, int *cbSeg);
  349. ERR ErrUtilWideCharToMultiByte(LPCWSTR lpcwStr, LPSTR *lplpOutStr);
  350. // RFS functions
  351. #ifdef RFS2
  352. int UtilRFSAlloc( const char *szType, int Type );
  353. int UtilRFSLog(const char *szType,int fPermitscritted);
  354. void UtilRFSLogJETCall(const char *szFunc,ERR err,const char *szFile,unsigned Line);
  355. void UtilRFSLogJETErr(ERR err,const char *szLabel,const char *szFile,unsigned szLine);
  356. #endif /* RFS2 */
  357. /*
  358. ** UtilInterlockedIncrement and UtilInterlockedDecrement are wrapper functions
  359. ** to increment or decrement a value in a thread-safe manner.
  360. **
  361. ** Return values are:
  362. **
  363. ** > 0 if resulting value > 0
  364. ** = 0 if resulting value = 0
  365. ** < 0 if resulting value < 0
  366. **
  367. ** Note that the return value isn't necessary the resulting value; it may be
  368. ** different than the resulting value, but sign is guarenteed to be the same.
  369. */
  370. STATIC INLINE long UtilInterlockedIncrement( long *lpValue )
  371. {
  372. return InterlockedIncrement( lpValue );
  373. }
  374. STATIC INLINE long UtilInterlockedDecrement( long *lpValue )
  375. {
  376. return InterlockedDecrement( lpValue );
  377. }
  378. /*
  379. ** UtilInterlockedExchange is a wrapper function to assign a value to a
  380. ** variable in a thread-safe manner.
  381. **
  382. ** This function returns the prior value of the variable (before the
  383. ** assignment was done).
  384. */
  385. STATIC INLINE long UtilInterlockedExchange( long *lpValue1, long lValue2 )
  386. {
  387. return InterlockedExchange( lpValue1, lValue2 );
  388. }
  389. // Debug output
  390. #ifdef DEBUG
  391. extern void * critDBGPrint;
  392. VOID JET_API DBGFPrintF( char *sz );
  393. void VARARG DebugWriteString(BOOL fHeader, const char *szFormat, ...);
  394. #else
  395. #define DBGFPrintF( sz ) 0
  396. #endif
  397. void UtilPerfDumpStats(char *szText);
  398. // End Assert redirection
  399. #undef szAssertFilename
  400. #endif // _UTILW32_H