microcom.h 8.1 KB

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