VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/sg.cpp@ 99553

Last change on this file since 99553 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1/* $Id: sg.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - S/G buffer handling.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/sg.h>
42#include <iprt/string.h>
43#include <iprt/assert.h>
44#include <iprt/asm.h>
45
46
47static void *rtSgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
48{
49 size_t cbData;
50 void *pvBuf;
51
52 /* Check that the S/G buffer has memory left. */
53 if (RT_UNLIKELY( pSgBuf->idxSeg == pSgBuf->cSegs
54 && !pSgBuf->cbSegLeft))
55 {
56 *pcbData = 0;
57 return NULL;
58 }
59
60#ifndef RDESKTOP
61 AssertMsg( pSgBuf->cbSegLeft <= 128 * _1M
62 && (uintptr_t)pSgBuf->pvSegCur >= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg
63 && (uintptr_t)pSgBuf->pvSegCur + pSgBuf->cbSegLeft <= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg + pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg,
64 ("pSgBuf->idxSeg=%d pSgBuf->cSegs=%d pSgBuf->pvSegCur=%p pSgBuf->cbSegLeft=%zd pSgBuf->paSegs[%d].pvSeg=%p pSgBuf->paSegs[%d].cbSeg=%zd\n",
65 pSgBuf->idxSeg, pSgBuf->cSegs, pSgBuf->pvSegCur, pSgBuf->cbSegLeft, pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg, pSgBuf->idxSeg,
66 pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg));
67#endif
68
69 cbData = RT_MIN(*pcbData, pSgBuf->cbSegLeft);
70 pvBuf = pSgBuf->pvSegCur;
71 pSgBuf->cbSegLeft -= cbData;
72
73 /* Advance to the next segment if required. */
74 if (!pSgBuf->cbSegLeft)
75 {
76 pSgBuf->idxSeg++;
77
78 if (pSgBuf->idxSeg < pSgBuf->cSegs)
79 {
80 pSgBuf->pvSegCur = pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg;
81 pSgBuf->cbSegLeft = pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg;
82 }
83
84 *pcbData = cbData;
85 }
86 else
87 pSgBuf->pvSegCur = (uint8_t *)pSgBuf->pvSegCur + cbData;
88
89 return pvBuf;
90}
91
92
93RTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs)
94{
95 AssertPtr(pSgBuf);
96 Assert( (cSegs > 0 && RT_VALID_PTR(paSegs))
97 || (!cSegs && !paSegs));
98 Assert(cSegs < (~(unsigned)0 >> 1));
99
100 pSgBuf->paSegs = paSegs;
101 pSgBuf->cSegs = (unsigned)cSegs;
102 pSgBuf->idxSeg = 0;
103 if (cSegs && paSegs)
104 {
105 pSgBuf->pvSegCur = paSegs[0].pvSeg;
106 pSgBuf->cbSegLeft = paSegs[0].cbSeg;
107 }
108 else
109 {
110 pSgBuf->pvSegCur = NULL;
111 pSgBuf->cbSegLeft = 0;
112 }
113}
114
115
116RTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf)
117{
118 AssertPtrReturnVoid(pSgBuf);
119
120 pSgBuf->idxSeg = 0;
121 if (pSgBuf->cSegs)
122 {
123 pSgBuf->pvSegCur = pSgBuf->paSegs[0].pvSeg;
124 pSgBuf->cbSegLeft = pSgBuf->paSegs[0].cbSeg;
125 }
126 else
127 {
128 pSgBuf->pvSegCur = NULL;
129 pSgBuf->cbSegLeft = 0;
130 }
131}
132
133
134RTDECL(void) RTSgBufClone(PRTSGBUF pSgBufTo, PCRTSGBUF pSgBufFrom)
135{
136 AssertPtr(pSgBufTo);
137 AssertPtr(pSgBufFrom);
138
139 pSgBufTo->paSegs = pSgBufFrom->paSegs;
140 pSgBufTo->cSegs = pSgBufFrom->cSegs;
141 pSgBufTo->idxSeg = pSgBufFrom->idxSeg;
142 pSgBufTo->pvSegCur = pSgBufFrom->pvSegCur;
143 pSgBufTo->cbSegLeft = pSgBufFrom->cbSegLeft;
144}
145
146
147RTDECL(void *) RTSgBufGetNextSegment(PRTSGBUF pSgBuf, size_t *pcbSeg)
148{
149 AssertPtrReturn(pSgBuf, NULL);
150 AssertPtrReturn(pcbSeg, NULL);
151
152 if (!*pcbSeg)
153 *pcbSeg = pSgBuf->cbSegLeft;
154
155 return rtSgBufGet(pSgBuf, pcbSeg);
156}
157
158
159RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy)
160{
161 AssertPtrReturn(pSgBufDst, 0);
162 AssertPtrReturn(pSgBufSrc, 0);
163
164 size_t cbLeft = cbCopy;
165 while (cbLeft)
166 {
167 size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
168 if (!cbThisCopy)
169 break;
170
171 size_t cbTmp = cbThisCopy;
172 void *pvBufDst = rtSgBufGet(pSgBufDst, &cbTmp);
173 Assert(cbTmp == cbThisCopy);
174 void *pvBufSrc = rtSgBufGet(pSgBufSrc, &cbTmp);
175 Assert(cbTmp == cbThisCopy);
176
177 memcpy(pvBufDst, pvBufSrc, cbThisCopy);
178
179 cbLeft -= cbThisCopy;
180 }
181
182 return cbCopy - cbLeft;
183}
184
185
186RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp)
187{
188 AssertPtrReturn(pSgBuf1, 0);
189 AssertPtrReturn(pSgBuf2, 0);
190
191 /* Set up the temporary buffers */
192 RTSGBUF SgBuf1;
193 RTSgBufClone(&SgBuf1, pSgBuf1);
194 RTSGBUF SgBuf2;
195 RTSgBufClone(&SgBuf2, pSgBuf2);
196
197 size_t cbLeft = cbCmp;
198 while (cbLeft)
199 {
200 size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
201 if (!cbThisCmp)
202 break;
203
204 size_t cbTmp = cbThisCmp;
205 void *pvBuf1 = rtSgBufGet(&SgBuf1, &cbTmp);
206 Assert(cbTmp == cbThisCmp);
207 void *pvBuf2 = rtSgBufGet(&SgBuf2, &cbTmp);
208 Assert(cbTmp == cbThisCmp);
209
210 int rc = memcmp(pvBuf1, pvBuf2, cbThisCmp);
211 if (rc)
212 return rc;
213
214 cbLeft -= cbThisCmp;
215 }
216
217 return 0;
218}
219
220
221RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp, size_t *poffDiff, bool fAdvance)
222{
223 AssertPtrReturn(pSgBuf1, 0);
224 AssertPtrReturn(pSgBuf2, 0);
225
226 RTSGBUF SgBuf1Tmp;
227 RTSGBUF SgBuf2Tmp;
228 PRTSGBUF pSgBuf1Tmp;
229 PRTSGBUF pSgBuf2Tmp;
230
231 if (!fAdvance)
232 {
233 /* Set up the temporary buffers */
234 RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
235 RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
236 pSgBuf1Tmp = &SgBuf1Tmp;
237 pSgBuf2Tmp = &SgBuf2Tmp;
238 }
239 else
240 {
241 pSgBuf1Tmp = pSgBuf1;
242 pSgBuf2Tmp = pSgBuf2;
243 }
244
245 size_t cbLeft = cbCmp;
246 size_t off = 0;
247 while (cbLeft)
248 {
249 size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
250 if (!cbThisCmp)
251 break;
252
253 size_t cbTmp = cbThisCmp;
254 uint8_t *pbBuf1 = (uint8_t *)rtSgBufGet(pSgBuf1Tmp, &cbTmp);
255 Assert(cbTmp == cbThisCmp);
256 uint8_t *pbBuf2 = (uint8_t *)rtSgBufGet(pSgBuf2Tmp, &cbTmp);
257 Assert(cbTmp == cbThisCmp);
258
259 int iDiff = memcmp(pbBuf1, pbBuf2, cbThisCmp);
260 if (iDiff)
261 {
262 /* Locate the first byte that differs if the caller requested this. */
263 if (poffDiff)
264 {
265 while ( cbThisCmp-- > 0
266 && *pbBuf1 == *pbBuf2)
267 {
268 pbBuf1++;
269 pbBuf2++;
270 off++;
271 }
272
273 *poffDiff = off;
274 }
275 return iDiff;
276 }
277
278 cbLeft -= cbThisCmp;
279 off += cbThisCmp;
280 }
281
282 return 0;
283}
284
285
286RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
287{
288 AssertPtrReturn(pSgBuf, 0);
289
290 size_t cbLeft = cbSet;
291
292 while (cbLeft)
293 {
294 size_t cbThisSet = cbLeft;
295 void *pvBuf = rtSgBufGet(pSgBuf, &cbThisSet);
296
297 if (!cbThisSet)
298 break;
299
300 memset(pvBuf, ubFill, cbThisSet);
301
302 cbLeft -= cbThisSet;
303 }
304
305 return cbSet - cbLeft;
306}
307
308
309RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
310{
311 AssertPtrReturn(pSgBuf, 0);
312 AssertPtrReturn(pvBuf, 0);
313
314 size_t cbLeft = cbCopy;
315
316 while (cbLeft)
317 {
318 size_t cbThisCopy = cbLeft;
319 void *pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy);
320
321 if (!cbThisCopy)
322 break;
323
324 memcpy(pvBuf, pvSrc, cbThisCopy);
325
326 cbLeft -= cbThisCopy;
327 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
328 }
329
330 return cbCopy - cbLeft;
331}
332
333
334RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, const void *pvBuf, size_t cbCopy)
335{
336 AssertPtrReturn(pSgBuf, 0);
337 AssertPtrReturn(pvBuf, 0);
338
339 size_t cbLeft = cbCopy;
340
341 while (cbLeft)
342 {
343 size_t cbThisCopy = cbLeft;
344 void *pvDst = rtSgBufGet(pSgBuf, &cbThisCopy);
345
346 if (!cbThisCopy)
347 break;
348
349 memcpy(pvDst, pvBuf, cbThisCopy);
350
351 cbLeft -= cbThisCopy;
352 pvBuf = (const void *)((uintptr_t)pvBuf + cbThisCopy);
353 }
354
355 return cbCopy - cbLeft;
356}
357
358
359RTDECL(size_t) RTSgBufCopyToFn(PRTSGBUF pSgBuf, size_t cbCopy, PFNRTSGBUFCOPYTO pfnCopyTo, void *pvUser)
360{
361 AssertPtrReturn(pSgBuf, 0);
362 AssertPtrReturn(pfnCopyTo, 0);
363
364 size_t cbLeft = cbCopy;
365
366 while (cbLeft)
367 {
368 size_t cbThisCopy = cbLeft;
369 void *pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy);
370
371 if (!cbThisCopy)
372 break;
373
374 size_t cbThisCopied = pfnCopyTo(pSgBuf, pvSrc, cbThisCopy, pvUser);
375 cbLeft -= cbThisCopied;
376 if (cbThisCopied < cbThisCopy)
377 break;
378 }
379
380 return cbCopy - cbLeft;
381}
382
383
384RTDECL(size_t) RTSgBufCopyFromFn(PRTSGBUF pSgBuf, size_t cbCopy, PFNRTSGBUFCOPYFROM pfnCopyFrom, void *pvUser)
385{
386 AssertPtrReturn(pSgBuf, 0);
387 AssertPtrReturn(pfnCopyFrom, 0);
388
389 size_t cbLeft = cbCopy;
390
391 while (cbLeft)
392 {
393 size_t cbThisCopy = cbLeft;
394 void *pvDst = rtSgBufGet(pSgBuf, &cbThisCopy);
395
396 if (!cbThisCopy)
397 break;
398
399 size_t cbThisCopied = pfnCopyFrom(pSgBuf, pvDst, cbThisCopy, pvUser);
400 cbLeft -= cbThisCopied;
401 if (cbThisCopied < cbThisCopy)
402 break;
403 }
404
405 return cbCopy - cbLeft;
406}
407
408
409RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance)
410{
411 AssertPtrReturn(pSgBuf, 0);
412
413 size_t cbLeft = cbAdvance;
414 while (cbLeft)
415 {
416 size_t cbThisAdvance = cbLeft;
417 rtSgBufGet(pSgBuf, &cbThisAdvance);
418 if (!cbThisAdvance)
419 break;
420
421 cbLeft -= cbThisAdvance;
422 }
423
424 return cbAdvance - cbLeft;
425}
426
427
428RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData)
429{
430 AssertPtrReturn(pSgBuf, 0);
431 AssertPtrReturn(pcSeg, 0);
432
433 unsigned cSeg = 0;
434 size_t cb = 0;
435
436 if (!paSeg)
437 {
438 if (pSgBuf->cbSegLeft > 0)
439 {
440 size_t idx = pSgBuf->idxSeg;
441 cSeg = 1;
442
443 cb += RT_MIN(pSgBuf->cbSegLeft, cbData);
444 cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);
445
446 while ( cbData
447 && idx < pSgBuf->cSegs - 1)
448 {
449 idx++;
450 cSeg++;
451 cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
452 cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
453 }
454 }
455 }
456 else
457 {
458 while ( cbData
459 && cSeg < *pcSeg)
460 {
461 size_t cbThisSeg = cbData;
462 void *pvSeg = rtSgBufGet(pSgBuf, &cbThisSeg);
463
464 if (!cbThisSeg)
465 {
466 Assert(!pvSeg);
467 break;
468 }
469
470 AssertMsg(cbThisSeg <= cbData, ("Impossible!\n"));
471
472 paSeg[cSeg].cbSeg = cbThisSeg;
473 paSeg[cSeg].pvSeg = pvSeg;
474 cSeg++;
475 cbData -= cbThisSeg;
476 cb += cbThisSeg;
477 }
478 }
479
480 *pcSeg = cSeg;
481
482 return cb;
483}
484
485
486RTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck)
487{
488 RTSGBUF SgBufTmp;
489 RTSgBufClone(&SgBufTmp, pSgBuf);
490
491 bool fIsZero = true;
492 size_t cbLeft = cbCheck;
493 while (cbLeft)
494 {
495 size_t cbThisCheck = cbLeft;
496 void *pvBuf = rtSgBufGet(&SgBufTmp, &cbThisCheck);
497 if (!cbThisCheck)
498 break;
499 fIsZero = ASMMemIsZero(pvBuf, cbThisCheck);
500 if (!fIsZero)
501 break;
502 cbLeft -= cbThisCheck;
503 }
504
505 return fIsZero;
506}
507
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