1 | /* $Id: htable.cpp 78190 2019-04-18 00:07:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * uint32_t handle to void simple table impl
|
---|
4 | *
|
---|
5 | * @todo Why couldn't you simply use iprt/handletable.h?
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <iprt/cdefs.h>
|
---|
21 | #include <iprt/asm.h>
|
---|
22 | #include "cr_spu.h"
|
---|
23 | #include "cr_vreg.h"
|
---|
24 |
|
---|
25 | #include "cr_htable.h"
|
---|
26 | #include "cr_spu.h"
|
---|
27 | #include "chromium.h"
|
---|
28 | #include "cr_error.h"
|
---|
29 | #include "cr_net.h"
|
---|
30 | #include "cr_mem.h"
|
---|
31 | #include "cr_string.h"
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/errcore.h>
|
---|
37 |
|
---|
38 | VBOXHTABLEDECL(int) CrHTableCreate(PCRHTABLE pTbl, uint32_t cSize)
|
---|
39 | {
|
---|
40 | memset(pTbl, 0, sizeof (*pTbl));
|
---|
41 | if (!cSize)
|
---|
42 | return VINF_SUCCESS;
|
---|
43 | pTbl->paData = (void**)RTMemAllocZ(sizeof (pTbl->paData[0]) * cSize);
|
---|
44 | if (pTbl->paData)
|
---|
45 | {
|
---|
46 | pTbl->cSize = cSize;
|
---|
47 | return VINF_SUCCESS;
|
---|
48 | }
|
---|
49 | WARN(("RTMemAllocZ failed!"));
|
---|
50 | return VERR_NO_MEMORY;
|
---|
51 | }
|
---|
52 |
|
---|
53 | VBOXHTABLEDECL(void) CrHTableDestroy(PCRHTABLE pTbl)
|
---|
54 | {
|
---|
55 | if (!pTbl->paData)
|
---|
56 | return;
|
---|
57 |
|
---|
58 | RTMemFree(pTbl->paData);
|
---|
59 | }
|
---|
60 |
|
---|
61 | int crHTableRealloc(PCRHTABLE pTbl, uint32_t cNewSize)
|
---|
62 | {
|
---|
63 | Assert(cNewSize > pTbl->cSize);
|
---|
64 | if (cNewSize > pTbl->cSize)
|
---|
65 | {
|
---|
66 | void **pvNewData = (void**)RTMemAllocZ(sizeof (pTbl->paData[0]) * cNewSize);
|
---|
67 | if (!pvNewData)
|
---|
68 | {
|
---|
69 | WARN(("RTMemAllocZ failed for size (%d)", (int)(sizeof (pTbl->paData[0]) * cNewSize)));
|
---|
70 | return VERR_NO_MEMORY;
|
---|
71 | }
|
---|
72 | memcpy(pvNewData, pTbl->paData, sizeof (pTbl->paData[0]) * pTbl->cSize);
|
---|
73 | RTMemFree(pTbl->paData);
|
---|
74 | pTbl->iNext2Search = pTbl->cSize;
|
---|
75 | pTbl->cSize = cNewSize;
|
---|
76 | pTbl->paData = pvNewData;
|
---|
77 | return VINF_SUCCESS;
|
---|
78 | }
|
---|
79 | else if (cNewSize >= pTbl->cData)
|
---|
80 | {
|
---|
81 | WARN(("not implemented"));
|
---|
82 | return VERR_NOT_IMPLEMENTED;
|
---|
83 | }
|
---|
84 | WARN(("invalid parameter"));
|
---|
85 | return VERR_INVALID_PARAMETER;
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | VBOXHTABLEDECL(int) CrHTableRealloc(PCRHTABLE pTbl, uint32_t cNewSize)
|
---|
90 | {
|
---|
91 | return crHTableRealloc(pTbl, cNewSize);
|
---|
92 | }
|
---|
93 |
|
---|
94 | VBOXHTABLEDECL(void) CrHTableEmpty(PCRHTABLE pTbl)
|
---|
95 | {
|
---|
96 | pTbl->cData = 0;
|
---|
97 | pTbl->iNext2Search = 0;
|
---|
98 | if (pTbl->cSize)
|
---|
99 | memset(pTbl->paData, 0, sizeof (pTbl->paData[0]) * pTbl->cSize);
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void* crHTablePutToSlot(PCRHTABLE pTbl, uint32_t iSlot, void* pvData)
|
---|
103 | {
|
---|
104 | Assert(pvData);
|
---|
105 | void* pvOld = pTbl->paData[iSlot];
|
---|
106 | pTbl->paData[iSlot] = pvData;
|
---|
107 | if (!pvOld)
|
---|
108 | ++pTbl->cData;
|
---|
109 | Assert(pTbl->cData <= pTbl->cSize);
|
---|
110 | return pvOld;
|
---|
111 | }
|
---|
112 |
|
---|
113 | VBOXHTABLEDECL(int) CrHTablePutToSlot(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle, void* pvData)
|
---|
114 | {
|
---|
115 | AssertReturn(pvData, VERR_INVALID_PARAMETER);
|
---|
116 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
117 | if (iIndex >= pTbl->cSize)
|
---|
118 | {
|
---|
119 | int rc = crHTableRealloc(pTbl, iIndex + RT_MAX(10, pTbl->cSize/4));
|
---|
120 | if (!RT_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | WARN(("crHTableRealloc failed rc %d", rc));
|
---|
123 | return CRHTABLE_HANDLE_INVALID;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | crHTablePutToSlot(pTbl, iIndex, pvData);
|
---|
128 |
|
---|
129 | return VINF_SUCCESS;
|
---|
130 | }
|
---|
131 |
|
---|
132 | VBOXHTABLEDECL(CRHTABLE_HANDLE) CrHTablePut(PCRHTABLE pTbl, void* pvData)
|
---|
133 | {
|
---|
134 | AssertReturn(pvData, CRHTABLE_HANDLE_INVALID);
|
---|
135 |
|
---|
136 | if (pTbl->cSize == pTbl->cData)
|
---|
137 | {
|
---|
138 | int rc = crHTableRealloc(pTbl, pTbl->cSize + RT_MAX(10, pTbl->cSize/4));
|
---|
139 | if (!RT_SUCCESS(rc))
|
---|
140 | {
|
---|
141 | WARN(("crHTableRealloc failed rc %d", rc));
|
---|
142 | return CRHTABLE_HANDLE_INVALID;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | for (uint32_t i = pTbl->iNext2Search; ; ++i, i %= pTbl->cSize)
|
---|
146 | {
|
---|
147 | Assert(i < pTbl->cSize);
|
---|
148 | if (!pTbl->paData[i])
|
---|
149 | {
|
---|
150 | void *pvOld = crHTablePutToSlot(pTbl, i, pvData);
|
---|
151 | Assert(!pvOld); NOREF(pvOld);
|
---|
152 | pTbl->iNext2Search = i+1;
|
---|
153 | pTbl->iNext2Search %= pTbl->cSize;
|
---|
154 | return crHTableIndex2Handle(i);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | /* not reached */
|
---|
158 | }
|
---|
159 |
|
---|
160 | VBOXHTABLEDECL(void*) CrHTableRemove(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle)
|
---|
161 | {
|
---|
162 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
163 | Assert(iIndex < pTbl->cSize);
|
---|
164 | if (iIndex < pTbl->cSize)
|
---|
165 | {
|
---|
166 | void* pvData = pTbl->paData[iIndex];
|
---|
167 | if (pvData)
|
---|
168 | {
|
---|
169 | pTbl->paData[iIndex] = NULL;
|
---|
170 | --pTbl->cData;
|
---|
171 | Assert(pTbl->cData <= pTbl->cSize);
|
---|
172 | pTbl->iNext2Search = iIndex;
|
---|
173 | }
|
---|
174 | return pvData;
|
---|
175 | }
|
---|
176 | WARN(("invalid handle supplied %d", hHandle));
|
---|
177 | return NULL;
|
---|
178 | }
|
---|
179 |
|
---|
180 | VBOXHTABLEDECL(void*) CrHTableGet(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle)
|
---|
181 | {
|
---|
182 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
183 | if (iIndex < pTbl->cSize)
|
---|
184 | return pTbl->paData[iIndex];
|
---|
185 | LOG(("invalid handle supplied %d", hHandle));
|
---|
186 | return NULL;
|
---|
187 | }
|
---|