summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/misc/handletable.h
blob: be43a69779eab0378daa8314686312c8e40507aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* $Id: handletable.h $ */
/** @file
 * IPRT - Handle Tables, internal header.
 */

/*
 * Copyright (C) 2008-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */

#ifndef IPRT_INCLUDED_SRC_common_misc_handletable_h
#define IPRT_INCLUDED_SRC_common_misc_handletable_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif


/*******************************************************************************
*   Defined Constants And Macros                                               *
*******************************************************************************/
/** The number of entries in the 2nd level lookup table. */
#define RTHT_LEVEL2_ENTRIES             2048

/** The number of (max) 1st level entries requiring dynamic allocation of the
 * 1st level table. If the max number is below this threshold, the 1st level
 * table will be allocated as part of the handle table structure. */
#define RTHT_LEVEL1_DYN_ALLOC_THRESHOLD 256

/** Checks whether a object pointer is really a free entry or not. */
#define RTHT_IS_FREE(pvObj)             ( ((uintptr_t)(pvObj) & 3) == 3 )

/** Sets RTHTENTRYFREE::iNext. */
#define RTHT_SET_FREE_IDX(pFree, idx) \
    do { \
        (pFree)->iNext = ((uintptr_t)((uint32_t)(idx)) << 2) | 3U; \
    } while (0)

/** Gets the index part of RTHTENTRYFREE::iNext. */
#define RTHT_GET_FREE_IDX(pFree)        ( (uint32_t)((pFree)->iNext >> 2) )

/** @def NIL_RTHT_INDEX
 * The NIL handle index for use in the free list. (The difference between
 * 32-bit and 64-bit hosts here comes down to the shifting performed for
 * RTHTENTRYFREE::iNext.) */
#if ARCH_BITS == 32
# define NIL_RTHT_INDEX                 ( UINT32_C(0x3fffffff) )
#elif ARCH_BITS >= 34
# define NIL_RTHT_INDEX                 ( UINT32_C(0xffffffff) )
#else
# error "Missing or unsupported ARCH_BITS."
#endif


/*******************************************************************************
*   Structures and Typedefs                                                    *
*******************************************************************************/

/**
 * Handle table entry, simple variant.
 */
typedef struct RTHTENTRY
{
    /** The object. */
    void *pvObj;
} RTHTENTRY;
/** Pointer to a handle table entry, simple variant. */
typedef RTHTENTRY *PRTHTENTRY;


/**
 * Handle table entry, context variant.
 */
typedef struct RTHTENTRYCTX
{
    /** The object. */
    void *pvObj;
    /** The context. */
    void *pvCtx;
} RTHTENTRYCTX;
/** Pointer to a handle table entry, context variant. */
typedef RTHTENTRYCTX *PRTHTENTRYCTX;


/**
 * Free handle table entry, shared by all variants.
 */
typedef struct RTHTENTRYFREE
{
    /** The index of the next handle, special format.
     * In order to distinguish free and used handle table entries we exploit
     * the heap alignment and use the lower two bits to do this. Used entries
     * will have these bits set to 0, while free entries will have tem set
     * to 3. Use the RTHT_GET_FREE_IDX and RTHT_SET_FREE_IDX macros to access
     * this field. */
    uintptr_t iNext;
} RTHTENTRYFREE;
/** Pointer to a free handle table entry. */
typedef RTHTENTRYFREE *PRTHTENTRYFREE;

AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRY));
AssertCompile(sizeof(RTHTENTRYFREE) <= sizeof(RTHTENTRYCTX));
AssertCompileMemberOffset(RTHTENTRYFREE, iNext, 0);
AssertCompileMemberOffset(RTHTENTRY,     pvObj, 0);
AssertCompileMemberOffset(RTHTENTRYCTX,  pvObj, 0);


/**
 * Internal handle table structure.
 */
typedef struct RTHANDLETABLEINT
{
    /** Magic value (RTHANDLETABLE_MAGIC). */
    uint32_t u32Magic;
    /** The handle table flags specified to RTHandleTableCreateEx. */
    uint32_t fFlags;
    /** The base handle value (i.e. the first handle). */
    uint32_t uBase;
    /** The current number of handle table entries. */
    uint32_t cCur;
    /** The spinlock handle (NIL if RTHANDLETABLE_FLAGS_LOCKED wasn't used). */
    RTSPINLOCK hSpinlock;
    /** The level one lookup table. */
    void **papvLevel1;
    /** The retainer callback. Can be NULL. */
    PFNRTHANDLETABLERETAIN pfnRetain;
    /** The user argument to the retainer. */
    void *pvRetainUser;
    /** The max number of handles. */
    uint32_t cMax;
    /** The number of handles currently allocated. (for optimizing destruction) */
    uint32_t cCurAllocated;
    /** The current number of 1st level entries. */
    uint32_t cLevel1;
    /** Head of the list of free handle entires (index). */
    uint32_t iFreeHead;
    /** Tail of the list of free handle entires (index). */
    uint32_t iFreeTail;
} RTHANDLETABLEINT;
/** Pointer to an handle table structure. */
typedef RTHANDLETABLEINT *PRTHANDLETABLEINT;


/**
 * Looks up a simple index.
 *
 * @returns Pointer to the handle table entry on success, NULL on failure.
 * @param   pThis           The handle table structure.
 * @param   i               The index to look up.
 */
DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimpleIdx(PRTHANDLETABLEINT pThis, uint32_t i)
{
    if (i < pThis->cCur)
    {
        PRTHTENTRY paTable = (PRTHTENTRY)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
        if (paTable)
            return &paTable[i % RTHT_LEVEL2_ENTRIES];
    }
    return NULL;
}


/**
 * Looks up a simple handle.
 *
 * @returns Pointer to the handle table entry on success, NULL on failure.
 * @param   pThis           The handle table structure.
 * @param   h               The handle to look up.
 */
DECLINLINE(PRTHTENTRY) rtHandleTableLookupSimple(PRTHANDLETABLEINT pThis, uint32_t h)
{
    return rtHandleTableLookupSimpleIdx(pThis, h - pThis->uBase);
}


/**
 * Looks up a context index.
 *
 * @returns Pointer to the handle table entry on success, NULL on failure.
 * @param   pThis           The handle table structure.
 * @param   i               The index to look up.
 */
DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtxIdx(PRTHANDLETABLEINT pThis, uint32_t i)
{
    if (i < pThis->cCur)
    {
        PRTHTENTRYCTX paTable = (PRTHTENTRYCTX)pThis->papvLevel1[i / RTHT_LEVEL2_ENTRIES];
        if (paTable)
            return &paTable[i % RTHT_LEVEL2_ENTRIES];
    }
    return NULL;
}


/**
 * Looks up a context handle.
 *
 * @returns Pointer to the handle table entry on success, NULL on failure.
 * @param   pThis           The handle table structure.
 * @param   h               The handle to look up.
 */
DECLINLINE(PRTHTENTRYCTX) rtHandleTableLookupWithCtx(PRTHANDLETABLEINT pThis, uint32_t h)
{
    return rtHandleTableLookupWithCtxIdx(pThis, h - pThis->uBase);
}


/**
 * Locks the handle table.
 *
 * @param   pThis           The handle table structure.
 */
DECLINLINE(void) rtHandleTableLock(PRTHANDLETABLEINT pThis)
{
    if (pThis->hSpinlock != NIL_RTSPINLOCK)
        RTSpinlockAcquire(pThis->hSpinlock);
}


/**
 * Locks the handle table.
 *
 * @param   pThis           The handle table structure.
 */
DECLINLINE(void) rtHandleTableUnlock(PRTHANDLETABLEINT pThis)
{
    if (pThis->hSpinlock != NIL_RTSPINLOCK)
        RTSpinlockRelease(pThis->hSpinlock);
}

#endif /* !IPRT_INCLUDED_SRC_common_misc_handletable_h */