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.

398 lines
12 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1999-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: OSInd.h
  6. * Content: OS indirection functions to abstract OS specific items.
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/12/1999 jtk Created
  12. * 10/16/2001 vanceo Added AssertNoCriticalSectionsTakenByThisThread capability
  13. *
  14. ***************************************************************************/
  15. #ifndef __OSIND_H__
  16. #define __OSIND_H__
  17. #include "CallStack.h"
  18. #include "ClassBilink.h"
  19. #include "HandleTracking.h"
  20. #include "CritsecTracking.h"
  21. #include "MemoryTracking.h"
  22. //**********************************************************************
  23. // Constant definitions
  24. //**********************************************************************
  25. #define GUID_STRING_LEN 39
  26. //**********************************************************************
  27. // Macro definitions
  28. //**********************************************************************
  29. #ifndef OFFSETOF
  30. #define OFFSETOF(s,m) ( ( INT_PTR ) ( ( PVOID ) &( ( (s*) 0 )->m ) ) )
  31. #endif // OFFSETOF
  32. //returns a pointer to a container structure given an internal member
  33. //con_type is the container type, mem_name is the member name and mem_ptr is the
  34. //pointer to the member
  35. #ifndef CONTAINEROF
  36. #define CONTAINEROF(con_type,mem_name,mem_ptr) ((con_type * ) (((char * ) mem_ptr)-\
  37. ( ( int ) ( ( void * ) &( ( (con_type*) 0 )->mem_name ) ) )));
  38. #endif // CONTAINEROF
  39. #ifndef LENGTHOF
  40. #define LENGTHOF( arg ) ( sizeof( arg ) / sizeof( arg[ 0 ] ) )
  41. #endif // OFFSETOF
  42. #ifndef _MIN
  43. #define _MIN(a, b) ((a) < (b) ? (a) : (b))
  44. #endif // _MIN
  45. #ifndef _MAX
  46. #define _MAX(a, b) ((a) > (b) ? (a) : (b))
  47. #endif // _MAX
  48. //**********************************************************************
  49. // Structure definitions
  50. //**********************************************************************
  51. //**********************************************************************
  52. // Variable definitions
  53. //**********************************************************************
  54. //**********************************************************************
  55. // Function prototypes
  56. //**********************************************************************
  57. //
  58. // initialization functions
  59. //
  60. BOOL DNOSIndirectionInit( DWORD_PTR dwpMaxMemUsage );
  61. void DNOSIndirectionDeinit( void );
  62. #ifndef DPNBUILD_NOPARAMVAL
  63. extern BOOL IsValidStringA( const CHAR * const swzString );
  64. #define DNVALID_STRING_A(a) IsValidStringA(a)
  65. extern BOOL IsValidStringW( const WCHAR * const szString );
  66. #define DNVALID_STRING_W(a) IsValidStringW(a)
  67. #define DNVALID_WRITEPTR(a,b) (!IsBadWritePtr(a,b))
  68. #define DNVALID_READPTR(a,b) (!IsBadReadPtr(a,b))
  69. #endif // ! DPNBUILD_NOPARAMVAL
  70. //
  71. // Function to get OS version. Supported returns:
  72. // VER_PLATFORM_WIN32_WINDOWS - Win9x
  73. // VER_PLATFORM_WIN32_NT - WinNT
  74. // VER_PLATFORM_WIN32s - Win32s on Win3.1
  75. // VER_PLATFORM_WIN32_CE - WinCE
  76. //
  77. #if ((! defined(WINCE)) && (! defined(_XBOX)))
  78. UINT_PTR DNGetOSType( void );
  79. #endif // ! WINCE and ! _XBOX
  80. struct in_addr;
  81. typedef struct in_addr IN_ADDR;
  82. void DNinet_ntow( IN_ADDR sin, WCHAR* pwsz );
  83. #ifdef WINNT
  84. BOOL DNOSIsXPOrGreater( void );
  85. #endif // WINNT
  86. #ifndef DPNBUILD_NOSERIALSP
  87. // Used only by serial provider
  88. HINSTANCE DNGetApplicationInstance( void );
  89. #endif // ! DPNBUILD_NOSERIALSP
  90. #ifdef WINNT
  91. PSECURITY_ATTRIBUTES DNGetNullDacl();
  92. #else
  93. #define DNGetNullDacl() 0
  94. #endif // WINNT
  95. #ifndef VER_PLATFORM_WIN32_CE
  96. #define VER_PLATFORM_WIN32_CE 3
  97. #endif // VER_PLATFORM_WIN32_CE
  98. #if ((defined(WINCE)) || (defined(_XBOX)))
  99. #define IsUnicodePlatform TRUE
  100. #else // ! WINCE and ! _XBOX
  101. #define IsUnicodePlatform (DNGetOSType() == VER_PLATFORM_WIN32_NT || DNGetOSType() == VER_PLATFORM_WIN32_CE)
  102. #endif // ! WINCE and ! _XBOX
  103. #ifdef WINCE
  104. #define GETTIMESTAMP() GetTickCount()
  105. #else
  106. #define GETTIMESTAMP() timeGetTime()
  107. #endif // WINCE
  108. //return a quick to generate but not particularly random number
  109. inline DWORD DNGetFastRandomNumber()
  110. {
  111. return (rand() | (rand() << 16));
  112. }
  113. #ifndef _XBOX
  114. extern HCRYPTPROV g_hCryptProv;
  115. //fill out an arbitary length buffer with good quality random data
  116. inline void DNGetGoodRandomData(void * pvData, DWORD dwNumBytes)
  117. {
  118. CryptGenRandom(g_hCryptProv, dwNumBytes, (BYTE * ) pvData);
  119. }
  120. #else
  121. //fill out an arbitary length buffer with good quality random data
  122. inline void DNGetGoodRandomData(void * pvData, DWORD dwNumBytes)
  123. {
  124. XNetRandom((BYTE * ) pvData, dwNumBytes);
  125. }
  126. #endif //!_XBOX
  127. //return a highly random number (suitable for crypto and key protocols)
  128. inline DWORD DNGetGoodRandomNumber()
  129. {
  130. DWORD dwRetVal;
  131. DNGetGoodRandomData(&dwRetVal, sizeof(dwRetVal));
  132. return dwRetVal;
  133. }
  134. //
  135. // Interlocked functions (not actually interlocked when DPNBUILD_ONLYONETHREAD)
  136. //
  137. #ifdef DPNBUILD_ONLYONETHREAD
  138. inline LONG DNInterlockedIncrement( IN OUT LONG volatile *Addend )
  139. {
  140. return ++(*Addend);
  141. }
  142. inline LONG DNInterlockedDecrement( IN OUT LONG volatile *Addend )
  143. {
  144. return --(*Addend);
  145. }
  146. inline LONG DNInterlockedExchange( IN OUT LONG volatile *Target, IN LONG Value )
  147. {
  148. LONG Previous;
  149. Previous = *Target;
  150. *Target = Value;
  151. return Previous;
  152. }
  153. inline LONG DNInterlockedExchangeAdd( IN OUT LONG volatile *Addend, IN LONG Value )
  154. {
  155. LONG Previous;
  156. Previous = *Addend;
  157. *Addend = Previous + Value;
  158. return Previous;
  159. }
  160. inline LONG DNInterlockedCompareExchange( IN OUT LONG volatile *Destination, IN LONG Exchange, IN LONG Comperand )
  161. {
  162. LONG Previous;
  163. Previous = *Destination;
  164. if (Previous == Comperand)
  165. {
  166. *Destination = Exchange;
  167. }
  168. return Previous;
  169. }
  170. inline PVOID DNInterlockedCompareExchangePointer( IN OUT PVOID volatile *Destination, IN PVOID Exchange, IN PVOID Comperand )
  171. {
  172. PVOID Previous;
  173. Previous = *Destination;
  174. if (Previous == Comperand)
  175. {
  176. *Destination = Exchange;
  177. }
  178. return Previous;
  179. }
  180. inline PVOID DNInterlockedExchangePointer( IN OUT PVOID volatile *Target, IN PVOID Value )
  181. {
  182. PVOID Previous;
  183. Previous = *Target;
  184. *Target = Value;
  185. return Previous;
  186. }
  187. #else // ! DPNBUILD_ONLYONETHREAD
  188. /*
  189. #ifdef WINCE
  190. #if defined(_ARM_)
  191. #define InterlockedExchangeAdd \
  192. ((long (*)(long *target, long increment))(PUserKData+0x3C0))
  193. #elif defined(_X86_)
  194. LONG WINAPI InterlockedExchangeAdd( LPLONG Addend, LONG Increment );
  195. #else
  196. #error("Unknown platform")
  197. #endif // Platform
  198. #endif // WINCE
  199. */
  200. #define DNInterlockedIncrement( Addend ) InterlockedIncrement( Addend )
  201. #define DNInterlockedDecrement( Addend ) InterlockedDecrement( Addend )
  202. #define DNInterlockedExchange( Target, Value ) InterlockedExchange( Target, Value )
  203. #define DNInterlockedExchangeAdd( Target, Value ) InterlockedExchangeAdd( Target, Value )
  204. #ifdef WINCE
  205. // NOTE: InterlockedTestExchange params 2 and 3 reversed intentionally, CE is that way
  206. #define DNInterlockedCompareExchange( Destination, Exchange, Comperand ) InterlockedTestExchange( Destination, Comperand, Exchange )
  207. #define DNInterlockedCompareExchangePointer( Destination, Exchange, Comperand ) (PVOID) (DNInterlockedCompareExchange( (LPLONG) Destination, (LONG) Exchange, (LONG) Comperand ))
  208. #define DNInterlockedExchangePointer( Target, Value ) (PVOID) (DNInterlockedExchange( (LPLONG) (Target), (LPLONG) (Value) ))
  209. #else // ! WINCE
  210. #define DNInterlockedCompareExchange( Destination, Exchange, Comperand ) InterlockedCompareExchange( Destination, Exchange, Comperand )
  211. #define DNInterlockedCompareExchangePointer( Destination, Exchange, Comperand ) InterlockedCompareExchangePointer( Destination, Exchange, Comperand )
  212. #define DNInterlockedExchangePointer( Target, Value ) InterlockedExchangePointer( Target, Value )
  213. #endif // WINCE
  214. #endif // ! DPNBUILD_ONLYONETHREAD
  215. // Special initialize to set spin count, avoid out-of-memory exceptions at Enter/Leave
  216. BOOL DNOSInitializeCriticalSection( CRITICAL_SECTION* pCriticalSection );
  217. #ifdef WINNT
  218. #define GLOBALIZE_STR _T("Global\\")
  219. #else
  220. #define GLOBALIZE_STR _T("")
  221. #endif // WINNT
  222. #if defined(WINCE) && !defined(WINCE_ON_DESKTOP)
  223. #define _TWINCE(x) __T(x)
  224. #else
  225. #define _TWINCE(x) x
  226. #endif // WINCE
  227. //
  228. // Memory functions
  229. //
  230. #ifdef DPNBUILD_LIBINTERFACE
  231. #define new __wont_compile_dont_use_new_operator__
  232. #define delete __wont_compile_dont_use_delete_operator__
  233. #else // ! DPNBUILD_LIBINTERFACE
  234. //**********************************************************************
  235. // ------------------------------
  236. // operator new - allocate memory for a C++ class
  237. //
  238. // Entry: Size of memory to allocate
  239. //
  240. // Exit: Pointer to memory
  241. // NULL = no memory available
  242. //
  243. // Notes: This function is for classes only and will ASSERT on zero sized
  244. // allocations! This function also doesn't do the whole proper class
  245. // thing of checking for replacement 'new handlers' and will not throw
  246. // an exception if allocation fails.
  247. // ------------------------------
  248. inline void* __cdecl operator new( size_t size )
  249. {
  250. return DNMalloc( size );
  251. }
  252. //**********************************************************************
  253. //**********************************************************************
  254. // ------------------------------
  255. // operator delete - deallocate memory for a C++ class
  256. //
  257. // Entry: Pointer to memory
  258. //
  259. // Exit: Nothing
  260. //
  261. // Notes: This function is for classes only and will ASSERT on NULL frees!
  262. // ------------------------------
  263. inline void __cdecl operator delete( void *pData )
  264. {
  265. //
  266. // Voice and lobby currently try allocating 0 byte buffers, can't disable this check yet.
  267. //
  268. if( pData == NULL )
  269. return;
  270. DNFree( pData );
  271. }
  272. //**********************************************************************
  273. #endif // ! DPNBUILD_LIBINTERFACE
  274. #ifdef WINCE
  275. #ifdef DBG
  276. UINT DNGetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
  277. #endif // DBG
  278. #ifndef WINCE_ON_DESKTOP
  279. HANDLE WINAPI OpenEvent(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  280. HANDLE WINAPI OpenFileMapping(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  281. HANDLE WINAPI OpenMutex(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  282. #endif // !WINCE_ON_DESKTOP
  283. #define WaitForSingleObjectEx(handle, time, fAlertable) WaitForSingleObject(handle, time)
  284. #define WaitForMultipleObjectsEx(count, handles, waitall, time, fAlertable) WaitForMultipleObjects(count, handles, waitall, time)
  285. #ifndef WINCE_ON_DESKTOP
  286. #define GetWindowLongPtr(a, b) GetWindowLong(a, b)
  287. #define GWLP_USERDATA GWL_USERDATA
  288. #define SetWindowLongPtr(a, b, c) SetWindowLong(a, b, c)
  289. #endif // WINCE_ON_DESKTOP
  290. #define SleepEx(a, b) Sleep(a)
  291. #ifndef MUTEX_ALL_ACCESS
  292. #define MUTEX_ALL_ACCESS 0
  293. #endif // MUTEX_ALL_ACCESS
  294. #ifndef NORMAL_PRIORITY_CLASS
  295. #define NORMAL_PRIORITY_CLASS 0
  296. #endif // NORMAL_PRIORITY_CLASS
  297. #else // ! WINCE
  298. #ifdef DBG
  299. #if ((defined(_XBOX)) && (! defined(XBOX_ON_DESKTOP)))
  300. UINT DNGetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
  301. #else // ! _XBOX or XBOX_ON_DESKTOP
  302. #define DNGetProfileInt(lpszSection, lpszEntry, nDefault) GetProfileInt(lpszSection, lpszEntry, nDefault)
  303. #endif// ! _XBOX or XBOX_ON_DESKTOP
  304. #endif // DBG
  305. #endif // ! WINCE
  306. #if ((defined(WINCE)) || (defined(DPNBUILD_LIBINTERFACE)))
  307. HRESULT DNCoCreateGuid(GUID* pguid);
  308. #else // ! WINCE and ! DPNBUILD_LIBINTERFACE
  309. #define DNCoCreateGuid CoCreateGuid
  310. #endif // ! WINCE and ! DPNBUILD_LIBINTERFACE
  311. #ifdef _XBOX
  312. #define swprintf wsprintfW
  313. #else // ! _XBOX
  314. #ifdef WINCE
  315. static inline FARPROC DNGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  316. { return GetProcAddressA(hModule, lpProcName); };
  317. #else
  318. static inline FARPROC DNGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  319. { return GetProcAddress(hModule, lpProcName); };
  320. #endif
  321. #endif // _XBOX
  322. #endif // __OSIND_H__