msi: Remove limit on number of handles.
[wine] / dlls / msi / handle.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2004 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winreg.h"
26 #include "shlwapi.h"
27 #include "wine/debug.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "msipriv.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(msi);
33
34 static CRITICAL_SECTION MSI_handle_cs;
35 static CRITICAL_SECTION_DEBUG MSI_handle_cs_debug =
36 {
37     0, 0, &MSI_handle_cs,
38     { &MSI_handle_cs_debug.ProcessLocksList, 
39       &MSI_handle_cs_debug.ProcessLocksList },
40       0, 0, { (DWORD_PTR)(__FILE__ ": MSI_handle_cs") }
41 };
42 static CRITICAL_SECTION MSI_handle_cs = { &MSI_handle_cs_debug, -1, 0, 0, 0, 0 };
43
44 static CRITICAL_SECTION MSI_object_cs;
45 static CRITICAL_SECTION_DEBUG MSI_object_cs_debug =
46 {
47     0, 0, &MSI_object_cs,
48     { &MSI_object_cs_debug.ProcessLocksList, 
49       &MSI_object_cs_debug.ProcessLocksList },
50       0, 0, { (DWORD_PTR)(__FILE__ ": MSI_object_cs") }
51 };
52 static CRITICAL_SECTION MSI_object_cs = { &MSI_object_cs_debug, -1, 0, 0, 0, 0 };
53
54 typedef struct msi_handle_info_t
55 {
56     MSIOBJECTHDR *obj;
57     DWORD dwThreadId;
58 } msi_handle_info;
59
60 static msi_handle_info *msihandletable = NULL;
61 static int msihandletable_size = 0;
62
63 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
64 {
65     MSIHANDLE ret = 0;
66     UINT i;
67
68     EnterCriticalSection( &MSI_handle_cs );
69
70     /* find a slot */
71     for(i=0; i<msihandletable_size; i++)
72         if( !msihandletable[i].obj )
73             break;
74     if( i==msihandletable_size )
75     {
76         msi_handle_info *p;
77         int newsize;
78         if (msihandletable_size == 0)
79         {
80             newsize = 256;
81             p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
82         }
83         else 
84         {
85             newsize = msihandletable_size * 2;
86             p = msi_realloc_zero(msihandletable,
87                             newsize*sizeof(msi_handle_info));
88         }
89         if (!p)
90             goto out;
91         msihandletable = p;
92         msihandletable_size = newsize;
93     }
94
95     msiobj_addref( obj );
96     msihandletable[i].obj = obj;
97     msihandletable[i].dwThreadId = GetCurrentThreadId();
98     ret = (MSIHANDLE) (i+1);
99 out:
100     TRACE("%p -> %ld\n", obj, ret );
101
102     LeaveCriticalSection( &MSI_handle_cs );
103     return ret;
104 }
105
106 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
107 {
108     MSIOBJECTHDR *ret = NULL;
109
110     EnterCriticalSection( &MSI_handle_cs );
111     handle--;
112     if( handle<0 )
113         goto out;
114     if( handle>=msihandletable_size )
115         goto out;
116     if( !msihandletable[handle].obj )
117         goto out;
118     if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
119         goto out;
120     if( type && (msihandletable[handle].obj->type != type) )
121         goto out;
122     ret = msihandletable[handle].obj;
123     msiobj_addref( ret );
124     
125 out:
126     LeaveCriticalSection( &MSI_handle_cs );
127
128     return (void*) ret;
129 }
130
131 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
132 {
133     MSIOBJECTHDR *info;
134
135     info = msi_alloc_zero( size );
136     if( info )
137     {
138         info->magic = MSIHANDLE_MAGIC;
139         info->type = type;
140         info->refcount = 1;
141         info->destructor = destroy;
142     }
143
144     return info;
145 }
146
147 void msiobj_addref( MSIOBJECTHDR *info )
148 {
149     TRACE("%p\n", info);
150
151     if( !info )
152         return;
153
154     if( info->magic != MSIHANDLE_MAGIC )
155     {
156         ERR("Invalid handle!\n");
157         return;
158     }
159
160     InterlockedIncrement(&info->refcount);
161 }
162
163 void msiobj_lock( MSIOBJECTHDR *info )
164 {
165     EnterCriticalSection( &MSI_object_cs );
166 }
167
168 void msiobj_unlock( MSIOBJECTHDR *info )
169 {
170     LeaveCriticalSection( &MSI_object_cs );
171 }
172
173 int msiobj_release( MSIOBJECTHDR *info )
174 {
175     int ret;
176
177     TRACE("%p\n",info);
178
179     if( !info )
180         return -1;
181
182     if( info->magic != MSIHANDLE_MAGIC )
183     {
184         ERR("Invalid handle!\n");
185         return -1;
186     }
187
188     ret = InterlockedDecrement( &info->refcount );
189     if( ret==0 )
190     {
191         if( info->destructor )
192             info->destructor( info );
193         msi_free( info );
194         TRACE("object %p destroyed\n", info);
195     }
196
197     return ret;
198 }
199
200 /***********************************************************
201  *   MsiCloseHandle   [MSI.@]
202  */
203 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
204 {
205     MSIOBJECTHDR *info;
206     UINT ret = ERROR_INVALID_HANDLE;
207
208     TRACE("%lx\n",handle);
209
210     if (!handle)
211         return ERROR_SUCCESS;
212
213     EnterCriticalSection( &MSI_handle_cs );
214
215     info = msihandle2msiinfo(handle, 0);
216     if( !info )
217         goto out;
218
219     if( info->magic != MSIHANDLE_MAGIC )
220     {
221         ERR("Invalid handle!\n");
222         goto out;
223     }
224
225     msiobj_release( info );
226     msihandletable[handle-1].obj = NULL;
227     ret = ERROR_SUCCESS;
228
229     TRACE("handle %lx Destroyed\n", handle);
230 out:
231     LeaveCriticalSection( &MSI_handle_cs );
232     if( info )
233         msiobj_release( info );
234
235     return ret;
236 }
237
238 /***********************************************************
239  *   MsiCloseAllHandles   [MSI.@]
240  *
241  *  Closes all handles owned by the current thread
242  *
243  *  RETURNS:
244  *   The number of handles closed
245  */
246 UINT WINAPI MsiCloseAllHandles(void)
247 {
248     UINT i, n=0;
249
250     TRACE("\n");
251
252     EnterCriticalSection( &MSI_handle_cs );
253     for(i=0; i<msihandletable_size; i++)
254     {
255         if(msihandletable[i].dwThreadId == GetCurrentThreadId())
256         {
257             LeaveCriticalSection( &MSI_handle_cs );
258             MsiCloseHandle( i+1 );
259             EnterCriticalSection( &MSI_handle_cs );
260             n++;
261         }
262     }
263     LeaveCriticalSection( &MSI_handle_cs );
264
265     return n;
266 }