microcom.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // microcom.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides support for basic COM-like constructs. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef __DXC_MICROCOM__
  12. #define __DXC_MICROCOM__
  13. template <typename TIface>
  14. class CComInterfaceArray {
  15. private:
  16. TIface **m_pData;
  17. unsigned m_length;
  18. public:
  19. CComInterfaceArray() : m_pData(nullptr), m_length(0) { }
  20. ~CComInterfaceArray() {
  21. clear();
  22. }
  23. bool empty() const { return m_length == 0; }
  24. unsigned size() const { return m_length; }
  25. TIface ***data_ref() { return &m_pData; }
  26. unsigned *size_ref() { return &m_length; }
  27. TIface **begin() {
  28. return m_pData;
  29. }
  30. TIface **end() {
  31. return m_pData + m_length;
  32. }
  33. void clear() {
  34. if (m_length) {
  35. for (unsigned i = 0; i < m_length; ++i) {
  36. if (m_pData[i] != nullptr) {
  37. m_pData[i]->Release();
  38. m_pData[i] = nullptr;
  39. }
  40. }
  41. m_length = 0;
  42. }
  43. if (m_pData) {
  44. CoTaskMemFree(m_pData);
  45. m_pData = nullptr;
  46. }
  47. }
  48. HRESULT alloc(unsigned count) {
  49. clear();
  50. m_pData = (TIface**)CoTaskMemAlloc(sizeof(TIface*) * count);
  51. if (m_pData == nullptr)
  52. return E_OUTOFMEMORY;
  53. m_length = count;
  54. ZeroMemory(m_pData, sizeof(TIface*) * count);
  55. return S_OK;
  56. }
  57. TIface **get_address_of(unsigned index) {
  58. return &(m_pData[index]);
  59. }
  60. TIface **release() {
  61. TIface **result = m_pData;
  62. m_pData = nullptr;
  63. m_length = 0;
  64. return result;
  65. }
  66. void release(TIface ***pValues, unsigned *length) {
  67. *pValues = m_pData;
  68. m_pData = nullptr;
  69. *length = m_length;
  70. m_length = 0;
  71. }
  72. };
  73. #define DXC_MICROCOM_REF_FIELD(m_dwRef) volatile ULONG m_dwRef = 0;
  74. #define DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  75. ULONG STDMETHODCALLTYPE AddRef() {\
  76. return InterlockedIncrement(&m_dwRef); \
  77. }
  78. #define DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef) \
  79. DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  80. ULONG STDMETHODCALLTYPE Release() { \
  81. ULONG result = InterlockedDecrement(&m_dwRef); \
  82. if (result == 0) delete this; \
  83. return result; \
  84. }
  85. template <typename T, typename... Args>
  86. inline T *CreateOnMalloc(IMalloc * pMalloc, Args&&... args) {
  87. void *P = pMalloc->Alloc(sizeof(T));
  88. try { if (P) new (P)T(pMalloc, std::forward<Args>(args)...); }
  89. catch (...) { pMalloc->Free(P); throw; }
  90. return (T *)P;
  91. }
  92. template<typename T>
  93. void DxcCallDestructor(T *obj) {
  94. obj->~T();
  95. }
  96. // The "TM" version keep an IMalloc field that, if not null, indicate
  97. // ownership of 'this' and of any allocations used during release.
  98. #define DXC_MICROCOM_TM_REF_FIELDS() \
  99. volatile ULONG m_dwRef = 0;\
  100. CComPtr<IMalloc> m_pMalloc;
  101. #define DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() \
  102. DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  103. ULONG STDMETHODCALLTYPE Release() { \
  104. ULONG result = InterlockedDecrement(&m_dwRef); \
  105. if (result == 0) { \
  106. CComPtr<IMalloc> pTmp(m_pMalloc); \
  107. DxcThreadMalloc M(pTmp); \
  108. DxcCallDestructor(this); \
  109. pTmp->Free(this); \
  110. } \
  111. return result; \
  112. }
  113. #define DXC_MICROCOM_TM_CTOR(T) \
  114. DXC_MICROCOM_TM_CTOR_ONLY(T) \
  115. DXC_MICROCOM_TM_ALLOC(T)
  116. #define DXC_MICROCOM_TM_CTOR_ONLY(T) \
  117. T(IMalloc *pMalloc) : m_dwRef(0), m_pMalloc(pMalloc) { }
  118. #define DXC_MICROCOM_TM_ALLOC(T) \
  119. template <typename... Args> \
  120. static T* Alloc(IMalloc *pMalloc, Args&&... args) { \
  121. void *P = pMalloc->Alloc(sizeof(T)); \
  122. try { if (P) new (P)T(pMalloc, std::forward<Args>(args)...); } \
  123. catch (...) { pMalloc->Free(P); throw; } \
  124. return (T *)P; \
  125. }
  126. /// <summary>
  127. /// Provides a QueryInterface implementation for a class that supports
  128. /// any number of interfaces in addition to IUnknown.
  129. /// </summary>
  130. /// <remarks>
  131. /// This implementation will also report the instance as not supporting
  132. /// marshaling. This will help catch marshaling problems early or avoid
  133. /// them altogether.
  134. /// </remarks>
  135. template<typename... Ts, typename TObject>
  136. HRESULT DoBasicQueryInterface(TObject* self, REFIID iid, void** ppvObject) {
  137. if (ppvObject == nullptr) return E_POINTER;
  138. // Support INoMarshal to void GIT shenanigans.
  139. if (IsEqualIID(iid, __uuidof(IUnknown)) ||
  140. IsEqualIID(iid, __uuidof(INoMarshal))) {
  141. *ppvObject = reinterpret_cast<IUnknown*>(self);
  142. reinterpret_cast<IUnknown*>(self)->AddRef();
  143. return S_OK;
  144. }
  145. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  146. }
  147. template<typename TObject>
  148. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  149. return E_NOINTERFACE;
  150. }
  151. template<typename TObject, typename TInterface, typename... Ts>
  152. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  153. if (ppvObject == nullptr) return E_POINTER;
  154. if (IsEqualIID(iid, __uuidof(TInterface))) {
  155. *(TInterface**)ppvObject = self;
  156. self->AddRef();
  157. return S_OK;
  158. }
  159. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  160. }
  161. template <typename T>
  162. HRESULT AssignToOut(T value, _Out_ T* pResult) {
  163. if (pResult == nullptr)
  164. return E_POINTER;
  165. *pResult = value;
  166. return S_OK;
  167. }
  168. template <typename T>
  169. HRESULT AssignToOut(nullptr_t value, _Out_ T* pResult) {
  170. if (pResult == nullptr)
  171. return E_POINTER;
  172. *pResult = value;
  173. return S_OK;
  174. }
  175. template <typename T>
  176. HRESULT ZeroMemoryToOut(_Out_ T* pResult) {
  177. if (pResult == nullptr)
  178. return E_POINTER;
  179. ZeroMemory(pResult, sizeof(*pResult));
  180. return S_OK;
  181. }
  182. template <typename T>
  183. void AssignToOutOpt(T value, _Out_opt_ T* pResult) {
  184. if (pResult != nullptr)
  185. *pResult = value;
  186. }
  187. template <typename T>
  188. void AssignToOutOpt(nullptr_t value, _Out_opt_ T* pResult) {
  189. if (pResult != nullptr)
  190. *pResult = value;
  191. }
  192. #endif