cmd: DIR command outputs free space for the path.
[wine.git] / dlls / mscms / handle.c
blob63c6a011bb359230b6b900ccec15e0a4c2178fc4
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>
22 #include <stdlib.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "icm.h"
29 #include "wine/debug.h"
31 #include "mscms_priv.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
35 static CRITICAL_SECTION mscms_handle_cs;
36 static CRITICAL_SECTION_DEBUG mscms_handle_cs_debug =
38 0, 0, &mscms_handle_cs,
39 { &mscms_handle_cs_debug.ProcessLocksList,
40 &mscms_handle_cs_debug.ProcessLocksList },
41 0, 0, { (DWORD_PTR)(__FILE__ ": mscms_handle_cs") }
43 static CRITICAL_SECTION mscms_handle_cs = { &mscms_handle_cs_debug, -1, 0, 0, 0, 0 };
45 static struct object **handle_table;
46 static ULONG_PTR next_handle;
47 static ULONG_PTR max_handles;
49 struct object *grab_object( HANDLE handle, enum object_type type )
51 struct object *obj = NULL;
52 ULONG_PTR index = (ULONG_PTR)handle;
54 EnterCriticalSection( &mscms_handle_cs );
55 if (index > 0 && index <= max_handles)
57 index--;
58 if (handle_table[index] && handle_table[index]->type == type)
60 obj = handle_table[index];
61 InterlockedIncrement( &obj->refs );
64 LeaveCriticalSection( &mscms_handle_cs );
66 TRACE( "handle %p -> %p\n", handle, obj );
67 return obj;
70 void release_object( struct object *obj )
72 ULONG refs = InterlockedDecrement( &obj->refs );
73 if (!refs)
75 if (obj->close) obj->close( obj );
76 TRACE( "destroying object %p\n", obj );
77 free( obj );
81 #define HANDLE_TABLE_SIZE 4
82 HANDLE alloc_handle( struct object *obj )
84 struct object **ptr;
85 ULONG_PTR index, count;
87 EnterCriticalSection( &mscms_handle_cs );
88 if (!max_handles)
90 count = HANDLE_TABLE_SIZE;
91 if (!(ptr = calloc( 1, sizeof(*ptr) * count )))
93 LeaveCriticalSection( &mscms_handle_cs );
94 return 0;
96 handle_table = ptr;
97 max_handles = count;
99 if (max_handles == next_handle)
101 size_t new_size, old_size = max_handles * sizeof(*ptr);
102 count = max_handles * 2;
103 new_size = count * sizeof(*ptr);
104 if (!(ptr = realloc( handle_table, new_size )))
106 LeaveCriticalSection( &mscms_handle_cs );
107 return 0;
109 memset( (char *)ptr + old_size, 0, new_size - old_size );
110 handle_table = ptr;
111 max_handles = count;
113 index = next_handle;
114 if (handle_table[index]) ERR( "handle isn't free but should be\n" );
116 handle_table[index] = obj;
117 InterlockedIncrement( &obj->refs );
118 while (next_handle < max_handles && handle_table[next_handle]) next_handle++;
120 LeaveCriticalSection( &mscms_handle_cs );
121 TRACE( "object %p -> %Ix\n", obj, index + 1 );
122 return (HANDLE)(index + 1);
125 void free_handle( HANDLE handle )
127 struct object *obj = NULL;
128 ULONG_PTR index = (ULONG_PTR)handle;
130 EnterCriticalSection( &mscms_handle_cs );
131 if (index > 0 && index <= max_handles)
133 index--;
134 if (handle_table[index])
136 obj = handle_table[index];
137 TRACE( "destroying handle %p for object %p\n", handle, obj );
138 handle_table[index] = NULL;
141 LeaveCriticalSection( &mscms_handle_cs );
143 if (obj) release_object( obj );
145 EnterCriticalSection( &mscms_handle_cs );
146 if (next_handle > index && !handle_table[index]) next_handle = index;
147 LeaveCriticalSection( &mscms_handle_cs );