1 | /* $Id: htable.cpp 69500 2017-10-28 15:14:05Z 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-2017 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_rand.h"
|
---|
31 | #include "cr_mem.h"
|
---|
32 | #include "cr_string.h"
|
---|
33 |
|
---|
34 | #include <iprt/cdefs.h>
|
---|
35 | #include <iprt/types.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 |
|
---|
39 | VBOXHTABLEDECL(int) CrHTableCreate(PCRHTABLE pTbl, uint32_t cSize)
|
---|
40 | {
|
---|
41 | memset(pTbl, 0, sizeof (*pTbl));
|
---|
42 | if (!cSize)
|
---|
43 | return VINF_SUCCESS;
|
---|
44 | pTbl->paData = (void**)RTMemAllocZ(sizeof (pTbl->paData[0]) * cSize);
|
---|
45 | if (pTbl->paData)
|
---|
46 | {
|
---|
47 | pTbl->cSize = cSize;
|
---|
48 | return VINF_SUCCESS;
|
---|
49 | }
|
---|
50 | WARN(("RTMemAllocZ failed!"));
|
---|
51 | return VERR_NO_MEMORY;
|
---|
52 | }
|
---|
53 |
|
---|
54 | VBOXHTABLEDECL(void) CrHTableDestroy(PCRHTABLE pTbl)
|
---|
55 | {
|
---|
56 | if (!pTbl->paData)
|
---|
57 | return;
|
---|
58 |
|
---|
59 | RTMemFree(pTbl->paData);
|
---|
60 | }
|
---|
61 |
|
---|
62 | int crHTableRealloc(PCRHTABLE pTbl, uint32_t cNewSize)
|
---|
63 | {
|
---|
64 | Assert(cNewSize > pTbl->cSize);
|
---|
65 | if (cNewSize > pTbl->cSize)
|
---|
66 | {
|
---|
67 | void **pvNewData = (void**)RTMemAllocZ(sizeof (pTbl->paData[0]) * cNewSize);
|
---|
68 | if (!pvNewData)
|
---|
69 | {
|
---|
70 | WARN(("RTMemAllocZ failed for size (%d)", (int)(sizeof (pTbl->paData[0]) * cNewSize)));
|
---|
71 | return VERR_NO_MEMORY;
|
---|
72 | }
|
---|
73 | memcpy(pvNewData, pTbl->paData, sizeof (pTbl->paData[0]) * pTbl->cSize);
|
---|
74 | RTMemFree(pTbl->paData);
|
---|
75 | pTbl->iNext2Search = pTbl->cSize;
|
---|
76 | pTbl->cSize = cNewSize;
|
---|
77 | pTbl->paData = pvNewData;
|
---|
78 | return VINF_SUCCESS;
|
---|
79 | }
|
---|
80 | else if (cNewSize >= pTbl->cData)
|
---|
81 | {
|
---|
82 | WARN(("not implemented"));
|
---|
83 | return VERR_NOT_IMPLEMENTED;
|
---|
84 | }
|
---|
85 | WARN(("invalid parameter"));
|
---|
86 | return VERR_INVALID_PARAMETER;
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | VBOXHTABLEDECL(int) CrHTableRealloc(PCRHTABLE pTbl, uint32_t cNewSize)
|
---|
91 | {
|
---|
92 | return crHTableRealloc(pTbl, cNewSize);
|
---|
93 | }
|
---|
94 |
|
---|
95 | VBOXHTABLEDECL(void) CrHTableEmpty(PCRHTABLE pTbl)
|
---|
96 | {
|
---|
97 | pTbl->cData = 0;
|
---|
98 | pTbl->iNext2Search = 0;
|
---|
99 | if (pTbl->cSize)
|
---|
100 | memset(pTbl->paData, 0, sizeof (pTbl->paData[0]) * pTbl->cSize);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void* crHTablePutToSlot(PCRHTABLE pTbl, uint32_t iSlot, void* pvData)
|
---|
104 | {
|
---|
105 | Assert(pvData);
|
---|
106 | void* pvOld = pTbl->paData[iSlot];
|
---|
107 | pTbl->paData[iSlot] = pvData;
|
---|
108 | if (!pvOld)
|
---|
109 | ++pTbl->cData;
|
---|
110 | Assert(pTbl->cData <= pTbl->cSize);
|
---|
111 | return pvOld;
|
---|
112 | }
|
---|
113 |
|
---|
114 | VBOXHTABLEDECL(int) CrHTablePutToSlot(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle, void* pvData)
|
---|
115 | {
|
---|
116 | AssertReturn(pvData, VERR_INVALID_PARAMETER);
|
---|
117 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
118 | if (iIndex >= pTbl->cSize)
|
---|
119 | {
|
---|
120 | int rc = crHTableRealloc(pTbl, iIndex + RT_MAX(10, pTbl->cSize/4));
|
---|
121 | if (!RT_SUCCESS(rc))
|
---|
122 | {
|
---|
123 | WARN(("crHTableRealloc failed rc %d", rc));
|
---|
124 | return CRHTABLE_HANDLE_INVALID;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | crHTablePutToSlot(pTbl, iIndex, pvData);
|
---|
129 |
|
---|
130 | return VINF_SUCCESS;
|
---|
131 | }
|
---|
132 |
|
---|
133 | VBOXHTABLEDECL(CRHTABLE_HANDLE) CrHTablePut(PCRHTABLE pTbl, void* pvData)
|
---|
134 | {
|
---|
135 | AssertReturn(pvData, CRHTABLE_HANDLE_INVALID);
|
---|
136 |
|
---|
137 | if (pTbl->cSize == pTbl->cData)
|
---|
138 | {
|
---|
139 | int rc = crHTableRealloc(pTbl, pTbl->cSize + RT_MAX(10, pTbl->cSize/4));
|
---|
140 | if (!RT_SUCCESS(rc))
|
---|
141 | {
|
---|
142 | WARN(("crHTableRealloc failed rc %d", rc));
|
---|
143 | return CRHTABLE_HANDLE_INVALID;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | for (uint32_t i = pTbl->iNext2Search; ; ++i, i %= pTbl->cSize)
|
---|
147 | {
|
---|
148 | Assert(i < pTbl->cSize);
|
---|
149 | if (!pTbl->paData[i])
|
---|
150 | {
|
---|
151 | void *pvOld = crHTablePutToSlot(pTbl, i, pvData);
|
---|
152 | Assert(!pvOld); NOREF(pvOld);
|
---|
153 | pTbl->iNext2Search = i+1;
|
---|
154 | pTbl->iNext2Search %= pTbl->cSize;
|
---|
155 | return crHTableIndex2Handle(i);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | /* not reached */
|
---|
159 | }
|
---|
160 |
|
---|
161 | VBOXHTABLEDECL(void*) CrHTableRemove(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle)
|
---|
162 | {
|
---|
163 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
164 | Assert(iIndex < pTbl->cSize);
|
---|
165 | if (iIndex < pTbl->cSize)
|
---|
166 | {
|
---|
167 | void* pvData = pTbl->paData[iIndex];
|
---|
168 | if (pvData)
|
---|
169 | {
|
---|
170 | pTbl->paData[iIndex] = NULL;
|
---|
171 | --pTbl->cData;
|
---|
172 | Assert(pTbl->cData <= pTbl->cSize);
|
---|
173 | pTbl->iNext2Search = iIndex;
|
---|
174 | }
|
---|
175 | return pvData;
|
---|
176 | }
|
---|
177 | WARN(("invalid handle supplied %d", hHandle));
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | VBOXHTABLEDECL(void*) CrHTableGet(PCRHTABLE pTbl, CRHTABLE_HANDLE hHandle)
|
---|
182 | {
|
---|
183 | uint32_t iIndex = crHTableHandle2Index(hHandle);
|
---|
184 | if (iIndex < pTbl->cSize)
|
---|
185 | return pTbl->paData[iIndex];
|
---|
186 | LOG(("invalid handle supplied %d", hHandle));
|
---|
187 | return NULL;
|
---|
188 | }
|
---|