comparison fuhtark_test/include/winapi/msputils.h @ 1500:91c8c3b7cbf0

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