msputils.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * This file has no copyright assigned and is placed in the Public Domain.
  3. * This file is part of the mingw-w64 runtime package.
  4. * No warranty is given; refer to the file DISCLAIMER.PD within this package.
  5. */
  6. #ifndef __MSPUTILS_H_
  7. #define __MSPUTILS_H_
  8. #if _ATL_VER >= 0x0300
  9. #define DECLARE_VQI()
  10. #else
  11. #define DECLARE_VQI() STDMETHOD(QueryInterface)(REFIID iid,void **ppvObject) = 0; STDMETHOD_(ULONG,AddRef)() = 0; STDMETHOD_(ULONG,Release)() = 0;
  12. #endif
  13. #define MSP_(hr) (FAILED(hr)?MSP_ERROR:MSP_TRACE)
  14. extern __inline WINBOOL IsValidAggregatedMediaType(DWORD dwAggregatedMediaType) {
  15. const DWORD dwAllPossibleMediaTypes = TAPIMEDIATYPE_AUDIO | TAPIMEDIATYPE_VIDEO | TAPIMEDIATYPE_DATAMODEM | TAPIMEDIATYPE_G3FAX | TAPIMEDIATYPE_MULTITRACK;
  16. WINBOOL bValidMediaType = FALSE;
  17. if((0==(dwAggregatedMediaType & dwAllPossibleMediaTypes)) || (0!=(dwAggregatedMediaType & (~dwAllPossibleMediaTypes)))) {
  18. bValidMediaType = FALSE;
  19. } else {
  20. bValidMediaType = TRUE;
  21. }
  22. return bValidMediaType;
  23. }
  24. extern __inline WINBOOL IsSingleMediaType(DWORD dwMediaType) { return !((dwMediaType==0) || ((dwMediaType & (dwMediaType - 1))!=0)); }
  25. extern __inline WINBOOL IsValidSingleMediaType(DWORD dwMediaType,DWORD dwMask) { return IsSingleMediaType(dwMediaType) && ((dwMediaType & dwMask)==dwMediaType); }
  26. const DWORD INITIAL = 8;
  27. const DWORD DELTA = 8;
  28. template <class T,DWORD dwInitial = INITIAL,DWORD dwDelta = DELTA> class CMSPArray {
  29. protected:
  30. T *m_aT;
  31. int m_nSize;
  32. int m_nAllocSize;
  33. public:
  34. CMSPArray() : m_aT(NULL),m_nSize(0),m_nAllocSize(0) { }
  35. ~CMSPArray() { RemoveAll(); }
  36. int GetSize() const { return m_nSize; }
  37. WINBOOL Grow() {
  38. T *aT;
  39. int nNewAllocSize = (m_nAllocSize==0) ? dwInitial : (m_nSize + DELTA);
  40. aT = (T *)realloc(m_aT,nNewAllocSize *sizeof(T));
  41. if(!aT) return FALSE;
  42. m_nAllocSize = nNewAllocSize;
  43. m_aT = aT;
  44. return TRUE;
  45. }
  46. WINBOOL Add(T &t) {
  47. if(m_nSize==m_nAllocSize) {
  48. if(!Grow()) return FALSE;
  49. }
  50. m_nSize++;
  51. SetAtIndex(m_nSize - 1,t);
  52. return TRUE;
  53. }
  54. WINBOOL Remove(T &t) {
  55. int nIndex = Find(t);
  56. if(nIndex==-1) return FALSE;
  57. return RemoveAt(nIndex);
  58. }
  59. WINBOOL RemoveAt(int nIndex) {
  60. if(nIndex!=(m_nSize - 1))
  61. memmove((void*)&m_aT[nIndex],(void*)&m_aT[nIndex + 1],(m_nSize - (nIndex + 1))*sizeof(T));
  62. m_nSize--;
  63. return TRUE;
  64. }
  65. void RemoveAll() {
  66. if(m_nAllocSize > 0) {
  67. free(m_aT);
  68. m_aT = NULL;
  69. m_nSize = 0;
  70. m_nAllocSize = 0;
  71. }
  72. }
  73. T &operator[] (int nIndex) const {
  74. _ASSERTE(nIndex >= 0 && nIndex < m_nSize);
  75. return m_aT[nIndex];
  76. }
  77. T *GetData() const { return m_aT; }
  78. void SetAtIndex(int nIndex,T &t) {
  79. _ASSERTE(nIndex >= 0 && nIndex < m_nSize);
  80. m_aT[nIndex] = t;
  81. }
  82. int Find(T &t) const {
  83. for(int i = 0;i < m_nSize;i++) {
  84. if(m_aT[i]==t) return i;
  85. }
  86. return -1;
  87. }
  88. };
  89. class CMSPCritSection {
  90. private:
  91. CRITICAL_SECTION m_CritSec;
  92. public:
  93. CMSPCritSection() { InitializeCriticalSection(&m_CritSec); }
  94. ~CMSPCritSection() { DeleteCriticalSection(&m_CritSec); }
  95. void Lock() { EnterCriticalSection(&m_CritSec); }
  96. WINBOOL TryLock() { return TryEnterCriticalSection(&m_CritSec); }
  97. void Unlock() { LeaveCriticalSection(&m_CritSec); }
  98. };
  99. class CLock {
  100. private:
  101. CMSPCritSection &m_CriticalSection;
  102. public:
  103. CLock(CMSPCritSection &CriticalSection) : m_CriticalSection(CriticalSection) {
  104. m_CriticalSection.Lock();
  105. }
  106. ~CLock() { m_CriticalSection.Unlock(); }
  107. };
  108. class CCSLock {
  109. private:
  110. CRITICAL_SECTION *m_pCritSec;
  111. public:
  112. CCSLock(CRITICAL_SECTION *pCritSec) : m_pCritSec(pCritSec) {
  113. EnterCriticalSection(m_pCritSec);
  114. }
  115. ~CCSLock() { LeaveCriticalSection(m_pCritSec); }
  116. };
  117. #ifndef CONTAINING_RECORD
  118. #define CONTAINING_RECORD(address,type,field) ((type *)((PCHAR)(address) - (ULONG_PTR)(&((type *)0)->field)))
  119. #endif
  120. #ifndef InitializeListHead
  121. #define InitializeListHead(ListHead) ((ListHead)->Flink = (ListHead)->Blink = (ListHead))
  122. #define IsListEmpty(ListHead) ((ListHead)->Flink==(ListHead))
  123. #define RemoveHeadList(ListHead) (ListHead)->Flink; {RemoveEntryList((ListHead)->Flink)}
  124. #define RemoveTailList(ListHead) (ListHead)->Blink; {RemoveEntryList((ListHead)->Blink)}
  125. #define RemoveEntryList(Entry) { PLIST_ENTRY _EX_Blink; PLIST_ENTRY _EX_Flink; _EX_Flink = (Entry)->Flink; _EX_Blink = (Entry)->Blink; _EX_Blink->Flink = _EX_Flink; _EX_Flink->Blink = _EX_Blink; }
  126. #define InsertTailList(ListHead,Entry) { PLIST_ENTRY _EX_Blink; PLIST_ENTRY _EX_ListHead; _EX_ListHead = (ListHead); _EX_Blink = _EX_ListHead->Blink; (Entry)->Flink = _EX_ListHead; (Entry)->Blink = _EX_Blink; _EX_Blink->Flink = (Entry); _EX_ListHead->Blink = (Entry); }
  127. #define InsertHeadList(ListHead,Entry) { PLIST_ENTRY _EX_Flink; PLIST_ENTRY _EX_ListHead; _EX_ListHead = (ListHead); _EX_Flink = _EX_ListHead->Flink; (Entry)->Flink = _EX_Flink; (Entry)->Blink = _EX_ListHead; _EX_Flink->Blink = (Entry); _EX_ListHead->Flink = (Entry); }
  128. WINBOOL IsNodeOnList(PLIST_ENTRY ListHead,PLIST_ENTRY Entry);
  129. #endif
  130. template <class T> ULONG MSPAddRefHelper (T *pMyThis) {
  131. LOG((MSP_INFO,"MSPAddRefHelper - this = 0x%08x",pMyThis));
  132. typedef CComAggObject<T> AggClass;
  133. AggClass *p = CONTAINING_RECORD(pMyThis,AggClass,m_contained);
  134. return p->AddRef();
  135. }
  136. template <class T> ULONG MSPReleaseHelper (T *pMyThis) {
  137. LOG((MSP_INFO,"MSPReleaseHelper - this = 0x%08x",pMyThis));
  138. typedef CComAggObject<T> AggClass;
  139. AggClass *p = CONTAINING_RECORD(pMyThis,AggClass,m_contained);
  140. return p->Release();
  141. }
  142. #include <objsafe.h>
  143. class CMSPObjectSafetyImpl : public IObjectSafety {
  144. public:
  145. CMSPObjectSafetyImpl() : m_dwSafety(0) { }
  146. enum {
  147. SUPPORTED_SAFETY_OPTIONS = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA
  148. };
  149. STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid,DWORD dwOptionSetMask,DWORD dwEnabledOptions) {
  150. if((~SUPPORTED_SAFETY_OPTIONS & dwOptionSetMask)!=0) return E_FAIL;
  151. IUnknown *pUnk = NULL;
  152. HRESULT hr = QueryInterface(riid,(void**)&pUnk);
  153. if(SUCCEEDED(hr)) {
  154. pUnk->Release();
  155. pUnk = NULL;
  156. s_CritSection.Lock();
  157. m_dwSafety = (dwEnabledOptions & dwOptionSetMask) | (m_dwSafety & ~dwOptionSetMask);
  158. s_CritSection.Unlock();
  159. }
  160. return hr;
  161. }
  162. STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid,DWORD *pdwSupportedOptions,DWORD *pdwEnabledOptions) {
  163. if(IsBadWritePtr(pdwSupportedOptions,sizeof(DWORD)) || IsBadWritePtr(pdwEnabledOptions,sizeof(DWORD))) return E_POINTER;
  164. *pdwSupportedOptions = 0;
  165. *pdwEnabledOptions = 0;
  166. IUnknown *pUnk = NULL;
  167. HRESULT hr = QueryInterface(riid,(void**)&pUnk);
  168. if(SUCCEEDED(hr)) {
  169. pUnk->Release();
  170. pUnk = NULL;
  171. *pdwSupportedOptions = SUPPORTED_SAFETY_OPTIONS;
  172. s_CritSection.Lock();
  173. *pdwEnabledOptions = m_dwSafety;
  174. s_CritSection.Unlock();
  175. }
  176. return hr;
  177. }
  178. private:
  179. DWORD m_dwSafety;
  180. static CMSPCritSection s_CritSection;
  181. };
  182. #endif