VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/handletable.h@ 100249

Last change on this file since 100249 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: handletable.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Handle Tables, internal header.
4 */
5
6/*
7 * Copyright (C) 2008-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37#ifndef IPRT_INCLUDED_SRC_common_misc_handletable_h
38#define IPRT_INCLUDED_SRC_common_misc_handletable_h
39#ifndef RT_WITHOUT_PRAGMA_ONCE
40# pragma once
41#endif
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/** The number of entries in the 2nd level lookup table. */
48#define RTHT_LEVEL2_ENTRIES 2048
49
50/** The number of (max) 1st level entries requiring dynamic allocation of the
51 * 1st level table. If the max number is below this threshold, the 1st level
52 * table will be allocated as part of the handle table structure. */
53#define RTHT_LEVEL1_DYN_ALLOC_THRESHOLD 256
54
55/** Checks whether a object pointer is really a free entry or not. */
56#define RTHT_IS_FREE(pvObj) ( ((uintptr_t)(pvObj) & 3) == 3 )
57
58/** Sets RTHTENTRYFREE::iNext. */
59#define RTHT_SET_FREE_IDX(pFree, idx) \
60 do { \
61 (pFree)->iNext = ((uintptr_t)((uint32_t)(idx)) << 2) | 3U; \
62 } while (0)
63
64/** Gets the index part of RTHTENTRYFREE::iNext. */
65#define RTHT_GET_FREE_IDX(pFree) ( (uint32_t)((pFree)->iNext >> 2) )
66
67/** @def NIL_RTHT_INDEX
68 * The NIL handle index for use in the free list. (The difference between
69 * 32-bit and 64-bit hosts here comes down to the shifting performed for
70 * RTHTENTRYFREE::iNext.) */
71#if ARCH_BITS == 32
72# define NIL_RTHT_INDEX ( UINT32_C(0x3fffffff) )
73#elif ARCH_BITS >= 34
74# define NIL_RTHT_INDEX ( UINT32_C(0xffffffff) )
75#else
76# error "Missing or unsupported ARCH_BITS."
77#endif
78
79
80/*******************************************************************************
81* Structures and Typedefs *
82*******************************************************************************/
83
84/**
85 * Handle table entry, simple variant.
86 */
87typedef struct RTHTENTRY
88{
89 /** The object. */
90 void *pvObj;
91} RTHTENTRY;
92/** Pointer to a handle table entry, simple variant. */
93typedef RTHTENTRY *PRTHTENTRY;
94
95
96/**
97 * Handle table entry, context variant.
98 */
99typedef struct RTHTENTRYCTX
100{
101 /** The object. */
102 void *pvObj;
103 /** The context. */
104 void *pvCtx;
105} RTHTENTRYCTX;
106/** Pointer to a handle table entry, context variant. */
107typedef RTHTENTRYCTX *PRTHTENTRYCTX;
108
109
110/**
111 * Free handle table entry, shared by all variants.
112 */
113typedef struct RTHTENTRYFREE
114{
115 /** The index of the next handle, special format.
116 * In order to distinguish free and used handle table entries we exploit
117 * the heap alignment and use the lower two bits to do this. Used entries
118 * will have these bits set to 0, while free entries will have tem set
119 * to 3. Use the RTHT_GET_FREE_IDX and RTHT_SET_FREE_IDX macros to access
120 * this field. */
121 uintptr_t iNext;
122} RTHTENTRYFREE;
123/** Pointer to a free handle table entry. */
124typedef RTHTENTRYFREE *PRTHTENTRYFREE;
125
126AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRY));
127AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRYCTX));
128AssertCompileMemberOffset(RTHTENTRYFREE, iNext, 0);
129AssertCompileMemberOffset(RTHTENTRY, pvObj, 0);
130AssertCompileMemberOffset(RTHTENTRYCTX, pvObj, 0);
131
132
133/**
134 * Internal handle table structure.
135 */
136typedef struct RTHANDLETABLEINT
137{
138 /** Magic value (RTHANDLETABLE_MAGIC). */
139 uint32_t u32Magic;
140 /** The handle table flags specified to RTHandleTableCreateEx. */
141 uint32_t fFlags;
142 /** The base handle value (i.e. the first handle). */
143 uint32_t uBase;
144 /** The current number of handle table entries. */
145 uint32_t cCur;
146 /** The spinlock handle (NIL if RTHANDLETABLE_FLAGS_LOCKED wasn't used). */
147 RTSPINLOCK hSpinlock;
148 /** The level one lookup table. */
149 void **papvLevel1;
150 /** The retainer callback. Can be NULL. */
151 PFNRTHANDLETABLERETAIN pfnRetain;
152 /** The user argument to the retainer. */
153 void *pvRetainUser;
154 /** The max number of handles. */
155 uint32_t cMax;
156 /** The number of handles currently allocated. (for optimizing destruction) */
157 uint32_t cCurAllocated;
158 /** The current number of 1st level entries. */
159 uint32_t cLevel1;
160 /** Head of the list of free handle entires (index). */
161 uint32_t iFreeHead;
162 /** Tail of the list of free handle entires (index). */
163 uint32_t iFreeTail;
164} RTHANDLETABLEINT;
165/** Pointer to an handle table structure. */
166typedef RTHANDLETABLEINT *PRTHANDLETABLEINT;
167
168
169/**
170 * Looks up a simple index.
171 *
172 * @returns Pointer to the handle table entry on success, NULL on failure.
173 * @param pThis The handle table structure.
174 * @param i The index to look up.
175 */
176DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimpleIdx(PRTHANDLETABLEINT pThis, uint32_t i)
177{
178 if (i < pThis->cCur)
179 {
180 PRTHTENTRY paTable = (PRTHTENTRY)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
181 if (paTable)
182 return &paTable[i % RTHT_LEVEL2_ENTRIES];
183 }
184 return NULL;
185}
186
187
188/**
189 * Looks up a simple handle.
190 *
191 * @returns Pointer to the handle table entry on success, NULL on failure.
192 * @param pThis The handle table structure.
193 * @param h The handle to look up.
194 */
195DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimple(PRTHANDLETABLEINT pThis, uint32_t h)
196{
197 return rtHandleTableLookupSimpleIdx(pThis, h - pThis->uBase);
198}
199
200
201/**
202 * Looks up a context index.
203 *
204 * @returns Pointer to the handle table entry on success, NULL on failure.
205 * @param pThis The handle table structure.
206 * @param i The index to look up.
207 */
208DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtxIdx(PRTHANDLETABLEINT pThis, uint32_t i)
209{
210 if (i < pThis->cCur)
211 {
212 PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
213 if (paTable)
214 return &paTable[i % RTHT_LEVEL2_ENTRIES];
215 }
216 return NULL;
217}
218
219
220/**
221 * Looks up a context handle.
222 *
223 * @returns Pointer to the handle table entry on success, NULL on failure.
224 * @param pThis The handle table structure.
225 * @param h The handle to look up.
226 */
227DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtx(PRTHANDLETABLEINT pThis, uint32_t h)
228{
229 return rtHandleTableLookupWithCtxIdx(pThis, h - pThis->uBase);
230}
231
232
233/**
234 * Locks the handle table.
235 *
236 * @param pThis The handle table structure.
237 */
238DECLINLINE(void) rtHandleTableLock(PRTHANDLETABLEINT pThis)
239{
240 if (pThis->hSpinlock != NIL_RTSPINLOCK)
241 RTSpinlockAcquire(pThis->hSpinlock);
242}
243
244
245/**
246 * Locks the handle table.
247 *
248 * @param pThis The handle table structure.
249 */
250DECLINLINE(void) rtHandleTableUnlock(PRTHANDLETABLEINT pThis)
251{
252 if (pThis->hSpinlock != NIL_RTSPINLOCK)
253 RTSpinlockRelease(pThis->hSpinlock);
254}
255
256#endif /* !IPRT_INCLUDED_SRC_common_misc_handletable_h */
257
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