Implemented a substantial part of rsaenh.dll.
[wine/hacks.git] / dlls / rsaenh / handle.c
blob7b2b8177774d8ac289f5ace7db00d07d3718c457
1 /*
2 * dlls/rsaenh/handle.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <string.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "handle.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(handle);
35 #define HANDLE2INDEX(h) ((h)-1)
36 #define INDEX2HANDLE(i) ((i)+1)
38 /******************************************************************************
39 * init_handle_table
41 * Initializes the HANDLETABLE structure pointed to by lpTable
43 * PARAMS
44 * lpTable [I] Pointer to the HANDLETABLE structure, which is to be initalized.
46 * NOTES
47 * Note that alloc_handle_table calls init_handle_table on it's own, which
48 * means that you only have to call init_handle_table, if you use a global
49 * variable of type HANDLETABLE for your handle table. However, in this
50 * case you have to call destroy_handle_table when you don't need the table
51 * any more.
53 void init_handle_table(HANDLETABLE *lpTable)
55 TRACE("(lpTable=%p)\n", lpTable);
57 lpTable->paEntries = NULL;
58 lpTable->iEntries = 0;
59 lpTable->iFirstFree = 0;
60 InitializeCriticalSection(&lpTable->mutex);
63 /******************************************************************************
64 * destroy_handle_table
66 * Destroys the handle table.
68 * PARAMS
69 * lpTable [I] Pointer to the handle table, which is to be destroyed.
71 * NOTES
72 * Note that release_handle_table takes care of this.
74 void destroy_handle_table(HANDLETABLE *lpTable)
76 TRACE("(lpTable=%p)\n", lpTable);
78 if (lpTable->paEntries)
79 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
80 DeleteCriticalSection(&lpTable->mutex);
83 /******************************************************************************
84 * is_valid_handle
86 * Tests if handle is valid given the specified handle table
88 * PARAMS
89 * lpTable [I] Pointer to the handle table, with respect to which the handle's
90 * validness is tested.
91 * handle [I] The handle tested for validness.
92 * dwType [I] A magic value that identifies the referenced object's type.
94 * RETURNS
95 * non zero, if handle is valid.
96 * zero, if handle is not valid.
98 int is_valid_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
100 unsigned int index = HANDLE2INDEX(handle);
101 int ret = 0;
103 TRACE("(lpTable=%p, handle=%d)\n", lpTable, handle);
105 EnterCriticalSection(&lpTable->mutex);
107 /* We don't use zero handle values */
108 if (!handle) goto exit;
110 /* Check for index out of table bounds */
111 if (index >= lpTable->iEntries) goto exit;
113 /* Check if this handle is currently allocated */
114 if (!lpTable->paEntries[index].pObject) goto exit;
116 /* Check if this handle references an object of the correct type. */
117 if (lpTable->paEntries[index].pObject->dwType != dwType) goto exit;
119 ret = 1;
120 exit:
121 LeaveCriticalSection(&lpTable->mutex);
122 return ret;
125 /******************************************************************************
126 * alloc_handle_table
128 * Allocates a new handle table
130 * PARAMS
131 * lplpTable [O] Pointer to the variable, to which the pointer to the newly
132 * allocated handle table is written.
133 * RETURNS
134 * non zero, if successfull
135 * zero, if not successfull (out of process heap memory)
137 * NOTES
138 * If all you need is a single handle table, you may as well declare a global
139 * variable of type HANDLETABLE and call init_handle_table on your own.
141 int alloc_handle_table(HANDLETABLE **lplpTable)
143 TRACE("(lplpTable=%p)\n", lplpTable);
145 *lplpTable = HeapAlloc(GetProcessHeap(), 0, sizeof(HANDLETABLE));
146 if (*lplpTable)
148 init_handle_table(*lplpTable);
149 return 1;
151 else
152 return 0;
155 /******************************************************************************
156 * release_handle_table
158 * Releases a handle table and frees the resources occupied by it.
160 * PARAMS
161 * lpTable [I] Pointer to the handle table, which is to be released.
163 * RETURNS
164 * non zero, if successfull
165 * zero, if not successfull
167 * NOTES
168 * All valid handles still in the table are released also.
170 int release_handle_table(HANDLETABLE *lpTable)
172 TRACE("(lpTable=%p)\n", lpTable);
174 release_all_handles(lpTable);
175 destroy_handle_table(lpTable);
176 return (int)HeapFree(GetProcessHeap(), 0, lpTable);
179 /******************************************************************************
180 * grow_handle_table [Internal]
182 * Grows the number of entries in the given table by TABLE_SIZE_INCREMENT
184 * PARAMS
185 * lpTable [I] Pointer to the table, which is to be grown
187 * RETURNS
188 * non zero, if successfull
189 * zero, if not successfull (out of memory on process heap)
191 * NOTES
192 * This is a support function for alloc_handle. Do not call!
194 static int grow_handle_table(HANDLETABLE *lpTable)
196 HANDLETABLEENTRY *newEntries;
197 unsigned int i, newIEntries;
199 newIEntries = lpTable->iEntries + TABLE_SIZE_INCREMENT;
201 newEntries = (HANDLETABLEENTRY*)HeapAlloc(GetProcessHeap(), 0,
202 sizeof(HANDLETABLEENTRY)*newIEntries);
203 if (!newEntries)
204 return 0;
206 if (lpTable->paEntries)
208 memcpy(newEntries, lpTable->paEntries, sizeof(HANDLETABLEENTRY)*lpTable->iEntries);
209 HeapFree(GetProcessHeap(), 0, lpTable->paEntries);
212 for (i=lpTable->iEntries; i<newIEntries; i++)
214 newEntries[i].pObject = NULL;
215 newEntries[i].iNextFree = i+1;
218 lpTable->paEntries = newEntries;
219 lpTable->iEntries = newIEntries;
221 return 1;
224 /******************************************************************************
225 * alloc_handle
227 * Allocates a new handle to the specified object in a given handle table.
229 * PARAMS
230 * lpTable [I] Pointer to the handle table, from which the new handle is
231 * allocated.
232 * lpObject [I] Pointer to the object, for which a handle shall be allocated.
233 * lpHandle [O] Pointer to a handle variable, into which the handle value will
234 * be stored. If not successfull, this will be
235 * INVALID_HANDLE_VALUE
236 * RETURNS
237 * non zero, if successfull
238 * zero, if not successfull (no free handle)
240 int alloc_handle(HANDLETABLE *lpTable, OBJECTHDR *lpObject, unsigned int *lpHandle)
242 int ret = 0;
244 TRACE("(lpTable=%p, lpObject=%p, lpHandle=%p)\n", lpTable, lpObject, lpHandle);
246 EnterCriticalSection(&lpTable->mutex);
247 if (lpTable->iFirstFree >= lpTable->iEntries)
248 if (!grow_handle_table(lpTable))
250 *lpHandle = (unsigned int)INVALID_HANDLE_VALUE;
251 goto exit;
254 *lpHandle = INDEX2HANDLE(lpTable->iFirstFree);
256 lpTable->paEntries[lpTable->iFirstFree].pObject = lpObject;
257 lpTable->iFirstFree = lpTable->paEntries[lpTable->iFirstFree].iNextFree;
258 lpObject->refcount++;
260 ret = 1;
261 exit:
262 LeaveCriticalSection(&lpTable->mutex);
263 return ret;
266 /******************************************************************************
267 * release_handle
269 * Releases resources occupied by the specified handle in the given table.
270 * The reference count of the handled object is decremented. If it becomes
271 * zero and if the 'destructor' function pointer member is non NULL, the
272 * destructor function will be called. Note that release_handle does not
273 * release resources other than the handle itself. If this is wanted, do it
274 * in the destructor function.
276 * PARAMS
277 * lpTable [I] Pointer to the handle table, from which a handle is to be
278 * released.
279 * handle [I] The handle, which is to be released
280 * dwType [I] Identifier for the type of the object, for which a handle is
281 * to be released.
283 * RETURNS
284 * non zero, if successfull
285 * zero, if not successfull (invalid handle)
287 int release_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType)
289 unsigned int index = HANDLE2INDEX(handle);
290 OBJECTHDR *pObject;
291 int ret = 0;
293 TRACE("(lpTable=%p, hande=%d)\n", lpTable, handle);
295 EnterCriticalSection(&lpTable->mutex);
297 if (!is_valid_handle(lpTable, handle, dwType))
298 goto exit;
300 pObject = lpTable->paEntries[index].pObject;
301 pObject->refcount--;
302 if (pObject->refcount == 0)
303 if (pObject->destructor)
304 pObject->destructor(pObject);
306 lpTable->paEntries[index].pObject = NULL;
307 lpTable->paEntries[index].iNextFree = lpTable->iFirstFree;
308 lpTable->iFirstFree = index;
310 ret = 1;
311 exit:
312 LeaveCriticalSection(&lpTable->mutex);
313 return ret;
316 /******************************************************************************
317 * release_all_handles
319 * Releases all valid handles in the given handle table and shrinks the table
320 * to zero size.
322 * PARAMS
323 * lpTable [I] The table of which all valid handles shall be released.
325 void release_all_handles(HANDLETABLE *lpTable)
327 unsigned int i;
329 TRACE("(lpTable=%p)\n", lpTable);
331 EnterCriticalSection(&lpTable->mutex);
332 for (i=0; i<lpTable->iEntries; i++)
333 if (lpTable->paEntries[i].pObject)
334 release_handle(lpTable, lpTable->paEntries[i].pObject->dwType, INDEX2HANDLE(i));
335 LeaveCriticalSection(&lpTable->mutex);
338 /******************************************************************************
339 * lookup_handle
341 * Returns the object identified by the handle in the given handle table
343 * PARAMS
344 * lpTable [I] Pointer to the handle table, in which the handle is looked up.
345 * handle [I] The handle, which is to be looked up
346 * lplpObject [O] Pointer to the variable, into which the pointer to the
347 * object looked up is copied.
348 * RETURNS
349 * non zero, if successfull
350 * zero, if not successfull (invalid handle)
352 int lookup_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, OBJECTHDR **lplpObject)
354 int ret = 0;
356 TRACE("(lpTable=%p, handle=%d, lplpObject=%p)\n", lpTable, handle, lplpObject);
358 EnterCriticalSection(&lpTable->mutex);
359 if (!is_valid_handle(lpTable, handle, dwType))
361 *lplpObject = NULL;
362 goto exit;
364 *lplpObject = lpTable->paEntries[HANDLE2INDEX(handle)].pObject;
366 ret = 1;
367 exit:
368 LeaveCriticalSection(&lpTable->mutex);
369 return ret;
372 /******************************************************************************
373 * copy_handle
375 * Copies a handle. Increments reference count in the object referenced by the
376 * handle
378 * PARAMS
379 * lpTable [I] Pointer to the handle table, which holds the handle to be copied.
380 * handle [I] The handle to be copied.
381 * copy [O] Pointer to a handle variable, where the copied handle is put.
383 * RETURNS
384 * non zero, if successfull
385 * zero, if not successfull (invalid handle or out of memory)
387 int copy_handle(HANDLETABLE *lpTable, unsigned int handle, DWORD dwType, unsigned int *copy)
389 OBJECTHDR *pObject;
390 int ret;
392 TRACE("(lpTable=%p, handle=%d, copy=%p)\n", lpTable, handle, copy);
394 EnterCriticalSection(&lpTable->mutex);
395 if (!lookup_handle(lpTable, handle, dwType, &pObject))
397 *copy = (unsigned int)INVALID_HANDLE_VALUE;
398 LeaveCriticalSection(&lpTable->mutex);
399 return 0;
402 ret = alloc_handle(lpTable, pObject, copy);
403 LeaveCriticalSection(&lpTable->mutex);
404 return ret;
407 /******************************************************************************
408 * new_object
410 * Allocates a new object of size cbSize on the current process's heap.
411 * Initializes the object header using the destructor and dwType params.
412 * Allocates a handle to the object in the handle table pointed to by lpTable.
413 * Returns a pointer to the created object in ppObject.
414 * Returns a handle to the created object.
416 * PARAMS
417 * lpTable [I] Pointer to the handle table, from which a handle is to be
418 * allocated.
419 * cbSize [I] Size of the object to be allocated in bytes.
420 * dwType [I] Object type; will be copied to the object header.
421 * destructor [I] Function pointer to a destructor function. Will be called
422 * once the object's reference count gets zero.
423 * ppObject [O] Pointer to a pointer variable, where a pointer to the newly
424 * created object will be stored. You may set this to NULL.
426 * RETURNS
427 * INVALID_HANDLE_VALUE, if something went wrong.
428 * a handle to the new object, if successfull.
430 unsigned int new_object(HANDLETABLE *lpTable, size_t cbSize, DWORD dwType, DESTRUCTOR destructor,
431 OBJECTHDR **ppObject)
433 OBJECTHDR *pObject;
434 unsigned int hObject;
436 if (ppObject)
437 *ppObject = NULL;
439 pObject = (OBJECTHDR*)HeapAlloc(GetProcessHeap(), 0, cbSize);
440 if (!pObject)
441 return (unsigned int)INVALID_HANDLE_VALUE;
443 pObject->dwType = dwType;
444 pObject->refcount = 0;
445 pObject->destructor = destructor;
447 if (!alloc_handle(lpTable, pObject, &hObject))
448 HeapFree(GetProcessHeap(), 0, pObject);
449 else
450 if (ppObject)
451 *ppObject = pObject;
453 return hObject;