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
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(handle
);
36 #define HANDLE2INDEX(h) ((h)-1)
37 #define INDEX2HANDLE(i) ((i)+1)
39 /******************************************************************************
42 * Initializes the HANDLETABLE structure pointed to by lpTable
45 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initialized.
48 * You have to call destroy_handle_table when you don't need the table
51 void init_handle_table(struct handle_table
*lpTable
)
53 TRACE("(lpTable=%p)\n", lpTable
);
55 lpTable
->paEntries
= NULL
;
56 lpTable
->iEntries
= 0;
57 lpTable
->iFirstFree
= 0;
58 InitializeCriticalSection(&lpTable
->mutex
);
59 lpTable
->mutex
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": HANDLETABLE.mutex");
62 /******************************************************************************
63 * destroy_handle_table
65 * Destroys the handle table.
68 * lpTable [I] Pointer to the handle table, which is to be destroyed.
70 void destroy_handle_table(struct handle_table
*lpTable
)
72 TRACE("(lpTable=%p)\n", lpTable
);
74 free(lpTable
->paEntries
);
75 lpTable
->mutex
.DebugInfo
->Spare
[0] = 0;
76 DeleteCriticalSection(&lpTable
->mutex
);
79 /******************************************************************************
82 * Tests if handle is valid given the specified handle table
85 * lpTable [I] Pointer to the handle table, with respect to which the handle's
86 * validness is tested.
87 * handle [I] The handle tested for validness.
88 * dwType [I] A magic value that identifies the referenced object's type.
91 * TRUE, if handle is valid.
92 * FALSE, if handle is not valid.
94 BOOL
is_valid_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
96 unsigned int index
= HANDLE2INDEX(handle
);
99 TRACE("(lpTable=%p, handle=%Id)\n", lpTable
, handle
);
101 EnterCriticalSection(&lpTable
->mutex
);
103 /* We don't use zero handle values */
104 if (!handle
) goto exit
;
106 /* Check for index out of table bounds */
107 if (index
>= lpTable
->iEntries
) goto exit
;
109 /* Check if this handle is currently allocated */
110 if (!lpTable
->paEntries
[index
].pObject
) goto exit
;
112 /* Check if this handle references an object of the correct type. */
113 if (lpTable
->paEntries
[index
].pObject
->dwType
!= dwType
) goto exit
;
117 LeaveCriticalSection(&lpTable
->mutex
);
121 /******************************************************************************
122 * grow_handle_table [Internal]
124 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
127 * lpTable [I] Pointer to the table, which is to be grown
130 * TRUE, if successful
131 * FALSE, if not successful (out of memory on process heap)
134 * This is a support function for alloc_handle. Do not call!
136 static BOOL
grow_handle_table(struct handle_table
*lpTable
)
138 struct handle_table_entry
*newEntries
;
139 unsigned int i
, newIEntries
;
141 newIEntries
= lpTable
->iEntries
+ TABLE_SIZE_INCREMENT
;
143 newEntries
= malloc(sizeof(struct handle_table_entry
)*newIEntries
);
147 if (lpTable
->paEntries
)
149 memcpy(newEntries
, lpTable
->paEntries
, sizeof(struct handle_table_entry
)*lpTable
->iEntries
);
150 free(lpTable
->paEntries
);
153 for (i
=lpTable
->iEntries
; i
<newIEntries
; i
++)
155 newEntries
[i
].pObject
= NULL
;
156 newEntries
[i
].iNextFree
= i
+1;
159 lpTable
->paEntries
= newEntries
;
160 lpTable
->iEntries
= newIEntries
;
165 /******************************************************************************
168 * Allocates a new handle to the specified object in a given handle table.
171 * lpTable [I] Pointer to the handle table, from which the new handle is
173 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
174 * lpHandle [O] Pointer to a handle variable, into which the handle value will
175 * be stored. If not successful, this will be
176 * INVALID_HANDLE_VALUE
178 * TRUE, if successful
179 * FALSE, if not successful (no free handle)
181 static BOOL
alloc_handle(struct handle_table
*lpTable
, OBJECTHDR
*lpObject
, HCRYPTKEY
*lpHandle
)
185 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable
, lpObject
, lpHandle
);
187 EnterCriticalSection(&lpTable
->mutex
);
188 if (lpTable
->iFirstFree
>= lpTable
->iEntries
)
189 if (!grow_handle_table(lpTable
))
191 *lpHandle
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
195 *lpHandle
= INDEX2HANDLE(lpTable
->iFirstFree
);
197 lpTable
->paEntries
[lpTable
->iFirstFree
].pObject
= lpObject
;
198 lpTable
->iFirstFree
= lpTable
->paEntries
[lpTable
->iFirstFree
].iNextFree
;
199 InterlockedIncrement(&lpObject
->refcount
);
203 LeaveCriticalSection(&lpTable
->mutex
);
207 /******************************************************************************
210 * Releases resources occupied by the specified handle in the given table.
211 * The reference count of the handled object is decremented. If it becomes
212 * zero and if the 'destructor' function pointer member is non NULL, the
213 * destructor function will be called. Note that release_handle does not
214 * release resources other than the handle itself. If this is wanted, do it
215 * in the destructor function.
218 * lpTable [I] Pointer to the handle table, from which a handle is to be
220 * handle [I] The handle, which is to be released
221 * dwType [I] Identifier for the type of the object, for which a handle is
225 * TRUE, if successful
226 * FALSE, if not successful (invalid handle)
228 BOOL
release_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
)
230 unsigned int index
= HANDLE2INDEX(handle
);
234 TRACE("(lpTable=%p, handle=%Id)\n", lpTable
, handle
);
236 EnterCriticalSection(&lpTable
->mutex
);
238 if (!is_valid_handle(lpTable
, handle
, dwType
))
241 pObject
= lpTable
->paEntries
[index
].pObject
;
242 if (InterlockedDecrement(&pObject
->refcount
) == 0)
244 TRACE("destroying handle %Id\n", handle
);
245 if (pObject
->destructor
)
246 pObject
->destructor(pObject
);
249 lpTable
->paEntries
[index
].pObject
= NULL
;
250 lpTable
->paEntries
[index
].iNextFree
= lpTable
->iFirstFree
;
251 lpTable
->iFirstFree
= index
;
255 LeaveCriticalSection(&lpTable
->mutex
);
259 /******************************************************************************
262 * Returns the object identified by the handle in the given handle table
265 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
266 * handle [I] The handle, which is to be looked up
267 * lplpObject [O] Pointer to the variable, into which the pointer to the
268 * object looked up is copied.
270 * TRUE, if successful
271 * FALSE, if not successful (invalid handle)
273 BOOL
lookup_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, OBJECTHDR
**lplpObject
)
277 TRACE("(lpTable=%p, handle=%Id, lplpObject=%p)\n", lpTable
, handle
, lplpObject
);
279 EnterCriticalSection(&lpTable
->mutex
);
280 if (!is_valid_handle(lpTable
, handle
, dwType
))
285 *lplpObject
= lpTable
->paEntries
[HANDLE2INDEX(handle
)].pObject
;
289 LeaveCriticalSection(&lpTable
->mutex
);
293 /******************************************************************************
296 * Copies a handle. Increments the reference count of the object referenced
300 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
301 * handle [I] The handle to be copied.
302 * copy [O] Pointer to a handle variable, where the copied handle is put.
305 * TRUE, if successful
306 * FALSE, if not successful (invalid handle or out of memory)
308 BOOL
copy_handle(struct handle_table
*lpTable
, HCRYPTKEY handle
, DWORD dwType
, HCRYPTKEY
*copy
)
313 TRACE("(lpTable=%p, handle=%Id, copy=%p)\n", lpTable
, handle
, copy
);
315 EnterCriticalSection(&lpTable
->mutex
);
316 if (!lookup_handle(lpTable
, handle
, dwType
, &pObject
))
318 *copy
= (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
319 LeaveCriticalSection(&lpTable
->mutex
);
323 ret
= alloc_handle(lpTable
, pObject
, copy
);
324 LeaveCriticalSection(&lpTable
->mutex
);
328 /******************************************************************************
331 * Allocates a new object of size cbSize on the current process's heap.
332 * Initializes the object header using the destructor and dwType params.
333 * Allocates a handle to the object in the handle table pointed to by lpTable.
334 * Returns a pointer to the created object in ppObject.
335 * Returns a handle to the created object.
338 * lpTable [I] Pointer to the handle table, from which a handle is to be
340 * cbSize [I] Size of the object to be allocated in bytes.
341 * dwType [I] Object type; will be copied to the object header.
342 * destructor [I] Function pointer to a destructor function. Will be called
343 * once the object's reference count gets zero.
344 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
345 * created object will be stored. You may set this to NULL.
348 * INVALID_HANDLE_VALUE, if something went wrong.
349 * a handle to the new object, if successful.
351 HCRYPTKEY
new_object(struct handle_table
*lpTable
, size_t cbSize
, DWORD dwType
, DESTRUCTOR destructor
,
352 OBJECTHDR
**ppObject
)
360 pObject
= malloc(cbSize
);
362 return (HCRYPTKEY
)INVALID_HANDLE_VALUE
;
364 pObject
->dwType
= dwType
;
365 pObject
->refcount
= 0;
366 pObject
->destructor
= destructor
;
368 if (!alloc_handle(lpTable
, pObject
, &hObject
))