microcom.h 8.1 KB

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