VirtualBox

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

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