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.

365 lines
11 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. DWORD DNGetRandomNumber();
  109. //
  110. // Interlocked functions (not actually interlocked when DPNBUILD_ONLYONETHREAD)
  111. //
  112. #ifdef DPNBUILD_ONLYONETHREAD
  113. inline LONG DNInterlockedIncrement( IN OUT LONG volatile *Addend )
  114. {
  115. return ++(*Addend);
  116. }
  117. inline LONG DNInterlockedDecrement( IN OUT LONG volatile *Addend )
  118. {
  119. return --(*Addend);
  120. }
  121. inline LONG DNInterlockedExchange( IN OUT LONG volatile *Target, IN LONG Value )
  122. {
  123. LONG Previous;
  124. Previous = *Target;
  125. *Target = Value;
  126. return Previous;
  127. }
  128. inline LONG DNInterlockedExchangeAdd( IN OUT LONG volatile *Addend, IN LONG Value )
  129. {
  130. LONG Previous;
  131. Previous = *Addend;
  132. *Addend = Previous + Value;
  133. return Previous;
  134. }
  135. inline LONG DNInterlockedCompareExchange( IN OUT LONG volatile *Destination, IN LONG Exchange, IN LONG Comperand )
  136. {
  137. LONG Previous;
  138. Previous = *Destination;
  139. if (Previous == Comperand)
  140. {
  141. *Destination = Exchange;
  142. }
  143. return Previous;
  144. }
  145. inline PVOID DNInterlockedCompareExchangePointer( IN OUT PVOID volatile *Destination, IN PVOID Exchange, IN PVOID Comperand )
  146. {
  147. PVOID Previous;
  148. Previous = *Destination;
  149. if (Previous == Comperand)
  150. {
  151. *Destination = Exchange;
  152. }
  153. return Previous;
  154. }
  155. inline PVOID DNInterlockedExchangePointer( IN OUT PVOID volatile *Target, IN PVOID Value )
  156. {
  157. PVOID Previous;
  158. Previous = *Target;
  159. *Target = Value;
  160. return Previous;
  161. }
  162. #else // ! DPNBUILD_ONLYONETHREAD
  163. /*
  164. #ifdef WINCE
  165. #if defined(_ARM_)
  166. #define InterlockedExchangeAdd \
  167. ((long (*)(long *target, long increment))(PUserKData+0x3C0))
  168. #elif defined(_X86_)
  169. LONG WINAPI InterlockedExchangeAdd( LPLONG Addend, LONG Increment );
  170. #else
  171. #error("Unknown platform")
  172. #endif // Platform
  173. #endif // WINCE
  174. */
  175. #define DNInterlockedIncrement( Addend ) InterlockedIncrement( Addend )
  176. #define DNInterlockedDecrement( Addend ) InterlockedDecrement( Addend )
  177. #define DNInterlockedExchange( Target, Value ) InterlockedExchange( Target, Value )
  178. #define DNInterlockedExchangeAdd( Target, Value ) InterlockedExchangeAdd( Target, Value )
  179. #ifdef WINCE
  180. // NOTE: InterlockedTestExchange params 2 and 3 reversed intentionally, CE is that way
  181. #define DNInterlockedCompareExchange( Destination, Exchange, Comperand ) InterlockedTestExchange( Destination, Comperand, Exchange )
  182. #define DNInterlockedCompareExchangePointer( Destination, Exchange, Comperand ) (PVOID) (DNInterlockedCompareExchange( (LPLONG) Destination, (LONG) Exchange, (LONG) Comperand ))
  183. #define DNInterlockedExchangePointer( Target, Value ) (PVOID) (DNInterlockedExchange( (LPLONG) (Target), (LPLONG) (Value) ))
  184. #else // ! WINCE
  185. #define DNInterlockedCompareExchange( Destination, Exchange, Comperand ) InterlockedCompareExchange( Destination, Exchange, Comperand )
  186. #define DNInterlockedCompareExchangePointer( Destination, Exchange, Comperand ) InterlockedCompareExchangePointer( Destination, Exchange, Comperand )
  187. #define DNInterlockedExchangePointer( Target, Value ) InterlockedExchangePointer( Target, Value )
  188. #endif // WINCE
  189. #endif // ! DPNBUILD_ONLYONETHREAD
  190. // Special initialize to set spin count, avoid out-of-memory exceptions at Enter/Leave
  191. BOOL DNOSInitializeCriticalSection( CRITICAL_SECTION* pCriticalSection );
  192. #ifdef WINNT
  193. #define GLOBALIZE_STR _T("Global\\")
  194. #else
  195. #define GLOBALIZE_STR _T("")
  196. #endif // WINNT
  197. #if defined(WINCE) && !defined(WINCE_ON_DESKTOP)
  198. #define _TWINCE(x) __T(x)
  199. #else
  200. #define _TWINCE(x) x
  201. #endif // WINCE
  202. //
  203. // Memory functions
  204. //
  205. #ifdef DPNBUILD_LIBINTERFACE
  206. #define new __wont_compile_dont_use_new_operator__
  207. #define delete __wont_compile_dont_use_delete_operator__
  208. #else // ! DPNBUILD_LIBINTERFACE
  209. //**********************************************************************
  210. // ------------------------------
  211. // operator new - allocate memory for a C++ class
  212. //
  213. // Entry: Size of memory to allocate
  214. //
  215. // Exit: Pointer to memory
  216. // NULL = no memory available
  217. //
  218. // Notes: This function is for classes only and will ASSERT on zero sized
  219. // allocations! This function also doesn't do the whole proper class
  220. // thing of checking for replacement 'new handlers' and will not throw
  221. // an exception if allocation fails.
  222. // ------------------------------
  223. inline void* __cdecl operator new( size_t size )
  224. {
  225. return DNMalloc( size );
  226. }
  227. //**********************************************************************
  228. //**********************************************************************
  229. // ------------------------------
  230. // operator delete - deallocate memory for a C++ class
  231. //
  232. // Entry: Pointer to memory
  233. //
  234. // Exit: Nothing
  235. //
  236. // Notes: This function is for classes only and will ASSERT on NULL frees!
  237. // ------------------------------
  238. inline void __cdecl operator delete( void *pData )
  239. {
  240. //
  241. // Voice and lobby currently try allocating 0 byte buffers, can't disable this check yet.
  242. //
  243. if( pData == NULL )
  244. return;
  245. DNFree( pData );
  246. }
  247. //**********************************************************************
  248. #endif // ! DPNBUILD_LIBINTERFACE
  249. #ifdef WINCE
  250. #ifdef DBG
  251. UINT DNGetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
  252. #endif // DBG
  253. #ifndef WINCE_ON_DESKTOP
  254. HANDLE WINAPI OpenEvent(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  255. HANDLE WINAPI OpenFileMapping(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  256. HANDLE WINAPI OpenMutex(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName);
  257. #endif // !WINCE_ON_DESKTOP
  258. #define WaitForSingleObjectEx(handle, time, fAlertable) WaitForSingleObject(handle, time)
  259. #define WaitForMultipleObjectsEx(count, handles, waitall, time, fAlertable) WaitForMultipleObjects(count, handles, waitall, time)
  260. #ifndef WINCE_ON_DESKTOP
  261. #define GetWindowLongPtr(a, b) GetWindowLong(a, b)
  262. #define GWLP_USERDATA GWL_USERDATA
  263. #define SetWindowLongPtr(a, b, c) SetWindowLong(a, b, c)
  264. #endif // WINCE_ON_DESKTOP
  265. #define SleepEx(a, b) Sleep(a)
  266. #ifndef MUTEX_ALL_ACCESS
  267. #define MUTEX_ALL_ACCESS 0
  268. #endif // MUTEX_ALL_ACCESS
  269. #ifndef NORMAL_PRIORITY_CLASS
  270. #define NORMAL_PRIORITY_CLASS 0
  271. #endif // NORMAL_PRIORITY_CLASS
  272. #else // ! WINCE
  273. #ifdef DBG
  274. #if ((defined(_XBOX)) && (! defined(XBOX_ON_DESKTOP)))
  275. UINT DNGetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault);
  276. #else // ! _XBOX or XBOX_ON_DESKTOP
  277. #define DNGetProfileInt(lpszSection, lpszEntry, nDefault) GetProfileInt(lpszSection, lpszEntry, nDefault)
  278. #endif// ! _XBOX or XBOX_ON_DESKTOP
  279. #endif // DBG
  280. #endif // ! WINCE
  281. #if ((defined(WINCE)) || (defined(DPNBUILD_LIBINTERFACE)))
  282. HRESULT DNCoCreateGuid(GUID* pguid);
  283. #else // ! WINCE and ! DPNBUILD_LIBINTERFACE
  284. #define DNCoCreateGuid CoCreateGuid
  285. #endif // ! WINCE and ! DPNBUILD_LIBINTERFACE
  286. #ifdef _XBOX
  287. #define swprintf wsprintfW
  288. #else // ! _XBOX
  289. #ifdef WINCE
  290. static inline FARPROC DNGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  291. { return GetProcAddressA(hModule, lpProcName); };
  292. #else
  293. static inline FARPROC DNGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  294. { return GetProcAddress(hModule, lpProcName); };
  295. #endif
  296. #endif // _XBOX
  297. #endif // __OSIND_H__