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.

95 lines
2.0 KiB

  1. // File: MMUtils.cpp
  2. // Author: Michael Marr (mikemarr)
  3. //
  4. // History:
  5. // -@- 04/12/96 (mikemarr) - created
  6. // -@- 11/07/96 (mikemarr) - combined debug stuff
  7. // -@- 09/09/97 (mikemarr) - snarfed from d2d\d2dutils\src\mmutils.cpp
  8. // -@- 09/09/97 (mikemarr) - will only create code when in debug mode
  9. // -@- 11/12/97 (mikemarr) - added CopyDWORDAligned
  10. #include "stdafx.h"
  11. #ifndef _MDMUtils_h
  12. #include "MDMUtils.h"
  13. #endif
  14. char g_rgchTmpBuf[nTMPBUFSIZE];
  15. char g_szEOFMessage[] = "unexpected EOF\n";
  16. void
  17. ZeroDWORDAligned(LPDWORD pdw, DWORD cEntries)
  18. {
  19. // verify alignment
  20. MMASSERT(pdw && ((DWORD(pdw) & 0x3) == 0));
  21. LPDWORD pdwLimit = pdw + cEntries;
  22. // REVIEW: use Duff-Marr machine
  23. for (; pdw != pdwLimit; *pdw++ = 0);
  24. }
  25. void
  26. CopyDWORDAligned(DWORD *pdwDst, const DWORD *pdwSrc, DWORD cEntries)
  27. {
  28. // verify alignment
  29. MMASSERT(pdwSrc && pdwDst && ((DWORD(pdwSrc) & 0x3) == 0));
  30. LPDWORD pdwLimit = pdwDst + cEntries;
  31. // REVIEW: use Duff-Marr machine
  32. for (; pdwDst != pdwLimit; *pdwDst++ = *pdwSrc++);
  33. }
  34. DWORD
  35. GetClosestMultipleOf4(DWORD n, bool bGreater)
  36. {
  37. return (n + bGreater * 3) & ~3;
  38. }
  39. DWORD
  40. GetClosestPowerOf2(DWORD n, bool bGreater)
  41. {
  42. DWORD i = 0;
  43. for (n >>= 1; n != 0; i++) {
  44. n >>= 1;
  45. }
  46. i += (bGreater && ((n & ~(1 << i)) != 0));
  47. return (1 << i);
  48. }
  49. //
  50. // Debug Stuff
  51. //
  52. #ifdef _DEBUG
  53. void _MMStall(const char *szExp, const char *szFile, int nLine) {
  54. sprintf(g_rgchTmpBuf, "error: (%s) in %s at line %d\n", szExp, szFile, nLine);
  55. #ifdef _WINDOWS
  56. OutputDebugString(g_rgchTmpBuf);
  57. #endif
  58. fprintf(stderr, "%s", g_rgchTmpBuf);
  59. // hardcode breakpoint
  60. #if defined(_DEBUG) && defined(_X86_)
  61. _asm { int 3 };
  62. #else
  63. exit(1);
  64. #endif
  65. }
  66. void _MMTrace(const char *szFmt, ...)
  67. {
  68. #ifndef _NOT_X86
  69. _vsnprintf(g_rgchTmpBuf, nTMPBUFSIZE - 1, szFmt, (va_list) (&szFmt+1));
  70. #if defined(_WINDOWS) && defined(_DEBUG)
  71. OutputDebugString(g_rgchTmpBuf);
  72. #else
  73. fprintf(stderr, "%s", g_rgchTmpBuf);
  74. #endif
  75. #endif // _NOT_X86
  76. }
  77. #endif // _DEBUG