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.

108 lines
2.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1998
  6. //
  7. // File: ncutil.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #pragma once
  11. #define NOTHROW
  12. inline void AddRefObj(IUnknown *punk)
  13. {
  14. if (punk)
  15. punk->AddRef();
  16. }
  17. inline void ReleaseObj(IUnknown * punk)
  18. {
  19. if (punk)
  20. punk->Release();
  21. }
  22. #define Assert(x) assert(x)
  23. #define AssertSz(x,sz) assert(x)
  24. #define celems(rgx) (sizeof(rgx) / sizeof(*rgx))
  25. #define TraceTag(a,b,c)
  26. #define TraceErrorOptional(a,b,c)
  27. void TraceError(LPCSTR pszString, HRESULT hr);
  28. void TraceResult(LPCSTR pszString, HRESULT hr);
  29. void TraceSz(LPCSTR pszString);
  30. /*---------------------------------------------------------------------------
  31. Class: RtrCriticalSection
  32. This class is used to support entering/leaving of critical sections.
  33. Put this class at the top of a function that you want protected.
  34. ---------------------------------------------------------------------------*/
  35. class RtrCriticalSection
  36. {
  37. public:
  38. RtrCriticalSection(CRITICAL_SECTION *pCritSec)
  39. : m_pCritSec(pCritSec)
  40. {
  41. // IfDebug(m_cEnter=0;)
  42. // Assert(m_pCritSec);
  43. Enter();
  44. }
  45. ~RtrCriticalSection()
  46. {
  47. Detach();
  48. }
  49. void Enter()
  50. {
  51. if (m_pCritSec)
  52. {
  53. // IfDebug(m_cEnter++;)
  54. EnterCriticalSection(m_pCritSec);
  55. // AssertSz(m_cEnter==1, "EnterCriticalSection called too much!");
  56. }
  57. }
  58. BOOL TryToEnter()
  59. {
  60. if (m_pCritSec)
  61. return TryEnterCriticalSection(m_pCritSec);
  62. return TRUE;
  63. }
  64. void Leave()
  65. {
  66. if (m_pCritSec)
  67. {
  68. // IfDebug(m_cEnter--;)
  69. LeaveCriticalSection(m_pCritSec);
  70. // Assert(m_cEnter==0);
  71. }
  72. }
  73. void Detach()
  74. {
  75. Leave();
  76. m_pCritSec = NULL;
  77. }
  78. private:
  79. CRITICAL_SECTION * m_pCritSec;
  80. // IfDebug(int m_cEnter;)
  81. };
  82. inline LPWSTR StrDupW(LPCWSTR pswz)
  83. {
  84. LPWSTR pswzcpy = new WCHAR[lstrlenW(pswz)+1];
  85. return lstrcpyW(pswzcpy, pswz);
  86. }