msi: Don't print traces for addref and release.
[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 void msi_free_handle_table(void)
64 {
65     msi_free( msihandletable );
66     msihandletable = NULL;
67     msihandletable_size = 0;
68 }
69
70 MSIHANDLE alloc_msihandle( MSIOBJECTHDR *obj )
71 {
72     MSIHANDLE ret = 0;
73     UINT i;
74
75     EnterCriticalSection( &MSI_handle_cs );
76
77     /* find a slot */
78     for(i=0; i<msihandletable_size; i++)
79         if( !msihandletable[i].obj )
80             break;
81     if( i==msihandletable_size )
82     {
83         msi_handle_info *p;
84         int newsize;
85         if (msihandletable_size == 0)
86         {
87             newsize = 256;
88             p = msi_alloc_zero(newsize*sizeof(msi_handle_info));
89         }
90         else
91         {
92             newsize = msihandletable_size * 2;
93             p = msi_realloc_zero(msihandletable,
94                             newsize*sizeof(msi_handle_info));
95         }
96         if (!p)
97             goto out;
98         msihandletable = p;
99         msihandletable_size = newsize;
100     }
101
102     msiobj_addref( obj );
103     msihandletable[i].obj = obj;
104     msihandletable[i].dwThreadId = GetCurrentThreadId();
105     ret = (MSIHANDLE) (i+1);
106 out:
107     TRACE("%p -> %ld\n", obj, ret );
108
109     LeaveCriticalSection( &MSI_handle_cs );
110     return ret;
111 }
112
113 void *msihandle2msiinfo(MSIHANDLE handle, UINT type)
114 {
115     MSIOBJECTHDR *ret = NULL;
116
117     EnterCriticalSection( &MSI_handle_cs );
118     handle--;
119     if( handle<0 )
120         goto out;
121     if( handle>=msihandletable_size )
122         goto out;
123     if( !msihandletable[handle].obj )
124         goto out;
125     if( msihandletable[handle].obj->magic != MSIHANDLE_MAGIC )
126         goto out;
127     if( type && (msihandletable[handle].obj->type != type) )
128         goto out;
129     ret = msihandletable[handle].obj;
130     msiobj_addref( ret );
131
132 out:
133     LeaveCriticalSection( &MSI_handle_cs );
134
135     return (void*) ret;
136 }
137
138 void *alloc_msiobject(UINT type, UINT size, msihandledestructor destroy )
139 {
140     MSIOBJECTHDR *info;
141
142     info = msi_alloc_zero( size );
143     if( info )
144     {
145         info->magic = MSIHANDLE_MAGIC;
146         info->type = type;
147         info->refcount = 1;
148         info->destructor = destroy;
149     }
150
151     return info;
152 }
153
154 void msiobj_addref( MSIOBJECTHDR *info )
155 {
156     if( !info )
157         return;
158
159     if( info->magic != MSIHANDLE_MAGIC )
160     {
161         ERR("Invalid handle!\n");
162         return;
163     }
164
165     InterlockedIncrement(&info->refcount);
166 }
167
168 void msiobj_lock( MSIOBJECTHDR *info )
169 {
170     EnterCriticalSection( &MSI_object_cs );
171 }
172
173 void msiobj_unlock( MSIOBJECTHDR *info )
174 {
175     LeaveCriticalSection( &MSI_object_cs );
176 }
177
178 int msiobj_release( MSIOBJECTHDR *info )
179 {
180     int ret;
181
182     if( !info )
183         return -1;
184
185     if( info->magic != MSIHANDLE_MAGIC )
186     {
187         ERR("Invalid handle!\n");
188         return -1;
189     }
190
191     ret = InterlockedDecrement( &info->refcount );
192     if( ret==0 )
193     {
194         if( info->destructor )
195             info->destructor( info );
196         msi_free( info );
197         TRACE("object %p destroyed\n", info);
198     }
199
200     return ret;
201 }
202
203 /***********************************************************
204  *   MsiCloseHandle   [MSI.@]
205  */
206 UINT WINAPI MsiCloseHandle(MSIHANDLE handle)
207 {
208     MSIOBJECTHDR *info;
209     UINT ret = ERROR_INVALID_HANDLE;
210
211     TRACE("%lx\n",handle);
212
213     if (!handle)
214         return ERROR_SUCCESS;
215
216     EnterCriticalSection( &MSI_handle_cs );
217
218     info = msihandle2msiinfo(handle, 0);
219     if( !info )
220         goto out;
221
222     if( info->magic != MSIHANDLE_MAGIC )
223     {
224         ERR("Invalid handle!\n");
225         goto out;
226     }
227
228     msiobj_release( info );
229     msihandletable[handle-1].obj = NULL;
230     ret = ERROR_SUCCESS;
231
232     TRACE("handle %lx Destroyed\n", handle);
233 out:
234     LeaveCriticalSection( &MSI_handle_cs );
235     if( info )
236         msiobj_release( info );
237
238     return ret;
239 }
240
241 /***********************************************************
242  *   MsiCloseAllHandles   [MSI.@]
243  *
244  *  Closes all handles owned by the current thread
245  *
246  *  RETURNS:
247  *   The number of handles closed
248  */
249 UINT WINAPI MsiCloseAllHandles(void)
250 {
251     UINT i, n=0;
252
253     TRACE("\n");
254
255     EnterCriticalSection( &MSI_handle_cs );
256     for(i=0; i<msihandletable_size; i++)
257     {
258         if(msihandletable[i].dwThreadId == GetCurrentThreadId())
259         {
260             LeaveCriticalSection( &MSI_handle_cs );
261             MsiCloseHandle( i+1 );
262             EnterCriticalSection( &MSI_handle_cs );
263             n++;
264         }
265     }
266     LeaveCriticalSection( &MSI_handle_cs );
267
268     return n;
269 }