VirtualBox

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

Last change on this file since 68716 was 62473, checked in by vboxsync, 8 years ago

(C) 2016

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