VirtualBox

source: vbox/trunk/include/iprt/handletable.h@ 10765

Last change on this file since 10765 was 10765, checked in by vboxsync, 16 years ago

Spec'ed out the RTHandleTable API.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
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 */
67typedef DECLCALLBACK(int) FNRTHANDLETABLERETAIN(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser);
68/** Pointer to a FNHANDLETABLERETAIN. */
69typedef 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 */
82typedef DECLCALLBACK(void) FNRTHANDLETABLEDELETE(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser);
83/** Pointer to a FNRTHANDLETABLEDELETE. */
84typedef 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/* @} */
101
102
103/**
104 * Creates a handle table.
105 *
106 * The handle table translates a 32-bit handle into an object pointer,
107 * optionally calling you back so you can retain the object without
108 * racing RTHandleTableFree.
109 *
110 * @returns IPRT status code and on success a handle table handle will be stored at the
111 * location phHandleTable points at.
112 *
113 * @param pHandleTable Where to store the handle table handle on success.
114 * @param fFlags Flags, see RTHANDLETABLE_FLAGS_*.
115 * @param uBase The handle base value. This is the value of the
116 * first handle to be returned.
117 * @param cMax The max number of handles. When exceeded the RTHandleTableAlloc
118 * or RTHandleTableAllocWithCtx calls will fail.
119 * @param pfnRetain Optional retain callback that will be called from behind the
120 * lock (if any) during lookup.
121 * @param pvUser The user argument to the retain callback.
122 */
123RTDECL(int) RTHandleTableCreateEx(PRTHANDLETABLE phHandleTable, uint32_t fFlags, uint32_t uBase, uint32_t cMax,
124 PFNRTHANDLETABLERETAIN pfnRetain, void *pvUser);
125
126/**
127 * A simplified version of the RTHandleTableCreateEx API.
128 *
129 * It assumes a max of 65534 handles with 1 being the base. The table
130 * serialized (RTHANDLETABLE_FLAGS_LOCKED).
131 *
132 * @returns IPRT status code and *phHandleTable.
133 *
134 * @param pHandleTable Where to store the handle table handle on success.
135 */
136RTDECL(int) RTHandleTableCreate(PRTHANDLETABLE hHandleTable);
137
138/**
139 * Destroys a handle table.
140 *
141 * If any entries are still in used the pfnDelete callback will be invoked
142 * on each of them (if specfied) to allow to you clean things up.
143 *
144 * @returns IPRT status code
145 *
146 * @param hHandleTable The handle to the handle table.
147 * @param pfnDelete Function to be called back on each handle still in use. Optional.
148 * @param pvUser The user argument to pfnDelete.
149 */
150RTDECL(int) RTHandleTableDestroy(RTHANDLETABLE hHandleTable, PFNRTHANDLETABLEDELETE pfnDelete, void *pvUser);
151
152/**
153 * Allocates a handle from the handle table.
154 *
155 * @returns IPRT status code, almost any.
156 * @retval VINF_SUCCESS on success.
157 * @retval VERR_NO_MEMORY if we failed to extend the handle table.
158 * @retval VERR_NO_MORE_HANDLES if we're out of handles.
159 *
160 * @param hHandleTable The handle to the handle table.
161 * @param pvObj The object to associate with the new handle.
162 * @param ph Where to return the handle on success.
163 *
164 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
165 */
166RTDECL(int) RTHandleTableAlloc(RTHANDLETABLE hHandleTable, void *pvObj, uint32_t *ph);
167
168/**
169 * Looks up a handle.
170 *
171 * @returns The object pointer on success. NULL on failure.
172 *
173 * @param hHandleTable The handle to the handle table.
174 * @param h The handle to lookup.
175 *
176 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
177 */
178RTDECL(void *) RTHandleTableLookup(RTHANDLETABLE hHandleTable, uint32_t h);
179
180/**
181 * Looks up and frees a handle.
182 *
183 * @returns The object pointer on success. NULL on failure.
184 *
185 * @param hHandleTable The handle to the handle table.
186 * @param h The handle to lookup.
187 *
188 * @remarks Do not call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
189 */
190RTDECL(void *) RTHandleTableFree(RTHANDLETABLE hHandleTable, uint32_t h);
191
192/**
193 * Allocates a handle from the handle table.
194 *
195 * @returns IPRT status code, almost any.
196 * @retval VINF_SUCCESS on success.
197 * @retval VERR_NO_MEMORY if we failed to extend the handle table.
198 * @retval VERR_NO_MORE_HANDLES if we're out of handles.
199 *
200 * @param hHandleTable The handle to the handle table.
201 * @param pvObj The object to associate with the new handle.
202 * @param pvCtx The context to associate with the new handle.
203 * @param ph Where to return the handle on success.
204 *
205 * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
206 */
207RTDECL(int) RTHandleTableAllocWithCtx(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, uint32_t *ph);
208
209/**
210 * Looks up a handle.
211 *
212 * @returns The object pointer on success. NULL on failure.
213 *
214 * @param hHandleTable The handle to the handle table.
215 * @param h The handle to lookup.
216 * @param pvCtx The handle context, this must match what was given on allocation.
217 *
218 * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
219 */
220RTDECL(void *) RTHandleTableLookupWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx);
221
222/**
223 * Looks up and frees a handle.
224 *
225 * @returns The object pointer on success. NULL on failure.
226 *
227 * @param hHandleTable The handle to the handle table.
228 * @param h The handle to lookup.
229 * @param pvCtx The handle context, this must match what was given on allocation.
230 *
231 * @remarks Call this if RTHANDLETABLE_FLAGS_CONTEXT was used during creation.
232 */
233RTDECL(void *) RTHandleTableFreeWithCtx(RTHANDLETABLE hHandleTable, uint32_t h, void *pvCtx);
234
235/** @} */
236
237__END_DECLS
238
239
240#endif
241
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette