1 | /* $Id: cr_htable.h 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | * uint32_t handle to void simple table API
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2013-2017 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 | #ifndef ___cr_htable_h_
|
---|
19 | #define ___cr_htable_h_
|
---|
20 |
|
---|
21 | #include <iprt/types.h>
|
---|
22 | #include <iprt/cdefs.h>
|
---|
23 |
|
---|
24 | #include <cr_error.h>
|
---|
25 |
|
---|
26 | #ifndef IN_RING0
|
---|
27 | # define VBOXHTABLEDECL(_type) DECLEXPORT(_type)
|
---|
28 | #else
|
---|
29 | # define VBOXHTABLEDECL(_type) RTDECL(_type)
|
---|
30 | #endif
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | RT_C_DECLS_BEGIN
|
---|
35 |
|
---|
36 | typedef uint32_t CRHTABLE_HANDLE;
|
---|
37 | #define CRHTABLE_HANDLE_INVALID 0UL
|
---|
38 |
|
---|
39 | typedef struct CRHTABLE
|
---|
40 | {
|
---|
41 | uint32_t cData;
|
---|
42 | uint32_t iNext2Search;
|
---|
43 | uint32_t cSize;
|
---|
44 | void **paData;
|
---|
45 | } CRHTABLE, *PCRHTABLE;
|
---|
46 |
|
---|
47 | typedef struct CRHTABLE_ITERATOR
|
---|
48 | {
|
---|
49 | PCRHTABLE pTbl;
|
---|
50 | uint32_t iCur;
|
---|
51 | uint32_t cLeft;
|
---|
52 | } VCRHTABLE_ITERATOR, *PCRHTABLE_ITERATOR;
|
---|
53 |
|
---|
54 | /*private stuff, not to be used directly */
|
---|
55 | DECLINLINE(CRHTABLE_HANDLE) crHTableIndex2Handle(uint32_t iIndex)
|
---|
56 | {
|
---|
57 | return iIndex+1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | DECLINLINE(uint32_t) crHTableHandle2Index(CRHTABLE_HANDLE hHandle)
|
---|
61 | {
|
---|
62 | return hHandle-1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* public API */
|
---|
66 | DECLINLINE(void) CrHTableIterInit(PCRHTABLE pTbl, PCRHTABLE_ITERATOR pIter)
|
---|
67 | {
|
---|
68 | pIter->pTbl = pTbl;
|
---|
69 | pIter->iCur = 0;
|
---|
70 | pIter->cLeft = pTbl->cData;
|
---|
71 | }
|
---|
72 |
|
---|
73 | DECLINLINE(void*) CrHTableIterNext(PCRHTABLE_ITERATOR pIter, CRHTABLE_HANDLE *phHandle)
|
---|
74 | {
|
---|
75 | PCRHTABLE pTbl;
|
---|
76 | uint32_t i;
|
---|
77 | if (!pIter->cLeft)
|
---|
78 | {
|
---|
79 | if (phHandle)
|
---|
80 | *phHandle = 0;
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 | pTbl = pIter->pTbl;
|
---|
85 |
|
---|
86 | for (i = pIter->iCur; i < pTbl->cSize; ++i)
|
---|
87 | {
|
---|
88 | if (pTbl->paData[i])
|
---|
89 | {
|
---|
90 | pIter->iCur = i+1;
|
---|
91 | --(pIter->cLeft);
|
---|
92 | if (phHandle)
|
---|
93 | *phHandle = crHTableIndex2Handle(i);
|
---|
94 | return pTbl->paData[i];
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | crWarning("interator concurent modification!");
|
---|
99 | return NULL;
|
---|
100 | }
|
---|
101 |
|
---|
102 | VBOXHTABLEDECL(int) CrHTableCreate(PCRHTABLE pTbl, uint32_t cSize);
|
---|
103 | DECLINLINE(void) CrHTableMoveTo(PCRHTABLE pSrcTbl, PCRHTABLE pDstTbl)
|
---|
104 | {
|
---|
105 | *pDstTbl = *pSrcTbl;
|
---|
106 | CrHTableCreate(pSrcTbl, 0);
|
---|
107 | }
|
---|
108 | VBOXHTABLEDECL(void) CrHTableEmpty(PCRHTABLE pTbl);
|
---|
109 | VBOXHTABLEDECL(void) CrHTableDestroy(PCRHTABLE pTbl);
|
---|
110 | VBOXHTABLEDECL(int) CrHTableRealloc(PCRHTABLE pTbl, uint32_t cNewSize);
|
---|
111 | VBOXHTABLEDECL(CRHTABLE_HANDLE) CrHTablePut(PCRHTABLE pTbl, void *pvData);
|
---|
112 | VBOXHTABLEDECL(int) CrHTablePutToSlot(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle, void* pvData);
|
---|
113 |
|
---|
114 | /* note: can be called for the element returned with CrHTableIterNext w/o corrupting the iterator */
|
---|
115 | VBOXHTABLEDECL(void*) CrHTableRemove(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle);
|
---|
116 | VBOXHTABLEDECL(void*) CrHTableGet(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle);
|
---|
117 |
|
---|
118 | RT_C_DECLS_END
|
---|
119 |
|
---|
120 | #endif /* #ifndef ___cr_htable_h_*/
|
---|