ieframe: Fix memory leak in PersistFile_Save.
[wine.git] / dlls / mscms / handle.c
blob4e532a6fca37c2771a5b496f681a42353b9973e5
1 /*
2 * MSCMS - Color Management System for Wine
4 * Copyright 2004, 2005, 2008 Hans Leidekker
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 <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "icm.h"
28 #include "wine/debug.h"
30 #include "mscms_priv.h"
32 static CRITICAL_SECTION mscms_handle_cs;
33 static CRITICAL_SECTION_DEBUG mscms_handle_cs_debug =
35 0, 0, &mscms_handle_cs,
36 { &mscms_handle_cs_debug.ProcessLocksList,
37 &mscms_handle_cs_debug.ProcessLocksList },
38 0, 0, { (DWORD_PTR)(__FILE__ ": mscms_handle_cs") }
40 static CRITICAL_SECTION mscms_handle_cs = { &mscms_handle_cs_debug, -1, 0, 0, 0, 0 };
42 static struct profile *profiletable;
43 static struct transform *transformtable;
45 static unsigned int num_profile_handles;
46 static unsigned int num_transform_handles;
48 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
50 void free_handle_tables( void )
52 HeapFree( GetProcessHeap(), 0, profiletable );
53 profiletable = NULL;
54 num_profile_handles = 0;
56 HeapFree( GetProcessHeap(), 0, transformtable );
57 transformtable = NULL;
58 num_transform_handles = 0;
60 DeleteCriticalSection( &mscms_handle_cs );
63 struct profile *grab_profile( HPROFILE handle )
65 DWORD_PTR index;
67 EnterCriticalSection( &mscms_handle_cs );
69 index = (DWORD_PTR)handle - 1;
70 if (index > num_profile_handles)
72 LeaveCriticalSection( &mscms_handle_cs );
73 return NULL;
75 return &profiletable[index];
78 void release_profile( struct profile *profile )
80 LeaveCriticalSection( &mscms_handle_cs );
83 struct transform *grab_transform( HTRANSFORM handle )
85 DWORD_PTR index;
87 EnterCriticalSection( &mscms_handle_cs );
89 index = (DWORD_PTR)handle - 1;
90 if (index > num_transform_handles)
92 LeaveCriticalSection( &mscms_handle_cs );
93 return NULL;
95 return &transformtable[index];
98 void release_transform( struct transform *transform )
100 LeaveCriticalSection( &mscms_handle_cs );
103 static HPROFILE alloc_profile_handle( void )
105 DWORD_PTR index;
106 struct profile *p;
107 unsigned int count = 128;
109 for (index = 0; index < num_profile_handles; index++)
111 if (!profiletable[index].data) return (HPROFILE)(index + 1);
113 if (!profiletable)
115 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct profile) );
117 else
119 count = num_profile_handles * 2;
120 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, profiletable, count * sizeof(struct profile) );
122 if (!p) return NULL;
124 profiletable = p;
125 num_profile_handles = count;
127 return (HPROFILE)(index + 1);
130 HPROFILE create_profile( struct profile *profile )
132 HPROFILE handle;
134 EnterCriticalSection( &mscms_handle_cs );
136 if ((handle = alloc_profile_handle()))
138 DWORD_PTR index = (DWORD_PTR)handle - 1;
139 profiletable[index] = *profile;
141 LeaveCriticalSection( &mscms_handle_cs );
142 return handle;
145 BOOL close_profile( HPROFILE handle )
147 DWORD_PTR index;
148 struct profile *profile;
150 EnterCriticalSection( &mscms_handle_cs );
152 index = (DWORD_PTR)handle - 1;
153 if (index > num_profile_handles)
155 LeaveCriticalSection( &mscms_handle_cs );
156 return FALSE;
158 profile = &profiletable[index];
160 if (profile->file != INVALID_HANDLE_VALUE)
162 if (profile->access & PROFILE_READWRITE)
164 DWORD written;
166 if (SetFilePointer( profile->file, 0, NULL, FILE_BEGIN ) ||
167 !WriteFile( profile->file, profile->data, profile->size, &written, NULL ) ||
168 written != profile->size)
170 ERR( "Unable to write color profile\n" );
173 CloseHandle( profile->file );
175 if (profile->cmsprofile) lcms_funcs->close_profile( profile->cmsprofile );
176 HeapFree( GetProcessHeap(), 0, profile->data );
178 memset( profile, 0, sizeof(struct profile) );
180 LeaveCriticalSection( &mscms_handle_cs );
181 return TRUE;
184 static HTRANSFORM alloc_transform_handle( void )
186 DWORD_PTR index;
187 struct transform *p;
188 unsigned int count = 128;
190 for (index = 0; index < num_transform_handles; index++)
192 if (!transformtable[index].cmstransform) return (HTRANSFORM)(index + 1);
194 if (!transformtable)
196 p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, count * sizeof(struct transform) );
198 else
200 count = num_transform_handles * 2;
201 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, transformtable, count * sizeof(struct transform) );
203 if (!p) return NULL;
205 transformtable = p;
206 num_transform_handles = count;
208 return (HTRANSFORM)(index + 1);
211 HTRANSFORM create_transform( struct transform *transform )
213 HTRANSFORM handle;
215 EnterCriticalSection( &mscms_handle_cs );
217 if ((handle = alloc_transform_handle()))
219 DWORD_PTR index = (DWORD_PTR)handle - 1;
220 transformtable[index] = *transform;
222 LeaveCriticalSection( &mscms_handle_cs );
223 return handle;
226 BOOL close_transform( HTRANSFORM handle )
228 DWORD_PTR index;
229 struct transform *transform;
231 EnterCriticalSection( &mscms_handle_cs );
233 index = (DWORD_PTR)handle - 1;
234 if (index > num_transform_handles)
236 LeaveCriticalSection( &mscms_handle_cs );
237 return FALSE;
239 transform = &transformtable[index];
241 lcms_funcs->close_transform( transform->cmstransform );
243 memset( transform, 0, sizeof(struct transform) );
245 LeaveCriticalSection( &mscms_handle_cs );
246 return TRUE;