VirtualBox

root/trunk/include/iprt/handletable.h

Revision 10788, 9.6 kB (checked in by vboxsync, 4 months ago)

IPRT: Implemented the context variant of the handle table.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /** @file
2  * IPRT - Handle Tables.
3  */
4
5 /*
6  * Copyright (C) 2008 Sun Microsystems, Inc.
7  *
8  * This file is part of VirtualBox Open Source Edition (OSE), as
9  * available from http://www.virtualbox.org. This file is free software;
10  * you can redistribute it and/or modify it under the terms of the GNU
11  * General Public License (GPL) as published by the Free Software
12  * Foundation, in version 2 as it comes in the "COPYING" file of the
13  * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14  * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15  *
16  * The contents of this file may alternatively be used under the terms
17  * of the Common Development and Distribution License Version 1.0
18  * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19  * VirtualBox OSE distribution, in which case the provisions of the
20  * CDDL are applicable instead of those of the GPL.
21  *
22  * You may elect to license modified versions of this file under the
23  * terms and conditions of either the GPL or the CDDL or both.
24  *
25  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26  * Clara, CA 95054 USA or visit http://www.sun.com if you need
27  * additional information or have any questions.
28  */
29
30 #ifndef ___iprt_handletable_h
31 #define ___iprt_handletable_h
32
33 #include <iprt/cdefs.h>
34 #include <iprt/types.h>
35
36 __BEGIN_DECLS
37
38 /** @defgroup grp_rt_handletable    RTHandleTable - Handle Tables
39  * @ingroup grp_rt
40  * @{
41  */
42
43 /**
44  * Callback for retaining an object during the lookup and free calls.
45  *
46  * This callback is executed when a handle is being looked up in one
47  * way or another from behind the handle table lock. This allows you
48  * to increase the reference (or some equivalent thing) during the
49  * handle lookup and thereby eliminate any race with anyone trying
50  * to free the handle.
51  *
52  * Note that there is no counterpart to this callback, so if you make
53  * use of this you'll have to release the object manually of course.
54  *
55  * Another use of this callback is to do some extra access checking.
56  * Use the return code to indicate whether the lookup should fail
57  * or not (no object is returned on faliure, naturally).
58  *
59  * @returns IPRT status code for the lookup (the caller won't see this).
60  *
61  * @param   hHandleTable    The handle table handle.
62  * @param   pvObj           The object which has been looked up.
63  * @param   pvCtx           The context argument if the handle table was created with the
64  *                          RTHANDLETABLE_FLAGS_CONTEXT set. Otherwise NULL.
65  * @param   pvUser          The user context argument specified when creating the table.
66  */
67 typedef DECLCALLBACK(int) FNRTHANDLETABLERETAIN(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser);
68 /** Pointer to a FNHANDLETABLERETAIN. */
69 typedef FNRTHANDLETABLERETAIN *PFNRTHANDLETABLERETAIN;
70
71 /**
72  * Callback for deleting a left over object during RTHandleTableDestroy.
73  *
74  * @param   hHandleTable    The handle table handle.
75  * @param   h               The handle.
76  * @param   pvObj           The object.
77  * @param   pvCtx           The context argument if the handle table was created with the
78  *                          RTHANDLETABLE_FLAGS_CONTEXT set. Otherwise NULL.
79  * @param   pvUser          The user context argument specified when creating the table.
80  *
81  */
82 typedef DECLCALLBACK(void) FNRTHANDLETABLEDELETE(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser);
83 /** Pointer to a FNRTHANDLETABLEDELETE. */
84 typedef FNRTHANDLETABLEDELETE *PFNRTHANDLETABLEDELETE;
85
86
87 /** @name RTHandleTableCreateEx flags
88  * @{ */
89 /** Whether the handle table entries takes a context or not.
90  *
91  * This can be useful for associating a handle with for instance a process or
92  * similar in order to prevent anyone but the owner from using the handle.
93  *
94  * Setting this means you will have to use the WithCtx functions to do the
95  * handle management. */
96 #define RTHANDLETABLE_FLAGS_CONTEXT     RT_BIT_32(0)
97 /** Whether the handle table should take care of the serialization.
98  * If not specfied the caller will have to take care of that. */
99 #define RTHANDLETABLE_FLAGS_LOCKED      RT_BIT_32(1)
100 /** The mask of valid flags. */
101 #define RTHANDLETABLE_FLAGS_MASK        UINT32_C(0x00000003)
102 /* @} */
103
104
105 /**
106  * Creates a handle table.
107  *
108  * The handle table translates a 32-bit handle into an object pointer,
109  * optionally calling you back so you can retain the object without
110  * racing RTHandleTableFree.
111  *
112  * @returns IPRT status code and on success a handle table handle will be stored at the
113  *          location phHandleTable points at.
114  *
115  * @param   pHandleTable    Where to store the handle table handle on success.
116  * @param   fFlags          Flags, see RTHANDLETABLE_FLAGS_*.
117  * @param   uBase           The handle base value. This is the value of the
118  *                          first handle to be returned.
119  * @param   cMax            The max number of handles. When exceeded the RTHandleTableAlloc
120  *                          or RTHandleTableAllocWithCtx calls will fail. Note that this
121  *                          number will be rounded up to a multiple of the sub-table size,
122  *                          or if it's too close to UINT32_MAX it will be rounded down.
123  * @param   pfnRetain       Optional retain callback that will be called from behind the
124  *                          lock (if any) during lookup.
125  * @param   pvUser          The user argument to the retain callback.
126  */
127 RTDECL(int)     RTHandleTableCreateEx(PRTHANDLETABLE phHandleTable, uint32_t fFlags, uint32_t uBase, uint32_t cMax,
128                                       PFNRTHANDLETABLERETAIN pfnRetain, void *pvUser);
129
130 /**
131  * A simplified version of the RTHandleTableCreateEx API.
132  *
133  * It assumes a max of about 64K handles with 1 being the base. The table
134  * access will serialized (RTHANDLETABLE_FLAGS_LOCKED).
135  *
136  * @returns IPRT status code and *phHandleTable.
137  *
138  * @param   pHandleTable    Where to store the handle table handle on success.
139  */
140 RTDECL(int)     RTHandleTableCreate(PRTHANDLETABLE phHandleTable);
141
142 /**
143  * Destroys a handle table.
144  *
145  * If any entries are still in used the pfnDelete callback will be invoked
146  * on each of them (if specfied) to allow to you clean things up.
147  *
148  * @returns IPRT status code
149  *
150  * @param   hHandleTable    The handle to the handle table.
151  * @param   pfnDelete       Function to be called back on each handle still in use. Optional.
152  * @param   pvUser          The user argument to pfnDelete.
153  */
154 RTDECL(int) RTHandleTableDestroy(RTHANDLETABLE hHandleTable, PFNRTHANDLETABLEDELETE pfnDelete, void *pvUser);
155
156 /**
157  * Allocates a handle from the handle table.
158  *
159  * @returns IPRT status code, almost any.
160  * @retval  VINF_SUCCESS on success.
161  * @retval  VERR_NO_MEMORY if we failed to extend the handle table.
162  * @retval  VERR_NO_MORE_HANDLES if we're out of handles.
163  *
164  * @param   hHandleTable    The handle to the handle table.
165  * @param   pvObj           The object to associate with the new handle.
166  *                          This must be aligned on a 4 byte boundrary.
167  * @param   ph              Where to return the handle on success.
168  *
169  * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
170  */
171 RTDECL(int) RTHandleTableAlloc(RTHANDLETABLE hHandleTable, void *pvObj, uint32_t *ph);
172
173 /**
174  * Looks up a handle.
175  *
176  * @returns The object pointer on success. NULL on failure.
177  *
178  * @param   hHandleTable    The handle to the handle table.
179  * @param   h               The handle to lookup.
180  *
181  * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
182  */
183 RTDECL(void *)  RTHandleTableLookup(RTHANDLETABLE hHandleTable, uint32_t h);
184
185 /**
186  * Looks up and frees a handle.
187  *
188  * @returns The object pointer on success. NULL on failure.
189  *
190  * @param   hHandleTable    The handle to the handle table.
191  * @param   h               The handle to lookup.
192  *
193  * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
194  */
195 RTDECL(void *)  RTHandleTableFree(RTHANDLETABLE hHandleTable, uint32_t h);
196
197 /**
198  * Allocates a handle from the handle table.
199  *
200  * @returns IPRT status code, almost any.
201  * @retval  VINF_SUCCESS on success.
202  * @retval  VERR_NO_MEMORY if we failed to extend the handle table.
203  * @retval  VERR_NO_MORE_HANDLES if we're out of handles.
204  *
205  * @param   hHandleTable    The handle to the handle table.
206  * @param   pvObj           The object to associate with the new handle.
207  *                          This must be aligned on a 4 byte boundrary.
208  * @param   pvCtx           The context to associate with the new handle.
209  * @param   ph              Where to return the handle on success.
210  *
211  * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
212  */
213 RTDECL(int) RTHandleTableAllocWithCtx(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, uint32_t *ph);
214
215 /**
216  * Looks up a handle.
217  *
218  * @returns The object pointer on success. NULL on failure.
219  *
220  * @param   hHandleTable    The handle to the handle table.
221  * @param   h               The handle to lookup.
222  * @param   pvCtx           The handle context, this must match what was given on allocation.
223  *
224  * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
225  */
226 RTDECL(void *)  RTHandleTableLookupWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx);
227
228 /**
229  * Looks up and frees a handle.
230  *
231  * @returns The object pointer on success. NULL on failure.
232  *
233  * @param   hHandleTable    The handle to the handle table.
234  * @param   h               The handle to lookup.
235  * @param   pvCtx           The handle context, this must match what was given on allocation.
236  *
237  * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
238  */
239 RTDECL(void *)  RTHandleTableFreeWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx);
240
241 /** @} */
242
243 __END_DECLS
244
245
246 #endif
247
Note: See TracBrowser for help on using the browser.

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy