ddraw: Set dwMaxVertexCount to 2048.
[wine.git] / dlls / msi / handle.c
blob4093878431c14ad5038ce7c69c163de3ae7a0b99
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 Mike McCormack for CodeWeavers
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 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "shlwapi.h"
29 #include "wine/debug.h"
30 #include "msi.h"
31 #include "msiquery.h"
33 #include "msipriv.h"
34 #include "winemsi.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
38 static CRITICAL_SECTION handle_cs;
39 static CRITICAL_SECTION_DEBUG handle_cs_debug =
41 0, 0, &handle_cs,
42 { &handle_cs_debug.ProcessLocksList,
43 &handle_cs_debug.ProcessLocksList },
44 0, 0, { (DWORD_PTR)(__FILE__ ": handle_cs") }
46 static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
48 static CRITICAL_SECTION object_cs;
49 static CRITICAL_SECTION_DEBUG object_cs_debug =
51 0, 0, &object_cs,
52 { &object_cs_debug.ProcessLocksList,
53 &object_cs_debug.ProcessLocksList },
54 0, 0, { (DWORD_PTR)(__FILE__ ": object_cs") }
56 static CRITICAL_SECTION object_cs = { &object_cs_debug, -1, 0, 0, 0, 0 };
58 struct handle_info
60 BOOL remote;
61 union {
62 MSIOBJECTHDR *obj;
63 MSIHANDLE rem;
64 } u;
65 DWORD dwThreadId;
68 static struct handle_info *handle_table = NULL;
69 static unsigned int handle_table_size = 0;
71 void msi_free_handle_table(void)
73 free( handle_table );
74 handle_table = NULL;
75 handle_table_size = 0;
76 DeleteCriticalSection(&handle_cs);
77 DeleteCriticalSection(&object_cs);
80 static MSIHANDLE alloc_handle_table_entry(void)
82 UINT i;
84 /* find a slot */
85 for(i = 0; i < handle_table_size; i++)
86 if (!handle_table[i].u.obj && !handle_table[i].u.rem)
87 break;
88 if (i == handle_table_size)
90 struct handle_info *p;
91 int newsize;
92 if (!handle_table_size)
94 newsize = 256;
95 p = calloc(newsize, sizeof(*p));
97 else
99 newsize = handle_table_size * 2;
100 p = realloc(handle_table, newsize * sizeof(*p));
101 if (p) memset(p + handle_table_size, 0, (newsize - handle_table_size) * sizeof(*p));
103 if (!p)
104 return 0;
105 handle_table = p;
106 handle_table_size = newsize;
108 return i + 1;
111 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
113 struct handle_info *entry;
114 MSIHANDLE ret;
116 EnterCriticalSection( &handle_cs );
118 ret = alloc_handle_table_entry();
119 if (ret)
121 entry = &handle_table[ ret - 1 ];
122 msiobj_addref( obj );
123 entry->u.obj = obj;
124 entry->dwThreadId = GetCurrentThreadId();
125 entry->remote = FALSE;
128 LeaveCriticalSection( &handle_cs );
130 TRACE( "%p -> %lu\n", obj, ret );
132 return ret;
135 MSIHANDLE alloc_msi_remote_handle(MSIHANDLE remote)
137 struct handle_info *entry;
138 MSIHANDLE ret;
140 EnterCriticalSection( &handle_cs );
142 ret = alloc_handle_table_entry();
143 if (ret)
145 entry = &handle_table[ ret - 1 ];
146 entry->u.rem = remote;
147 entry->dwThreadId = GetCurrentThreadId();
148 entry->remote = TRUE;
151 LeaveCriticalSection( &handle_cs );
153 TRACE( "%lu -> %lu\n", remote, ret );
155 return ret;
158 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
160 MSIOBJECTHDR *ret = NULL;
162 EnterCriticalSection( &handle_cs );
163 handle--;
164 if (handle >= handle_table_size)
165 goto out;
166 if (handle_table[handle].remote)
167 goto out;
168 if (!handle_table[handle].u.obj)
169 goto out;
170 if (handle_table[handle].u.obj->magic != MSIHANDLE_MAGIC)
171 goto out;
172 if (type && (handle_table[handle].u.obj->type != type))
173 goto out;
174 ret = handle_table[handle].u.obj;
175 msiobj_addref( ret );
177 out:
178 LeaveCriticalSection( &handle_cs );
180 return ret;
183 MSIHANDLE msi_get_remote( MSIHANDLE handle )
185 MSIHANDLE ret = 0;
187 EnterCriticalSection( &handle_cs );
188 handle--;
189 if (handle >= handle_table_size)
190 goto out;
191 if (!handle_table[handle].remote)
192 goto out;
193 ret = handle_table[handle].u.rem;
195 out:
196 LeaveCriticalSection( &handle_cs );
198 return ret;
201 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
203 MSIOBJECTHDR *info;
205 info = calloc( 1, size );
206 if( info )
208 info->magic = MSIHANDLE_MAGIC;
209 info->type = type;
210 info->refcount = 1;
211 info->destructor = destroy;
214 return info;
217 void msiobj_addref( MSIOBJECTHDR *info )
219 if( !info )
220 return;
222 if( info->magic != MSIHANDLE_MAGIC )
224 ERR("Invalid handle!\n");
225 return;
228 InterlockedIncrement(&info->refcount);
231 void msiobj_lock( MSIOBJECTHDR *info )
233 EnterCriticalSection( &object_cs );
236 void msiobj_unlock( MSIOBJECTHDR *info )
238 LeaveCriticalSection( &object_cs );
241 int msiobj_release( MSIOBJECTHDR *info )
243 int ret;
245 if( !info )
246 return -1;
248 if( info->magic != MSIHANDLE_MAGIC )
250 ERR("Invalid handle!\n");
251 return -1;
254 ret = InterlockedDecrement( &info->refcount );
255 if( ret==0 )
257 if( info->destructor )
258 info->destructor( info );
259 TRACE("object %p destroyed\n", info);
260 free( info );
263 return ret;
266 /***********************************************************
267 * MsiCloseHandle [MSI.@]
269 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
271 MSIOBJECTHDR *info = NULL;
272 UINT ret = ERROR_INVALID_HANDLE;
274 TRACE( "%lu\n", handle );
276 if (!handle)
277 return ERROR_SUCCESS;
279 EnterCriticalSection( &handle_cs );
281 handle--;
282 if (handle >= handle_table_size)
283 goto out;
285 if (handle_table[handle].remote)
287 remote_CloseHandle( handle_table[handle].u.rem );
289 else
291 info = handle_table[handle].u.obj;
292 if( !info )
293 goto out;
295 if( info->magic != MSIHANDLE_MAGIC )
297 ERR("Invalid handle!\n");
298 goto out;
302 handle_table[handle].u.obj = NULL;
303 handle_table[handle].remote = 0;
304 handle_table[handle].dwThreadId = 0;
306 ret = ERROR_SUCCESS;
308 TRACE( "handle %lu destroyed\n", handle + 1 );
309 out:
310 LeaveCriticalSection( &handle_cs );
311 if( info )
312 msiobj_release( info );
314 return ret;
317 /***********************************************************
318 * MsiCloseAllHandles [MSI.@]
320 * Closes all handles owned by the current thread
322 * RETURNS:
323 * The number of handles closed
325 UINT WINAPI MsiCloseAllHandles(void)
327 UINT i, n=0;
329 TRACE("\n");
331 EnterCriticalSection( &handle_cs );
332 for (i = 0; i < handle_table_size; i++)
334 if (handle_table[i].dwThreadId == GetCurrentThreadId())
336 LeaveCriticalSection( &handle_cs );
337 MsiCloseHandle( i + 1 );
338 EnterCriticalSection( &handle_cs );
339 n++;
342 LeaveCriticalSection( &handle_cs );
344 return n;
347 UINT __cdecl s_remote_CloseHandle(MSIHANDLE handle)
349 return MsiCloseHandle(handle);