VirtualBox

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

Last change on this file since 36597 was 36312, checked in by vboxsync, 14 years ago

Runtime/common/misc/sg: temporarily sanity check

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/* $Id: sg.cpp 36312 2011-03-18 12:59:15Z vboxsync $ */
2/** @file
3 * IPRT - S/G buffer handling.
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/sg.h>
32#include <iprt/string.h>
33#include <iprt/assert.h>
34
35
36static void *sgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
37{
38 size_t cbData = RT_MIN(*pcbData, pSgBuf->cbSegLeft);
39 void *pvBuf = pSgBuf->pvSegCur;
40
41 AssertReleaseMsg( pSgBuf->cbSegLeft <= 5 * _1M
42 && (uintptr_t)pSgBuf->pvSegCur >= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg
43 && (uintptr_t)pSgBuf->pvSegCur + pSgBuf->cbSegLeft <= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg + pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg,
44 ("pSgBuf->idxSeg=%d pSgBuf->cSegs=%d pSgBuf->pvSegCur=%p pSgBuf->cbSegLeft=%zd pSgBuf->paSegs[%d].pvSeg=%p pSgBuf->paSegs[%d].cbSeg=%zd\n",
45 pSgBuf->idxSeg, pSgBuf->cSegs, pSgBuf->pvSegCur, pSgBuf->cbSegLeft,
46 pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg, pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg));
47
48 pSgBuf->cbSegLeft -= cbData;
49
50 /* Advance to the next segment if required. */
51 if (!pSgBuf->cbSegLeft)
52 {
53 pSgBuf->idxSeg++;
54
55 if (RT_UNLIKELY(pSgBuf->idxSeg == pSgBuf->cSegs))
56 {
57 pSgBuf->cbSegLeft = 0;
58 pSgBuf->pvSegCur = NULL;
59 }
60 else
61 {
62 pSgBuf->pvSegCur = pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg;
63 pSgBuf->cbSegLeft = pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg;
64 }
65
66 *pcbData = cbData;
67 }
68 else
69 pSgBuf->pvSegCur = (uint8_t *)pSgBuf->pvSegCur + cbData;
70
71 return pvBuf;
72}
73
74
75RTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs)
76{
77 AssertPtr(pSgBuf);
78 AssertPtr(paSegs);
79 Assert(cSegs > 0);
80 Assert(cSegs < (~(unsigned)0 >> 1));
81
82 pSgBuf->paSegs = paSegs;
83 pSgBuf->cSegs = (unsigned)cSegs;
84 pSgBuf->idxSeg = 0;
85 pSgBuf->pvSegCur = paSegs[0].pvSeg;
86 pSgBuf->cbSegLeft = paSegs[0].cbSeg;
87}
88
89
90RTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf)
91{
92 AssertPtrReturnVoid(pSgBuf);
93
94 pSgBuf->idxSeg = 0;
95 pSgBuf->pvSegCur = pSgBuf->paSegs[0].pvSeg;
96 pSgBuf->cbSegLeft = pSgBuf->paSegs[0].cbSeg;
97}
98
99
100RTDECL(void) RTSgBufClone(PRTSGBUF pSgBufTo, PCRTSGBUF pSgBufFrom)
101{
102 AssertPtr(pSgBufTo);
103 AssertPtr(pSgBufFrom);
104
105 pSgBufTo->paSegs = pSgBufFrom->paSegs;
106 pSgBufTo->cSegs = pSgBufFrom->cSegs;
107 pSgBufTo->idxSeg = pSgBufFrom->idxSeg;
108 pSgBufTo->pvSegCur = pSgBufFrom->pvSegCur;
109 pSgBufTo->cbSegLeft = pSgBufFrom->cbSegLeft;
110}
111
112
113RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy)
114{
115 AssertPtrReturn(pSgBufDst, 0);
116 AssertPtrReturn(pSgBufSrc, 0);
117
118 size_t cbLeft = cbCopy;
119
120 while (cbLeft)
121 {
122 size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
123 size_t cbTmp = cbThisCopy;
124 void *pvBufDst;
125 void *pvBufSrc;
126
127 if (!cbThisCopy)
128 break;
129
130 pvBufDst = sgBufGet(pSgBufDst, &cbTmp);
131 Assert(cbTmp == cbThisCopy);
132 pvBufSrc = sgBufGet(pSgBufSrc, &cbTmp);
133 Assert(cbTmp == cbThisCopy);
134
135 memcpy(pvBufDst, pvBufSrc, cbThisCopy);
136
137 cbLeft -= cbThisCopy;
138 }
139
140 return cbCopy - cbLeft;
141}
142
143
144RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp)
145{
146 AssertPtrReturn(pSgBuf1, 0);
147 AssertPtrReturn(pSgBuf2, 0);
148
149 size_t cbLeft = cbCmp;
150 RTSGBUF SgBuf1;
151 RTSGBUF SgBuf2;
152
153 /* Set up the temporary buffers */
154 RTSgBufClone(&SgBuf1, pSgBuf1);
155 RTSgBufClone(&SgBuf2, pSgBuf2);
156
157 while (cbLeft)
158 {
159 size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
160 size_t cbTmp = cbThisCmp;
161 void *pvBuf1;
162 void *pvBuf2;
163
164 if (!cbCmp)
165 break;
166
167 pvBuf1 = sgBufGet(&SgBuf1, &cbTmp);
168 Assert(cbTmp == cbThisCmp);
169 pvBuf2 = sgBufGet(&SgBuf2, &cbTmp);
170 Assert(cbTmp == cbThisCmp);
171
172 int rc = memcmp(pvBuf1, pvBuf2, cbThisCmp);
173 if (rc)
174 return rc;
175
176 cbLeft -= cbThisCmp;
177 }
178
179 return 0;
180}
181
182
183RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
184 size_t *pcbOff, bool fAdvance)
185{
186 AssertPtrReturn(pSgBuf1, 0);
187 AssertPtrReturn(pSgBuf2, 0);
188
189 size_t cbLeft = cbCmp;
190 size_t cbOff = 0;
191 RTSGBUF SgBuf1Tmp;
192 RTSGBUF SgBuf2Tmp;
193 PRTSGBUF pSgBuf1Tmp;
194 PRTSGBUF pSgBuf2Tmp;
195
196 if (!fAdvance)
197 {
198 /* Set up the temporary buffers */
199 RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
200 RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
201 pSgBuf1Tmp = &SgBuf1Tmp;
202 pSgBuf2Tmp = &SgBuf2Tmp;
203 }
204 else
205 {
206 pSgBuf1Tmp = pSgBuf1;
207 pSgBuf2Tmp = pSgBuf2;
208 }
209
210 while (cbLeft)
211 {
212 size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
213 size_t cbTmp = cbThisCmp;
214 uint8_t *pbBuf1;
215 uint8_t *pbBuf2;
216
217 if (!cbCmp)
218 break;
219
220 pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
221 Assert(cbTmp == cbThisCmp);
222 pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
223 Assert(cbTmp == cbThisCmp);
224
225 int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
226 if (rc)
227 {
228 if (pcbOff)
229 {
230 /* Search for the correct offset */
231 while ( cbThisCmp-- > 0
232 && (*pbBuf1 == *pbBuf2))
233 {
234 pbBuf1++;
235 pbBuf2++;
236 cbOff++;
237 }
238
239 *pcbOff = cbOff;
240 }
241 return rc;
242 }
243
244 cbLeft -= cbThisCmp;
245 cbOff += cbThisCmp;
246 }
247
248 return 0;
249}
250
251
252RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
253{
254 AssertPtrReturn(pSgBuf, 0);
255
256 size_t cbLeft = cbSet;
257
258 while (cbLeft)
259 {
260 size_t cbThisSet = cbLeft;
261 void *pvBuf = sgBufGet(pSgBuf, &cbThisSet);
262
263 if (!cbThisSet)
264 break;
265
266 memset(pvBuf, ubFill, cbThisSet);
267
268 cbLeft -= cbThisSet;
269 }
270
271 return cbSet - cbLeft;
272}
273
274
275RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
276{
277 AssertPtrReturn(pSgBuf, 0);
278 AssertPtrReturn(pvBuf, 0);
279
280 size_t cbLeft = cbCopy;
281
282 while (cbLeft)
283 {
284 size_t cbThisCopy = cbLeft;
285 void *pvSrc = sgBufGet(pSgBuf, &cbThisCopy);
286
287 if (!cbThisCopy)
288 break;
289
290 memcpy(pvBuf, pvSrc, cbThisCopy);
291
292 cbLeft -= cbThisCopy;
293 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
294 }
295
296 return cbCopy - cbLeft;
297}
298
299
300RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
301{
302 AssertPtrReturn(pSgBuf, 0);
303 AssertPtrReturn(pvBuf, 0);
304
305 size_t cbLeft = cbCopy;
306
307 while (cbLeft)
308 {
309 size_t cbThisCopy = cbLeft;
310 void *pvDst = sgBufGet(pSgBuf, &cbThisCopy);
311
312 if (!cbThisCopy)
313 break;
314
315 memcpy(pvDst, pvBuf, cbThisCopy);
316
317 cbLeft -= cbThisCopy;
318 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
319 }
320
321 return cbCopy - cbLeft;
322}
323
324
325RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance)
326{
327 AssertPtrReturn(pSgBuf, 0);
328
329 size_t cbLeft = cbAdvance;
330
331 while (cbLeft)
332 {
333 size_t cbThisAdvance = cbLeft;
334 void *pv = sgBufGet(pSgBuf, &cbThisAdvance);
335
336 NOREF(pv);
337
338 if (!cbThisAdvance)
339 break;
340
341 cbLeft -= cbThisAdvance;
342 }
343
344 return cbAdvance - cbLeft;
345}
346
347
348RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData)
349{
350 AssertPtrReturn(pSgBuf, 0);
351 AssertPtrReturn(pcSeg, 0);
352
353 unsigned cSeg = 0;
354 size_t cb = 0;
355
356 if (!paSeg)
357 {
358 if (pSgBuf->cbSegLeft > 0)
359 {
360 size_t idx = pSgBuf->idxSeg;
361 cSeg = 1;
362
363 cb += RT_MIN(pSgBuf->cbSegLeft, cbData);
364 cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);
365
366 while ( cbData
367 && idx < pSgBuf->cSegs - 1)
368 {
369 idx++;
370 cSeg++;
371 cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
372 cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
373 }
374 }
375 }
376 else
377 {
378 while ( cbData
379 && cSeg < *pcSeg)
380 {
381 size_t cbThisSeg = cbData;
382 void *pvSeg = NULL;
383
384 pvSeg = sgBufGet(pSgBuf, &cbThisSeg);
385
386 if (!cbThisSeg)
387 {
388 Assert(!pvSeg);
389 break;
390 }
391
392 AssertMsg(cbThisSeg <= cbData, ("Impossible!\n"));
393
394 paSeg[cSeg].cbSeg = cbThisSeg;
395 paSeg[cSeg].pvSeg = pvSeg;
396 cSeg++;
397 cbData -= cbThisSeg;
398 cb += cbThisSeg;
399 }
400 }
401
402 *pcSeg = cSeg;
403
404 return cb;
405}
406
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette