VirtualBox

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

Last change on this file since 85416 was 85121, checked in by vboxsync, 4 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

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