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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(handle
);
35 #define HANDLE2INDEX(h) ((h)-1)
36 #define INDEX2HANDLE(i) ((i)+1)
38 /******************************************************************************
41 * Initializes the HANDLETABLE structure pointed to by lpTable
44 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initialized.
47 * You have to call destroy_handle_table when you don't need the table
50 void init_handle_table(struct handle_table
*lpTable
)
52 TRACE("(lpTable=%p)\n", lpTable
);
54 lpTable
->paEntries
= NULL
;
55 lpTable
->iEntries
= 0;
56 lpTable
->iFirstFree
= 0;
57 InitializeCriticalSection(&lpTable
->mutex
);
58 lpTable
->mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": HANDLETABLE.mutex");
61 /******************************************************************************
62 * destroy_handle_table
64 * Destroys the handle table.
67 * lpTable [I] Pointer to the handle table, which is to be destroyed.
69 void destroy_handle_table(struct handle_table
*lpTable
)
71 TRACE("(lpTable=%p)\n", lpTable
);
73 HeapFree(GetProcessHeap(), 0, lpTable
->paEntries
);
74 lpTable
->mutex
.DebugInfo
->Spare
[0] = 0;
75 DeleteCriticalSection(&lpTable
->mutex
);
78 /******************************************************************************
81 * Tests if handle is valid given the specified handle table
84 * lpTable [I] Pointer to the handle table, with respect to which the handle's
85 * validness is tested.
86 * handle [I] The handle tested for validness.
87 * dwType [I] A magic value that identifies the referenced object's type.
90 * non zero, if handle is valid.
91 * zero, if handle is not valid.
93 int is_valid_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
95 unsigned int index
= HANDLE2INDEX(handle
);
98 TRACE("(lpTable=%p, handle=%ld)\n", lpTable
, handle
);
100 EnterCriticalSection(&lpTable
->mutex
);
102 /* We don't use zero handle values */
103 if (!handle
) goto exit
;
105 /* Check for index out of table bounds */
106 if (index
>= lpTable
->iEntries
) goto exit
;
108 /* Check if this handle is currently allocated */
109 if (!lpTable
->paEntries
[index
].pObject
) goto exit
;
111 /* Check if this handle references an object of the correct type. */
112 if (lpTable
->paEntries
[index
].pObject
->dwType
!= dwType
) goto exit
;
116 LeaveCriticalSection(&lpTable
->mutex
);
120 /******************************************************************************
121 * grow_handle_table [Internal]
123 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
126 * lpTable [I] Pointer to the table, which is to be grown
129 * non zero, if successful
130 * zero, if not successful (out of memory on process heap)
133 * This is a support function for alloc_handle. Do not call!
135 static int grow_handle_table(struct handle_table
*lpTable
)
137 struct handle_table_entry
*newEntries
;
138 unsigned int i
, newIEntries
;
140 newIEntries
= lpTable
->iEntries
+ TABLE_SIZE_INCREMENT
;
142 newEntries
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct handle_table_entry
)*newIEntries
);
146 if (lpTable
->paEntries
)
148 memcpy(newEntries
, lpTable
->paEntries
, sizeof(struct handle_table_entry
)*lpTable
->iEntries
);
149 HeapFree(GetProcessHeap(), 0, lpTable
->paEntries
);
152 for (i
=lpTable
->iEntries
; i
<newIEntries
; i
++)
154 newEntries
[i
].pObject
= NULL
;
155 newEntries
[i
].iNextFree
= i
+1;
158 lpTable
->paEntries
= newEntries
;
159 lpTable
->iEntries
= newIEntries
;
164 /******************************************************************************
167 * Allocates a new handle to the specified object in a given handle table.
170 * lpTable [I] Pointer to the handle table, from which the new handle is
172 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
173 * lpHandle [O] Pointer to a handle variable, into which the handle value will
174 * be stored. If not successful, this will be
175 * INVALID_HANDLE_VALUE
177 * non zero, if successful
178 * zero, if not successful (no free handle)
180 static int alloc_handle(struct handle_table
*lpTable
, OBJECTHDR
*lpObject
, HCRYPTKEY
*lpHandle
)
184 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable
, lpObject
, lpHandle
);
186 EnterCriticalSection(&lpTable
->mutex
);
187 if (lpTable
->iFirstFree
>= lpTable
->iEntries
)
188 if (!grow_handle_table(lpTable
))
190 *lpHandle
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
194 *lpHandle
= INDEX2HANDLE(lpTable
->iFirstFree
);
196 lpTable
->paEntries
[lpTable
->iFirstFree
].pObject
= lpObject
;
197 lpTable
->iFirstFree
= lpTable
->paEntries
[lpTable
->iFirstFree
].iNextFree
;
198 InterlockedIncrement(&lpObject
->refcount
);
202 LeaveCriticalSection(&lpTable
->mutex
);
206 /******************************************************************************
209 * Releases resources occupied by the specified handle in the given table.
210 * The reference count of the handled object is decremented. If it becomes
211 * zero and if the 'destructor' function pointer member is non NULL, the
212 * destructor function will be called. Note that release_handle does not
213 * release resources other than the handle itself. If this is wanted, do it
214 * in the destructor function.
217 * lpTable [I] Pointer to the handle table, from which a handle is to be
219 * handle [I] The handle, which is to be released
220 * dwType [I] Identifier for the type of the object, for which a handle is
224 * non zero, if successful
225 * zero, if not successful (invalid handle)
227 int release_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
229 unsigned int index
= HANDLE2INDEX(handle
);
233 TRACE("(lpTable=%p, handle=%ld)\n", lpTable
, handle
);
235 EnterCriticalSection(&lpTable
->mutex
);
237 if (!is_valid_handle(lpTable
, handle
, dwType
))
240 pObject
= lpTable
->paEntries
[index
].pObject
;
241 if (InterlockedDecrement(&pObject
->refcount
) == 0)
243 TRACE("destroying handle %ld\n", handle
);
244 if (pObject
->destructor
)
245 pObject
->destructor(pObject
);
248 lpTable
->paEntries
[index
].pObject
= NULL
;
249 lpTable
->paEntries
[index
].iNextFree
= lpTable
->iFirstFree
;
250 lpTable
->iFirstFree
= index
;
254 LeaveCriticalSection(&lpTable
->mutex
);
258 /******************************************************************************
261 * Returns the object identified by the handle in the given handle table
264 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
265 * handle [I] The handle, which is to be looked up
266 * lplpObject [O] Pointer to the variable, into which the pointer to the
267 * object looked up is copied.
269 * non zero, if successful
270 * zero, if not successful (invalid handle)
272 int lookup_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, OBJECTHDR
**lplpObject
)
276 TRACE("(lpTable=%p, handle=%ld, lplpObject=%p)\n", lpTable
, handle
, lplpObject
);
278 EnterCriticalSection(&lpTable
->mutex
);
279 if (!is_valid_handle(lpTable
, handle
, dwType
))
284 *lplpObject
= lpTable
->paEntries
[HANDLE2INDEX(handle
)].pObject
;
288 LeaveCriticalSection(&lpTable
->mutex
);
292 /******************************************************************************
295 * Copies a handle. Increments the reference count of the object referenced
299 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
300 * handle [I] The handle to be copied.
301 * copy [O] Pointer to a handle variable, where the copied handle is put.
304 * non zero, if successful
305 * zero, if not successful (invalid handle or out of memory)
307 int copy_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, HCRYPTKEY
*copy
)
312 TRACE("(lpTable=%p, handle=%ld, copy=%p)\n", lpTable
, handle
, copy
);
314 EnterCriticalSection(&lpTable
->mutex
);
315 if (!lookup_handle(lpTable
, handle
, dwType
, &pObject
))
317 *copy
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
318 LeaveCriticalSection(&lpTable
->mutex
);
322 ret
= alloc_handle(lpTable
, pObject
, copy
);
323 LeaveCriticalSection(&lpTable
->mutex
);
327 /******************************************************************************
330 * Allocates a new object of size cbSize on the current process's heap.
331 * Initializes the object header using the destructor and dwType params.
332 * Allocates a handle to the object in the handle table pointed to by lpTable.
333 * Returns a pointer to the created object in ppObject.
334 * Returns a handle to the created object.
337 * lpTable [I] Pointer to the handle table, from which a handle is to be
339 * cbSize [I] Size of the object to be allocated in bytes.
340 * dwType [I] Object type; will be copied to the object header.
341 * destructor [I] Function pointer to a destructor function. Will be called
342 * once the object's reference count gets zero.
343 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
344 * created object will be stored. You may set this to NULL.
347 * INVALID_HANDLE_VALUE, if something went wrong.
348 * a handle to the new object, if successful.
350 HCRYPTKEY
new_object(struct handle_table
*lpTable
, size_t cbSize
, DWORD dwType
, DESTRUCTOR destructor
,
351 OBJECTHDR
**ppObject
)
359 pObject
= HeapAlloc(GetProcessHeap(), 0, cbSize
);
361 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
363 pObject
->dwType
= dwType
;
364 pObject
->refcount
= 0;
365 pObject
->destructor
= destructor
;
367 if (!alloc_handle(lpTable
, pObject
, &hObject
))
368 HeapFree(GetProcessHeap(), 0, pObject
);