VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPSa.cpp@ 80422

Last change on this file since 80422 was 80422, checked in by vboxsync, 5 years ago

WDDM: remove old chromium based 3D code from the miniport driver. bugref:9529

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1/* $Id: VBoxMPSa.cpp 80422 2019-08-26 13:56:24Z vboxsync $ */
2
3/** @file
4 * Sorted array impl
5 */
6
7/*
8 * Copyright (C) 2014-2019 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
19#include "common/VBoxMPUtils.h"
20#include <iprt/err.h>
21#include <iprt/mem.h>
22
23#include <memory.h>
24
25#include "VBoxMPSa.h"
26
27VBOXSADECL(int) CrSaInit(CR_SORTARRAY *pArray, uint32_t cInitBuffer)
28{
29 pArray->cBufferSize = cInitBuffer;
30 pArray->cSize = 0;
31 if (cInitBuffer)
32 {
33 pArray->pElements = (uint64_t*)RTMemAlloc(cInitBuffer * sizeof (pArray->pElements[0]));
34 if (!pArray->pElements)
35 {
36 WARN(("no memory"));
37 /* sanity */
38 pArray->cBufferSize = 0;
39 return VERR_NO_MEMORY;
40 }
41 }
42 else
43 pArray->pElements = NULL;
44
45 return VINF_SUCCESS;
46}
47
48VBOXSADECL(void) CrSaCleanup(CR_SORTARRAY *pArray)
49{
50 if (pArray->pElements)
51 RTMemFree(pArray->pElements);
52
53 CrSaInit(pArray, 0);
54}
55
56static int crSaSearch(const CR_SORTARRAY *pArray, uint64_t element)
57{
58 int iMin = 0;
59 int iMax = pArray->cSize;
60 int i = 0;
61
62 while (iMin < iMax)
63 {
64 i = (iMax + iMin) / 2;
65
66 uint64_t el = pArray->pElements[i];
67 if (el == element)
68 return i;
69 else if (el < element)
70 iMin = i + 1;
71 else
72 iMax = i;
73 }
74
75 return -1;
76}
77
78static void crSaDbgValidate(const CR_SORTARRAY *pArray)
79{
80 Assert(pArray->cSize <= pArray->cBufferSize);
81 Assert(!pArray->pElements == !pArray->cBufferSize);
82 if (!pArray->cSize)
83 return;
84 uint64_t cur = pArray->pElements[0];
85 for (uint32_t i = 1; i < pArray->cSize; ++i)
86 {
87 Assert(pArray->pElements[i] > cur);
88 cur = pArray->pElements[i];
89 }
90}
91
92#ifdef DEBUG
93# define crSaValidate crSaDbgValidate
94#else
95# define crSaValidate(_a) do {} while (0)
96#endif
97
98static int crSaInsAt(CR_SORTARRAY *pArray, uint32_t iPos, uint64_t element)
99{
100 if (pArray->cSize == pArray->cBufferSize)
101 {
102 uint32_t cNewBufferSize = pArray->cBufferSize + 16;
103 uint64_t *pNew;
104 if (pArray->pElements)
105 pNew = (uint64_t*)RTMemRealloc(pArray->pElements, cNewBufferSize * sizeof (pArray->pElements[0]));
106 else
107 pNew = (uint64_t*)RTMemAlloc(cNewBufferSize * sizeof (pArray->pElements[0]));
108 if (!pNew)
109 {
110 WARN(("no memory"));
111 return VERR_NO_MEMORY;
112 }
113
114 pArray->pElements = pNew;
115 pArray->cBufferSize = cNewBufferSize;
116 crSaValidate(pArray);
117 }
118
119 for (int32_t i = (int32_t)pArray->cSize - 1; i >= (int32_t)iPos; --i)
120 {
121 pArray->pElements[i+1] = pArray->pElements[i];
122 }
123
124 pArray->pElements[iPos] = element;
125 ++pArray->cSize;
126
127 crSaValidate(pArray);
128
129 return VINF_SUCCESS;
130}
131
132static void crSaDelAt(CR_SORTARRAY *pArray, uint32_t iPos)
133{
134 Assert(pArray->cSize > iPos);
135
136 for (uint32_t i = iPos; i < pArray->cSize - 1; ++i)
137 {
138 pArray->pElements[i] = pArray->pElements[i+1];
139 }
140
141 --pArray->cSize;
142}
143
144static int crSaAdd(CR_SORTARRAY *pArray, uint64_t element)
145{
146 int iMin = 0;
147 int iMax = pArray->cSize;
148 int i = 0;
149 uint64_t el;
150
151 if (!iMax)
152 return crSaInsAt(pArray, 0, element);
153
154 el = element; /* Shup up MSC. */
155 while (iMin < iMax)
156 {
157 i = (iMax + iMin) / 2;
158
159 el = pArray->pElements[i];
160 if (el == element)
161 return VINF_ALREADY_INITIALIZED;
162 else if (el < element)
163 iMin = i + 1;
164 else
165 iMax = i;
166 }
167
168 if (el < element)
169 return crSaInsAt(pArray, i+1, element);
170 return crSaInsAt(pArray, i, element);
171}
172
173static int crSaRemove(CR_SORTARRAY *pArray, uint64_t element)
174{
175 int i = crSaSearch(pArray, element);
176 if (i >= 0)
177 {
178 crSaDelAt(pArray, i);
179 return VINF_SUCCESS;
180 }
181 return VINF_ALREADY_INITIALIZED;
182}
183
184/*
185 * * @return true if element is found */
186VBOXSADECL(bool) CrSaContains(const CR_SORTARRAY *pArray, uint64_t element)
187{
188 return crSaSearch(pArray, element) >= 0;
189}
190
191VBOXSADECL(int) CrSaAdd(CR_SORTARRAY *pArray, uint64_t element)
192{
193 return crSaAdd(pArray, element);
194}
195
196VBOXSADECL(int) CrSaRemove(CR_SORTARRAY *pArray, uint64_t element)
197{
198 return crSaRemove(pArray, element);
199}
200
201static int crSaIntersected(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult)
202{
203 int rc = VINF_SUCCESS;
204 CrSaClear(pResult);
205
206 for (uint32_t i = 0, j = 0; i < pArray1->cSize && j < pArray2->cSize; )
207 {
208 if (pArray1->pElements[i] == pArray2->pElements[j])
209 {
210 rc = CrSaAdd(pResult, pArray1->pElements[i]);
211 if (rc < 0)
212 {
213 WARN(("CrSaAdd failed"));
214 return rc;
215 }
216
217 ++i;
218 ++j;
219 }
220 else if (pArray1->pElements[i] < pArray2->pElements[j])
221 {
222 ++i;
223 }
224 else
225 {
226 ++j;
227 }
228 }
229
230 return VINF_SUCCESS;
231}
232
233static void crSaIntersect(CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
234{
235 for (uint32_t i = 0, j = 0; i < pArray1->cSize && j < pArray2->cSize; )
236 {
237 if (pArray1->pElements[i] == pArray2->pElements[j])
238 {
239 ++i;
240 ++j;
241 }
242 else if (pArray1->pElements[i] < pArray2->pElements[j])
243 crSaDelAt(pArray1, i);
244 else
245 ++j;
246 }
247}
248
249/*
250 * @return >= 0 success
251 * < 0 - no memory
252 * */
253VBOXSADECL(void) CrSaIntersect(CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
254{
255 crSaIntersect(pArray1, pArray2);
256}
257
258VBOXSADECL(int) CrSaIntersected(CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult)
259{
260 return crSaIntersected(pArray1, pArray2, pResult);
261}
262
263static int crSaUnited(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult)
264{
265 int rc = VINF_SUCCESS;
266 CrSaClear(pResult);
267
268 uint32_t i = 0, j = 0;
269 uint32_t cResult = 0;
270 while (i < pArray1->cSize && j < pArray2->cSize)
271 {
272 uint64_t element;
273 if (pArray1->pElements[i] == pArray2->pElements[j])
274 {
275 element = pArray1->pElements[i];
276 ++i;
277 ++j;
278 }
279 else if (pArray1->pElements[i] < pArray2->pElements[j])
280 {
281 element = pArray1->pElements[i];
282 ++i;
283 }
284 else
285 {
286 element = pArray1->pElements[j];
287 ++j;
288 }
289
290 rc = crSaInsAt(pResult, cResult++, element);
291 if (rc < 0)
292 {
293 WARN(("crSaInsAt failed"));
294 return rc;
295 }
296 }
297
298 uint32_t iTail;
299 const CR_SORTARRAY *pTail;
300
301 if (i < pArray1->cSize)
302 {
303 iTail = i;
304 pTail = pArray1;
305 }
306 else if (j < pArray2->cSize)
307 {
308 iTail = j;
309 pTail = pArray2;
310 }
311 else
312 {
313 iTail = 0;
314 pTail = 0;
315 }
316
317 if (pTail)
318 {
319 for (;iTail < pTail->cSize; ++iTail)
320 {
321 rc = crSaInsAt(pResult, cResult++, pTail->pElements[iTail]);
322 if (rc < 0)
323 {
324 WARN(("crSaInsAt failed"));
325 return rc;
326 }
327 }
328 }
329
330 return VINF_SUCCESS;
331}
332
333VBOXSADECL(int) CrSaUnited(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2, CR_SORTARRAY *pResult)
334{
335 return crSaUnited(pArray1, pArray2, pResult);
336}
337
338static int crSaClone(const CR_SORTARRAY *pArray1, CR_SORTARRAY *pResult)
339{
340 CrSaClear(pResult);
341
342 if (pArray1->cSize > pResult->cBufferSize)
343 {
344 CrSaCleanup(pResult);
345 uint32_t cNewBufferSize = pArray1->cSize;
346 uint64_t *pNew = (uint64_t*)RTMemAlloc(cNewBufferSize * sizeof (pResult->pElements[0]));
347 if (!pNew)
348 {
349 WARN(("no memory"));
350 return VERR_NO_MEMORY;
351 }
352
353 pResult->pElements = pNew;
354 pResult->cBufferSize = cNewBufferSize;
355 crSaValidate(pResult);
356 }
357
358 pResult->cSize = pArray1->cSize;
359 memcpy(pResult->pElements, pArray1->pElements, pArray1->cSize * sizeof (pArray1->pElements[0]));
360 return VINF_SUCCESS;
361}
362
363/*
364 * @return VINF_SUCCESS on success
365 * VERR_NO_MEMORY - no memory
366 * */
367VBOXSADECL(int) CrSaClone(const CR_SORTARRAY *pArray1, CR_SORTARRAY *pResult)
368{
369 return crSaClone(pArray1, pResult);
370}
371
372static int crSaCmp(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
373{
374 int diff = CrSaGetSize(pArray1) - CrSaGetSize(pArray2);
375 if (diff)
376 return diff;
377
378 return memcmp(pArray1->pElements, pArray2->pElements, pArray1->cSize * sizeof (pArray1->pElements[0]));
379}
380
381VBOXSADECL(int) CrSaCmp(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
382{
383 return crSaCmp(pArray1, pArray2);
384}
385
386static bool crSaCovers(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
387{
388 if (CrSaGetSize(pArray1) < CrSaGetSize(pArray2))
389 return false;
390
391 uint32_t i = 0, j = 0;
392 while (j < pArray2->cSize)
393 {
394 if (i == pArray1->cSize)
395 return false;
396
397 if (pArray1->pElements[i] == pArray2->pElements[j])
398 {
399 ++i;
400 ++j;
401 }
402 else if (pArray1->pElements[i] < pArray2->pElements[j])
403 ++i;
404 else
405 return false;
406 }
407
408 return true;
409}
410
411VBOXSADECL(bool) CrSaCovers(const CR_SORTARRAY *pArray1, const CR_SORTARRAY *pArray2)
412{
413 return crSaCovers(pArray1, pArray2);
414}
415
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