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.

174 lines
3.0 KiB

  1. #ifndef _MISCHLPR_H_
  2. #define _MISCHLPR_H_
  3. #include <objbase.h>
  4. #include "dbg.h"
  5. #include "tfids.h"
  6. #define UNREF_PARAM(a)
  7. #define IID_PPV_ARG(IType, ppType) IID_##IType, reinterpret_cast<void**>(static_cast<IType**>(ppType))
  8. class CRefCounted
  9. {
  10. public:
  11. ULONG RCAddRef() { return ::InterlockedIncrement(&_cRef); }
  12. ULONG RCRelease()
  13. {
  14. ASSERT( 0 != _cRef );
  15. ULONG cRef = ::InterlockedDecrement(&_cRef);
  16. if (!cRef)
  17. {
  18. delete this;
  19. }
  20. return cRef;
  21. }
  22. CRefCounted() : _cRef(1) {}
  23. virtual ~CRefCounted() {}
  24. private:
  25. LONG _cRef;
  26. };
  27. class CRefCountedCritSect : public CRefCounted, public CRITICAL_SECTION
  28. {};
  29. class CCritSect : CRITICAL_SECTION
  30. {
  31. public:
  32. HRESULT Init()
  33. {
  34. if (InitializeCriticalSectionAndSpinCount(this, 0))
  35. {
  36. _fInited = TRUE;
  37. }
  38. #ifdef DEBUG
  39. _iLevel = 0;
  40. #endif
  41. return _fInited ? S_OK : E_FAIL;
  42. }
  43. void Enter()
  44. {
  45. ASSERT(_fInited);
  46. EnterCriticalSection(this);
  47. #ifdef DEBUG
  48. ++_iLevel;
  49. #endif
  50. }
  51. void Leave()
  52. {
  53. ASSERT(_fInited);
  54. #ifdef DEBUG
  55. --_iLevel;
  56. #endif
  57. LeaveCriticalSection(this);
  58. }
  59. void Delete()
  60. {
  61. if (_fInited)
  62. {
  63. _fInited = FALSE;
  64. DeleteCriticalSection(this);
  65. }
  66. }
  67. BOOL IsInitialized()
  68. {
  69. return _fInited;
  70. }
  71. BOOL _fInited;
  72. CCritSect() : _fInited(FALSE) {}
  73. #ifdef DEBUG
  74. BOOL IsInside()
  75. {
  76. ASSERT(_fInited);
  77. return _iLevel;
  78. }
  79. DWORD _iLevel;
  80. #endif
  81. };
  82. class CThreadTask
  83. {
  84. public:
  85. virtual ~CThreadTask() {}
  86. public:
  87. // Uses CreateThread, delete 'this' at the end
  88. HRESULT RunWithTimeout(DWORD dwTimeout);
  89. // Uses Thread Pool, delete 'this' at the end
  90. HRESULT Run();
  91. // Run on 'this' thread, does NOT delete 'this' at the end
  92. HRESULT RunSynchronously();
  93. protected:
  94. virtual HRESULT _DoStuff() = 0;
  95. private:
  96. static DWORD WINAPI _ThreadProc(void* pv);
  97. };
  98. template<typename TDataPtr>
  99. HRESULT _AllocMemoryChunk(DWORD cbSize, TDataPtr* pdataOut)
  100. {
  101. HRESULT hr;
  102. *pdataOut = (TDataPtr)LocalAlloc(LPTR, cbSize);
  103. if (*pdataOut)
  104. {
  105. hr = S_OK;
  106. }
  107. else
  108. {
  109. hr = E_OUTOFMEMORY;
  110. }
  111. return hr;
  112. }
  113. template<typename TDataPtr>
  114. HRESULT _DupMemoryChunk(TDataPtr pdata, DWORD cbSize, TDataPtr* pdataOut)
  115. {
  116. HRESULT hr;
  117. *pdataOut = (TDataPtr)LocalAlloc(LPTR, cbSize);
  118. if (*pdataOut)
  119. {
  120. CopyMemory((void*)*pdataOut, pdata, cbSize);
  121. hr = S_OK;
  122. }
  123. else
  124. {
  125. hr = E_OUTOFMEMORY;
  126. }
  127. return hr;
  128. }
  129. template<typename TDataPtr>
  130. HRESULT _FreeMemoryChunk(TDataPtr pdata)
  131. {
  132. HRESULT hr = S_OK;
  133. if (LocalFree((HLOCAL)pdata))
  134. {
  135. hr = E_FAIL;
  136. }
  137. return hr;
  138. }
  139. #endif //_MISCHLPR_H_