ws2_32: Close the dest socket (Coverity).
[wine.git] / dlls / qmgr / enum_jobs.c
blob9af4e3df3daadd1e0e745375aff98de086fb9f1b
1 /*
2 * Queue Manager (BITS) Job Enumerator
4 * Copyright 2007 Google (Roy Shea)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "qmgr.h"
22 #include "wine/debug.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(qmgr);
26 typedef struct
28 IEnumBackgroundCopyJobs IEnumBackgroundCopyJobs_iface;
29 LONG ref;
30 IBackgroundCopyJob **jobs;
31 ULONG numJobs;
32 ULONG indexJobs;
33 } EnumBackgroundCopyJobsImpl;
35 static inline EnumBackgroundCopyJobsImpl *impl_from_IEnumBackgroundCopyJobs(IEnumBackgroundCopyJobs *iface)
37 return CONTAINING_RECORD(iface, EnumBackgroundCopyJobsImpl, IEnumBackgroundCopyJobs_iface);
40 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_QueryInterface(IEnumBackgroundCopyJobs *iface,
41 REFIID riid, void **ppv)
43 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppv);
45 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IEnumBackgroundCopyJobs))
47 *ppv = iface;
48 IEnumBackgroundCopyJobs_AddRef(iface);
49 return S_OK;
52 *ppv = NULL;
53 return E_NOINTERFACE;
56 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_AddRef(IEnumBackgroundCopyJobs *iface)
58 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
59 ULONG ref = InterlockedIncrement(&This->ref);
61 TRACE("(%p) ref=%d\n", This, ref);
63 return ref;
66 static ULONG WINAPI BITS_IEnumBackgroundCopyJobs_Release(IEnumBackgroundCopyJobs *iface)
68 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
69 ULONG ref = InterlockedDecrement(&This->ref);
70 ULONG i;
72 TRACE("(%p) ref=%d\n", This, ref);
74 if (ref == 0) {
75 for(i = 0; i < This->numJobs; i++)
76 IBackgroundCopyJob_Release(This->jobs[i]);
77 HeapFree(GetProcessHeap(), 0, This->jobs);
78 HeapFree(GetProcessHeap(), 0, This);
81 return ref;
84 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Next(IEnumBackgroundCopyJobs *iface, ULONG celt,
85 IBackgroundCopyJob **rgelt, ULONG *pceltFetched)
87 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
88 ULONG fetched;
89 ULONG i;
90 IBackgroundCopyJob *job;
92 fetched = min(celt, This->numJobs - This->indexJobs);
93 if (pceltFetched)
94 *pceltFetched = fetched;
95 else
97 /* We need to initialize this array if the caller doesn't request
98 the length because length_is will default to celt. */
99 for (i = 0; i < celt; ++i)
100 rgelt[i] = NULL;
102 /* pceltFetched can only be NULL if celt is 1 */
103 if (celt != 1)
104 return E_INVALIDARG;
107 /* Fill in the array of objects */
108 for (i = 0; i < fetched; ++i)
110 job = This->jobs[This->indexJobs++];
111 IBackgroundCopyJob_AddRef(job);
112 rgelt[i] = job;
115 return fetched == celt ? S_OK : S_FALSE;
118 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Skip(IEnumBackgroundCopyJobs *iface, ULONG celt)
120 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
122 if (This->numJobs - This->indexJobs < celt)
124 This->indexJobs = This->numJobs;
125 return S_FALSE;
128 This->indexJobs += celt;
129 return S_OK;
132 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Reset(IEnumBackgroundCopyJobs *iface)
134 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
135 This->indexJobs = 0;
136 return S_OK;
139 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_Clone(IEnumBackgroundCopyJobs *iface,
140 IEnumBackgroundCopyJobs **ppenum)
142 FIXME("Not implemented\n");
143 return E_NOTIMPL;
146 static HRESULT WINAPI BITS_IEnumBackgroundCopyJobs_GetCount(IEnumBackgroundCopyJobs *iface,
147 ULONG *puCount)
149 EnumBackgroundCopyJobsImpl *This = impl_from_IEnumBackgroundCopyJobs(iface);
150 *puCount = This->numJobs;
151 return S_OK;
154 static const IEnumBackgroundCopyJobsVtbl BITS_IEnumBackgroundCopyJobs_Vtbl =
156 BITS_IEnumBackgroundCopyJobs_QueryInterface,
157 BITS_IEnumBackgroundCopyJobs_AddRef,
158 BITS_IEnumBackgroundCopyJobs_Release,
159 BITS_IEnumBackgroundCopyJobs_Next,
160 BITS_IEnumBackgroundCopyJobs_Skip,
161 BITS_IEnumBackgroundCopyJobs_Reset,
162 BITS_IEnumBackgroundCopyJobs_Clone,
163 BITS_IEnumBackgroundCopyJobs_GetCount
166 HRESULT enum_copy_job_create(BackgroundCopyManagerImpl *qmgr, IEnumBackgroundCopyJobs **enumjob)
168 EnumBackgroundCopyJobsImpl *This;
169 BackgroundCopyJobImpl *job;
170 ULONG i;
172 TRACE("%p, %p)\n", qmgr, enumjob);
174 This = HeapAlloc(GetProcessHeap(), 0, sizeof *This);
175 if (!This)
176 return E_OUTOFMEMORY;
177 This->IEnumBackgroundCopyJobs_iface.lpVtbl = &BITS_IEnumBackgroundCopyJobs_Vtbl;
178 This->ref = 1;
180 /* Create array of jobs */
181 This->indexJobs = 0;
183 EnterCriticalSection(&qmgr->cs);
184 This->numJobs = list_count(&qmgr->jobs);
186 if (0 < This->numJobs)
188 This->jobs = HeapAlloc(GetProcessHeap(), 0,
189 This->numJobs * sizeof *This->jobs);
190 if (!This->jobs)
192 LeaveCriticalSection(&qmgr->cs);
193 HeapFree(GetProcessHeap(), 0, This);
194 return E_OUTOFMEMORY;
197 else
198 This->jobs = NULL;
200 i = 0;
201 LIST_FOR_EACH_ENTRY(job, &qmgr->jobs, BackgroundCopyJobImpl, entryFromQmgr)
203 IBackgroundCopyJob *job_iface = (IBackgroundCopyJob*)&job->IBackgroundCopyJob2_iface;
204 IBackgroundCopyJob_AddRef(job_iface);
205 This->jobs[i++] = job_iface;
207 LeaveCriticalSection(&qmgr->cs);
209 *enumjob = &This->IEnumBackgroundCopyJobs_iface;
210 return S_OK;