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.

258 lines
7.4 KiB

  1. #ifndef _MDMUtils_h
  2. #define _MDMUtils_h
  3. // File: MDMUtils.h
  4. // Author: Michael Marr (mikemarr)
  5. //
  6. // Description:
  7. // This header contains miscellaneous utility functions.
  8. //
  9. // History:
  10. // -@- 08/04/95 (mikemarr) - created
  11. // -@- 09/09/97 (mikemarr) - snarfed from d2d\d2dutils\src\mmutils.cpp
  12. // -@- 09/09/97 (mikemarr) - will only create code when in debug mode
  13. // -@- 11/12/97 (mikemarr) - added CopyDWORDAligned
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <stddef.h>
  17. #include <stdarg.h>
  18. #include <limits.h>
  19. #include <memory.h>
  20. #define nTMPBUFSIZE 1024
  21. extern char g_rgchTmpBuf[nTMPBUFSIZE];
  22. #if defined(_WINDOWS) || defined(WIN32)
  23. #ifndef _INC_WINDOWS
  24. #define WIN32_EXTRA_LEAN
  25. #define WIN32_LEAN_AND_MEAN
  26. #include <WINDOWS.H>
  27. #endif
  28. #endif
  29. #ifndef NULL
  30. #ifdef __cplusplus
  31. #define NULL 0
  32. #else
  33. #define NULL ((void *)0)
  34. #endif
  35. #endif
  36. #ifndef FALSE
  37. #define FALSE false
  38. #endif
  39. #ifndef TRUE
  40. #define TRUE true
  41. #endif
  42. #ifndef IN
  43. #define IN
  44. #endif
  45. #ifndef OUT
  46. #define OUT
  47. #endif
  48. #ifndef INOUT
  49. #define INOUT
  50. #endif
  51. #ifndef BOOL
  52. typedef int BOOL;
  53. typedef BOOL far *LPBOOL;
  54. #endif
  55. #ifndef BYTE
  56. typedef unsigned char BYTE;
  57. typedef BYTE far *LPBYTE;
  58. #endif
  59. #ifndef WORD
  60. typedef unsigned short WORD;
  61. typedef WORD far *LPWORD;
  62. #endif
  63. #ifndef DWORD
  64. typedef unsigned long DWORD;
  65. typedef DWORD far *LPDWORD;
  66. #endif
  67. #ifndef LPVOID
  68. typedef void far *LPVOID;
  69. #endif
  70. #define maskBYTE 0xFF
  71. #define maskWORD 0xFFFF
  72. #define maskDWORD 0xFFFFFFFF
  73. #define maxBYTE 0xFF
  74. #define maxWORD 0xFFFF
  75. #define maxDWORD 0xFFFFFFFF
  76. #define chSPC ' '
  77. #define chTAB ' '
  78. #define chEOL '\0'
  79. #define chNULL '\0'
  80. #define chLINEFEED 0x0D
  81. #define chCARRIAGERETURN 0x0A
  82. #ifndef max
  83. #define max(a,b) (((a) > (b)) ? (a) : (b))
  84. #endif
  85. #ifndef min
  86. #define min(a,b) (((a) < (b)) ? (a) : (b))
  87. #endif
  88. #define MACSTART do {
  89. #define MACEND } while(0)
  90. // Macro: ISWAP
  91. // This macro swaps two integer registers using 3 xor's.
  92. // This is unsafe because a & b are not guaranteed to get 2 regs.
  93. #define ISWAP(a,b) MACSTART (a)^=(b); (b)^=(a); (a)^=(b); MACEND
  94. // Macro: PSWAP
  95. // This macro swaps two pointers in place using 3 xor's.
  96. #define PSWAP(a,b,type) MACSTART \
  97. a = (type *)(int(a) ^ int(b)); \
  98. b = (type *)(int(a) ^ int(b)); \
  99. a = (type *)(int(a) ^ int(b)); \
  100. MACEND
  101. #define MMINITSTRUCT(__s) memset(&(__s), 0, sizeof(__s))
  102. #ifndef ZERO_DXSTRUCT
  103. #define ZERO_DXSTRUCT(__dxstruct) MACSTART \
  104. MMINITSTRUCT(__dxstruct); (__dxstruct).dwSize = sizeof(__dxstruct); MACEND
  105. #endif
  106. #ifndef INIT_DXSTRUCT
  107. #define INIT_DXSTRUCT(__dxstruct) MACSTART \
  108. MMINITSTRUCT(__dxstruct); (__dxstruct).dwSize = sizeof(__dxstruct); MACEND
  109. #endif
  110. #ifdef DBG
  111. #define CHECK_HR(__hr) MACSTART if (FAILED(__hr)) { printf("%s(%d): CHECK_HR failed (0x%X)\n", __FILE__, __LINE__, hr); goto e_Exit; } MACEND
  112. #else
  113. #define CHECK_HR(__hr) MACSTART if (FAILED(__hr)) goto e_Exit; MACEND
  114. #endif
  115. #define CHECK_MEM(__p) MACSTART if ((__p) == NULL) { hr = E_OUTOFMEMORY; goto e_Exit; } MACEND
  116. #define INRANGE(x, xmin, xmax) (((x) >= (xmin)) && ((x) <= (xmax)))
  117. #define INARRAY(x, xmax) (((x) >= 0) && ((x) < (xmax)))
  118. #define SETABS(x) MACSTART if ((x) < 0) (x) = -(x); MACEND
  119. #define SETMAX(dst, src1, src2) MACSTART if ((src1) < (src2)) (dst) = (src2); else (dst) = (src1); MACEND
  120. #define SETMIN(dst, src1, src2) MACSTART if ((src1) > (src2)) (dst) = (src2); else (dst) = (src1); MACEND
  121. #define UPDATEMAX(dst, src) MACSTART if ((src) > (dst)) (dst) = (src); MACEND
  122. #define UPDATEMIN(dst, src) MACSTART if ((src) < (dst)) (dst) = (src); MACEND
  123. #define UPDATEMINMAX(xmin, xmax, x) MACSTART if ((x) < (xmin)) (xmin) = (x); else if ((x) > (xmax)) (xmax) = (x); MACEND
  124. #define CLAMPMAX(x, xmax) MACSTART if ((x) > (xmax)) (x) = (xmax); MACEND
  125. #define CLAMPMIN(x, xmin) MACSTART if ((x) < (xmin)) (x) = (xmin); MACEND
  126. #define CLAMP(x, xmin, xmax) MACSTART \
  127. if ((x) > (xmax)) (x) = (xmax); \
  128. else if ((x) < (xmin)) (x) = (xmin); \
  129. MACEND
  130. //
  131. // BIT MANIPULATION: BitVector
  132. //
  133. // **Hungarian Prefix: bv
  134. typedef unsigned int BitVector;
  135. #define NUMBITS(Type) (sizeof(Type) << 3)
  136. // Macro: MASKRANGE
  137. // Create a bit mask in the specified range, where the lo value is inclusive, and the
  138. // hi value is exclusive.
  139. // For example, MASKRANGE(8, 16) == 0x0000FF00.
  140. #define MASKRANGE(lo, hi) \
  141. (((((BitVector) ~0) >> (lo)) << ((lo) + (NUMBITS(BitVector) - (hi)))) >> (NUMBITS(BitVector) - (hi)))
  142. #define SETBIT(bv, i) ((bv) |= (((BitVector) 1) << (i)))
  143. #define UNSETBIT(bv, i) ((bv) &= ~(((BitVector) 1) << (i)))
  144. #define SETRANGE(bv, lo, hi) ((bv) |= MASKRANGE(lo, hi))
  145. #define UNSETRANGE(bv, lo, hi) ((bv) &= ~MASKRANGE(lo, hi))
  146. #define SETFLAG(_dwFlags, _flag, _b) MACSTART if (_b) _dwFlags |= _flag; else _dwFlags &= ~_flag; MACEND
  147. //
  148. // DEBUG STUFF
  149. //
  150. #ifndef __AFX_H__
  151. #ifdef _DEBUG
  152. void _MMStall(const char *szExp, const char *szFile, int nLine);
  153. void _MMTrace(const char *szFmt, ...);
  154. #define MMASSERT(exp) (void)((exp) || (_MMStall(#exp, __FILE__, __LINE__),0))
  155. #define MMASSERT_VALID(exp) MMASSERT(exp)
  156. #define MMVERIFY(exp) MMASSERT(exp)
  157. #define MMDEBUG_ONLY(exp) (exp)
  158. #define MMTRACE ::_MMTrace
  159. #else
  160. // void _MMIgnore(const char *szFmt, ...) {}
  161. #define MMASSERT(exp) ((void)0)
  162. #define MMASSERT_VALID(exp) ((void)0)
  163. #define MMVERIFY(exp) ((void)(exp))
  164. #define MMDEBUG_ONLY(exp) ((void)0)
  165. #define MMTRACE 1 ? (void)0 : ::printf
  166. #endif
  167. #else
  168. #define MMASSERT(exp) ASSERT(exp)
  169. #define MMASSERT_VALID(exp) ASSERT_VALID(exp)
  170. #define MMVERIFY(exp) VERIFY(exp)
  171. #define MMDEBUG_ONLY(exp) DEBUG_ONLY(exp)
  172. #define MMTRACE TRACE
  173. #endif
  174. // Macro: MMRELEASE
  175. // Safe release for COM objects
  176. // ***this code should never change - there is stuff that relies on the pointer being
  177. // set to NULL after being released
  178. #ifndef MMRELEASE
  179. #define MMRELEASE(_p) MACSTART if ((_p) != NULL) {(_p)->Release(); (_p) = NULL;} MACEND
  180. #endif
  181. #define MMDELETE(__ptr) MACSTART delete (__ptr); (__ptr) = NULL; MACEND
  182. #define MMDELETERG(__ptr) MACSTART delete [] (__ptr); (__ptr) = NULL; MACEND
  183. #define MMSETREF(_pOld, _pNew) MACSTART if (_pOld) (_pOld)->Release(); if ((_pOld) = (_pNew)) (_pOld)->AddRef(); MACEND
  184. #define MAKE_USERERROR(code) MAKE_HRESULT(1,FACILITY_ITF,code)
  185. #define E_NOTINITIALIZED MAKE_USERERROR(0xFFFC)
  186. #define E_ALREADYINITIALIZED MAKE_USERERROR(0xFFFB)
  187. #define E_NOTFOUND MAKE_USERERROR(0xFFFA)
  188. #define E_INSUFFICIENTDATA MAKE_USERERROR(0xFFF9)
  189. extern char g_szEOFMessage[];
  190. // Macro for memory mapped file stuff
  191. #define CHECKEOF(__pFilePos, __pFileLimit) MACSTART \
  192. if ((__pFilePos) >= (__pFileLimit)) { \
  193. MMTRACE(g_szEOFMessage); \
  194. return E_UNEXPECTED; \
  195. } MACEND
  196. void ZeroDWORDAligned(LPDWORD pdw, DWORD cEntries);
  197. void CopyDWORDAligned(DWORD *pdwDst, const DWORD *pdwSrc, DWORD cEntries);
  198. inline void
  199. ZeroPointers(void **ppv, DWORD cEntries)
  200. {
  201. // use a Duff-Marr machine
  202. void **ppvLimit = ppv;
  203. ppv += (cEntries & ~0x7);
  204. switch (cEntries & 0x7) {
  205. do {
  206. ppv -= 8;
  207. ppv[7] = NULL;
  208. case 7: ppv[6] = NULL;
  209. case 6: ppv[5] = NULL;
  210. case 5: ppv[4] = NULL;
  211. case 4: ppv[3] = NULL;
  212. case 3: ppv[2] = NULL;
  213. case 2: ppv[1] = NULL;
  214. case 1: ppv[0] = NULL;
  215. case 0: ;
  216. } while (ppv != ppvLimit);
  217. }
  218. }
  219. DWORD GetClosestMultipleOf4(DWORD n, bool bGreater);
  220. DWORD GetClosestPowerOf2(DWORD n, bool bGreater);
  221. #endif