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
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
)
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
);
70 void release_object( struct object
*obj
)
72 ULONG refs
= InterlockedDecrement( &obj
->refs
);
75 if (obj
->close
) obj
->close( obj
);
76 TRACE( "destroying object %p\n", obj
);
81 #define HANDLE_TABLE_SIZE 4
82 HANDLE
alloc_handle( struct object
*obj
)
85 ULONG_PTR index
, count
;
87 EnterCriticalSection( &mscms_handle_cs
);
90 count
= HANDLE_TABLE_SIZE
;
91 if (!(ptr
= calloc( 1, sizeof(*ptr
) * count
)))
93 LeaveCriticalSection( &mscms_handle_cs
);
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
);
109 memset( (char *)ptr
+ old_size
, 0, new_size
- old_size
);
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
)
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
);