3 * Support code to manage HANDLE tables.
5 * Copyright 1998 Alexandre Julliard
6 * Copyright 2002-2004 Mike McCormack for CodeWeavers
7 * Copyright 2004 Michael Jung
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_NO_STATUS
25 #define WIN32_LEAN_AND_MEAN
34 #include <wine/debug.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(handle
);
38 #define HANDLE2INDEX(h) ((h)-1)
39 #define INDEX2HANDLE(i) ((i)+1)
41 /******************************************************************************
44 * Initializes the HANDLETABLE structure pointed to by lpTable
47 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initialized.
50 * You have to call destroy_handle_table when you don't need the table
53 void init_handle_table(struct handle_table
*lpTable
)
55 TRACE("(lpTable=%p)\n", lpTable
);
57 lpTable
->paEntries
= NULL
;
58 lpTable
->iEntries
= 0;
59 lpTable
->iFirstFree
= 0;
60 InitializeCriticalSection(&lpTable
->mutex
);
61 lpTable
->mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": HANDLETABLE.mutex");
64 /******************************************************************************
65 * destroy_handle_table
67 * Destroys the handle table.
70 * lpTable [I] Pointer to the handle table, which is to be destroyed.
72 void destroy_handle_table(struct handle_table
*lpTable
)
74 TRACE("(lpTable=%p)\n", lpTable
);
76 HeapFree(GetProcessHeap(), 0, lpTable
->paEntries
);
77 lpTable
->mutex
.DebugInfo
->Spare
[0] = 0;
78 DeleteCriticalSection(&lpTable
->mutex
);
81 /******************************************************************************
84 * Tests if handle is valid given the specified handle table
87 * lpTable [I] Pointer to the handle table, with respect to which the handle's
88 * validness is tested.
89 * handle [I] The handle tested for validness.
90 * dwType [I] A magic value that identifies the referenced object's type.
93 * TRUE, if handle is valid.
94 * FALSE, if handle is not valid.
96 BOOL
is_valid_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
98 unsigned int index
= HANDLE2INDEX(handle
);
101 TRACE("(lpTable=%p, handle=%ld)\n", lpTable
, handle
);
103 EnterCriticalSection(&lpTable
->mutex
);
105 /* We don't use zero handle values */
106 if (!handle
) goto exit
;
108 /* Check for index out of table bounds */
109 if (index
>= lpTable
->iEntries
) goto exit
;
111 /* Check if this handle is currently allocated */
112 if (!lpTable
->paEntries
[index
].pObject
) goto exit
;
114 /* Check if this handle references an object of the correct type. */
115 if (lpTable
->paEntries
[index
].pObject
->dwType
!= dwType
) goto exit
;
119 LeaveCriticalSection(&lpTable
->mutex
);
123 /******************************************************************************
124 * grow_handle_table [Internal]
126 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
129 * lpTable [I] Pointer to the table, which is to be grown
132 * TRUE, if successful
133 * FALSE, if not successful (out of memory on process heap)
136 * This is a support function for alloc_handle. Do not call!
138 static BOOL
grow_handle_table(struct handle_table
*lpTable
)
140 struct handle_table_entry
*newEntries
;
141 unsigned int i
, newIEntries
;
143 newIEntries
= lpTable
->iEntries
+ TABLE_SIZE_INCREMENT
;
145 newEntries
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct handle_table_entry
)*newIEntries
);
149 if (lpTable
->paEntries
)
151 memcpy(newEntries
, lpTable
->paEntries
, sizeof(struct handle_table_entry
)*lpTable
->iEntries
);
152 HeapFree(GetProcessHeap(), 0, lpTable
->paEntries
);
155 for (i
=lpTable
->iEntries
; i
<newIEntries
; i
++)
157 newEntries
[i
].pObject
= NULL
;
158 newEntries
[i
].iNextFree
= i
+1;
161 lpTable
->paEntries
= newEntries
;
162 lpTable
->iEntries
= newIEntries
;
167 /******************************************************************************
170 * Allocates a new handle to the specified object in a given handle table.
173 * lpTable [I] Pointer to the handle table, from which the new handle is
175 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
176 * lpHandle [O] Pointer to a handle variable, into which the handle value will
177 * be stored. If not successful, this will be
178 * INVALID_HANDLE_VALUE
180 * TRUE, if successful
181 * FALSE, if not successful (no free handle)
183 static BOOL
alloc_handle(struct handle_table
*lpTable
, OBJECTHDR
*lpObject
, HCRYPTKEY
*lpHandle
)
187 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable
, lpObject
, lpHandle
);
189 EnterCriticalSection(&lpTable
->mutex
);
190 if (lpTable
->iFirstFree
>= lpTable
->iEntries
)
191 if (!grow_handle_table(lpTable
))
193 *lpHandle
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
197 *lpHandle
= INDEX2HANDLE(lpTable
->iFirstFree
);
199 lpTable
->paEntries
[lpTable
->iFirstFree
].pObject
= lpObject
;
200 lpTable
->iFirstFree
= lpTable
->paEntries
[lpTable
->iFirstFree
].iNextFree
;
201 InterlockedIncrement(&lpObject
->refcount
);
205 LeaveCriticalSection(&lpTable
->mutex
);
209 /******************************************************************************
212 * Releases resources occupied by the specified handle in the given table.
213 * The reference count of the handled object is decremented. If it becomes
214 * zero and if the 'destructor' function pointer member is non NULL, the
215 * destructor function will be called. Note that release_handle does not
216 * release resources other than the handle itself. If this is wanted, do it
217 * in the destructor function.
220 * lpTable [I] Pointer to the handle table, from which a handle is to be
222 * handle [I] The handle, which is to be released
223 * dwType [I] Identifier for the type of the object, for which a handle is
227 * TRUE, if successful
228 * FALSE, if not successful (invalid handle)
230 BOOL
release_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
232 unsigned int index
= HANDLE2INDEX(handle
);
236 TRACE("(lpTable=%p, handle=%ld)\n", lpTable
, handle
);
238 EnterCriticalSection(&lpTable
->mutex
);
240 if (!is_valid_handle(lpTable
, handle
, dwType
))
243 pObject
= lpTable
->paEntries
[index
].pObject
;
244 if (InterlockedDecrement(&pObject
->refcount
) == 0)
246 TRACE("destroying handle %ld\n", handle
);
247 if (pObject
->destructor
)
248 pObject
->destructor(pObject
);
251 lpTable
->paEntries
[index
].pObject
= NULL
;
252 lpTable
->paEntries
[index
].iNextFree
= lpTable
->iFirstFree
;
253 lpTable
->iFirstFree
= index
;
257 LeaveCriticalSection(&lpTable
->mutex
);
261 /******************************************************************************
264 * Returns the object identified by the handle in the given handle table
267 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
268 * handle [I] The handle, which is to be looked up
269 * lplpObject [O] Pointer to the variable, into which the pointer to the
270 * object looked up is copied.
272 * TRUE, if successful
273 * FALSE, if not successful (invalid handle)
275 BOOL
lookup_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, OBJECTHDR
**lplpObject
)
279 TRACE("(lpTable=%p, handle=%ld, lplpObject=%p)\n", lpTable
, handle
, lplpObject
);
281 EnterCriticalSection(&lpTable
->mutex
);
282 if (!is_valid_handle(lpTable
, handle
, dwType
))
287 *lplpObject
= lpTable
->paEntries
[HANDLE2INDEX(handle
)].pObject
;
291 LeaveCriticalSection(&lpTable
->mutex
);
295 /******************************************************************************
298 * Copies a handle. Increments the reference count of the object referenced
302 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
303 * handle [I] The handle to be copied.
304 * copy [O] Pointer to a handle variable, where the copied handle is put.
307 * TRUE, if successful
308 * FALSE, if not successful (invalid handle or out of memory)
310 BOOL
copy_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, HCRYPTKEY
*copy
)
315 TRACE("(lpTable=%p, handle=%ld, copy=%p)\n", lpTable
, handle
, copy
);
317 EnterCriticalSection(&lpTable
->mutex
);
318 if (!lookup_handle(lpTable
, handle
, dwType
, &pObject
))
320 *copy
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
321 LeaveCriticalSection(&lpTable
->mutex
);
325 ret
= alloc_handle(lpTable
, pObject
, copy
);
326 LeaveCriticalSection(&lpTable
->mutex
);
330 /******************************************************************************
333 * Allocates a new object of size cbSize on the current process's heap.
334 * Initializes the object header using the destructor and dwType params.
335 * Allocates a handle to the object in the handle table pointed to by lpTable.
336 * Returns a pointer to the created object in ppObject.
337 * Returns a handle to the created object.
340 * lpTable [I] Pointer to the handle table, from which a handle is to be
342 * cbSize [I] Size of the object to be allocated in bytes.
343 * dwType [I] Object type; will be copied to the object header.
344 * destructor [I] Function pointer to a destructor function. Will be called
345 * once the object's reference count gets zero.
346 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
347 * created object will be stored. You may set this to NULL.
350 * INVALID_HANDLE_VALUE, if something went wrong.
351 * a handle to the new object, if successful.
353 HCRYPTKEY
new_object(struct handle_table
*lpTable
, size_t cbSize
, DWORD dwType
, DESTRUCTOR destructor
,
354 OBJECTHDR
**ppObject
)
362 pObject
= HeapAlloc(GetProcessHeap(), 0, cbSize
);
364 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
366 pObject
->dwType
= dwType
;
367 pObject
->refcount
= 0;
368 pObject
->destructor
= destructor
;
370 if (!alloc_handle(lpTable
, pObject
, &hObject
))
371 HeapFree(GetProcessHeap(), 0, pObject
);