Implemented IMemAllocator.
[wine/dcerpc.git] / dlls / quartz / complist.c
blob48fca2b978959da591e4799ce9f8cb6817136362
1 /*
2 * List of components. (for internal use)
4 * hidenori@a2.ctktv.ne.jp
5 */
7 #include "config.h"
9 #include "windef.h"
10 #include "winbase.h"
11 #include "wingdi.h"
12 #include "winuser.h"
13 #include "winerror.h"
14 #include "wine/obj_base.h"
15 #include "strmif.h"
17 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(quartz);
20 #include "quartz_private.h"
21 #include "complist.h"
25 struct QUARTZ_CompList
27 QUARTZ_CompListItem* pFirst;
28 QUARTZ_CompListItem* pLast;
29 CRITICAL_SECTION csList;
32 struct QUARTZ_CompListItem
34 IUnknown* punk;
35 QUARTZ_CompListItem* pNext;
36 QUARTZ_CompListItem* pPrev;
37 void* pvData;
38 DWORD dwDataLen;
42 QUARTZ_CompList* QUARTZ_CompList_Alloc( void )
44 QUARTZ_CompList* pList;
46 pList = (QUARTZ_CompList*)QUARTZ_AllocMem( sizeof(QUARTZ_CompList) );
47 if ( pList != NULL )
49 /* construct. */
50 pList->pFirst = NULL;
51 pList->pLast = NULL;
53 InitializeCriticalSection( &pList->csList );
56 return pList;
59 void QUARTZ_CompList_Free( QUARTZ_CompList* pList )
61 QUARTZ_CompListItem* pCur;
62 QUARTZ_CompListItem* pNext;
64 if ( pList != NULL )
66 pCur = pList->pFirst;
67 while ( pCur != NULL )
69 pNext = pCur->pNext;
70 if ( pCur->punk != NULL )
71 IUnknown_Release( pCur->punk );
72 if ( pCur->pvData != NULL )
73 QUARTZ_FreeMem( pCur->pvData );
74 QUARTZ_FreeMem( pCur );
75 pCur = pNext;
78 DeleteCriticalSection( &pList->csList );
80 QUARTZ_FreeMem( pList );
84 void QUARTZ_CompList_Lock( QUARTZ_CompList* pList )
86 EnterCriticalSection( &pList->csList );
89 void QUARTZ_CompList_Unlock( QUARTZ_CompList* pList )
91 LeaveCriticalSection( &pList->csList );
94 QUARTZ_CompList* QUARTZ_CompList_Dup(
95 const QUARTZ_CompList* pList, BOOL fDupData )
97 QUARTZ_CompList* pNewList;
98 const QUARTZ_CompListItem* pCur;
99 HRESULT hr;
101 pNewList = QUARTZ_CompList_Alloc();
102 if ( pNewList == NULL )
103 return NULL;
105 pCur = pList->pFirst;
106 while ( pCur != NULL )
108 if ( pCur->punk != NULL )
110 if ( fDupData )
111 hr = QUARTZ_CompList_AddComp(
112 pNewList, pCur->punk,
113 pCur->pvData, pCur->dwDataLen );
114 else
115 hr = QUARTZ_CompList_AddComp(
116 pNewList, pCur->punk, NULL, 0 );
117 if ( FAILED(hr) )
119 QUARTZ_CompList_Free( pNewList );
120 return NULL;
123 pCur = pCur->pNext;
126 return pNewList;
129 HRESULT QUARTZ_CompList_AddComp(
130 QUARTZ_CompList* pList, IUnknown* punk,
131 const void* pvData, DWORD dwDataLen )
133 QUARTZ_CompListItem* pItem;
135 pItem = (QUARTZ_CompListItem*)QUARTZ_AllocMem( sizeof(QUARTZ_CompListItem) );
136 if ( pItem == NULL )
137 return E_OUTOFMEMORY; /* out of memory. */
139 pItem->pvData = NULL;
140 pItem->dwDataLen = 0;
141 if ( pvData != NULL )
143 pItem->pvData = (void*)QUARTZ_AllocMem( dwDataLen );
144 if ( pItem->pvData == NULL )
146 QUARTZ_FreeMem( pItem );
147 return E_OUTOFMEMORY;
149 memcpy( pItem->pvData, pvData, dwDataLen );
150 pItem->dwDataLen = dwDataLen;
153 pItem->punk = punk; IUnknown_AddRef(punk);
155 if ( pList->pFirst != NULL )
156 pList->pFirst->pPrev = pItem;
157 else
158 pList->pLast = pItem;
159 pItem->pNext = pList->pFirst;
160 pList->pFirst = pItem;
161 pItem->pPrev = NULL;
163 return S_OK;
166 HRESULT QUARTZ_CompList_RemoveComp( QUARTZ_CompList* pList, IUnknown* punk )
168 QUARTZ_CompListItem* pCur;
170 pCur = QUARTZ_CompList_SearchComp( pList, punk );
171 if ( pCur == NULL )
172 return S_FALSE; /* already removed. */
174 /* remove from list. */
175 if ( pCur->pNext != NULL )
176 pCur->pNext->pPrev = pCur->pPrev;
177 else
178 pList->pLast = pCur->pPrev;
179 if ( pCur->pPrev != NULL )
180 pCur->pPrev->pNext = pCur->pNext;
181 else
182 pList->pFirst = pCur->pNext;
184 /* release this item. */
185 if ( pCur->punk != NULL )
186 IUnknown_Release( pCur->punk );
187 if ( pCur->pvData != NULL )
188 QUARTZ_FreeMem( pCur->pvData );
189 QUARTZ_FreeMem( pCur );
191 return S_OK;
194 QUARTZ_CompListItem* QUARTZ_CompList_SearchComp(
195 QUARTZ_CompList* pList, IUnknown* punk )
197 QUARTZ_CompListItem* pCur;
199 pCur = pList->pFirst;
200 while ( pCur != NULL )
202 if ( pCur->punk == punk )
203 return pCur;
204 pCur = pCur->pNext;
207 return NULL;
210 QUARTZ_CompListItem* QUARTZ_CompList_SearchData(
211 QUARTZ_CompList* pList, const void* pvData, DWORD dwDataLen )
213 QUARTZ_CompListItem* pCur;
215 pCur = pList->pFirst;
216 while ( pCur != NULL )
218 if ( pCur->dwDataLen == dwDataLen &&
219 !memcmp( pCur->pvData, pvData, dwDataLen ) )
220 return pCur;
221 pCur = pCur->pNext;
224 return NULL;
227 QUARTZ_CompListItem* QUARTZ_CompList_GetFirst(
228 QUARTZ_CompList* pList )
230 return pList->pFirst;
233 QUARTZ_CompListItem* QUARTZ_CompList_GetNext(
234 QUARTZ_CompList* pList, QUARTZ_CompListItem* pPrev )
236 return pPrev->pNext;
239 IUnknown* QUARTZ_CompList_GetItemPtr( QUARTZ_CompListItem* pItem )
241 return pItem->punk;
244 const void* QUARTZ_CompList_GetDataPtr( QUARTZ_CompListItem* pItem )
246 return pItem->pvData;
249 DWORD QUARTZ_CompList_GetDataLength( QUARTZ_CompListItem* pItem )
251 return pItem->dwDataLen;