VirtualBox

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

最後變更 在這個檔案從38539是 38539,由 vboxsync 提交於 14 年 前

Runtime/SgBuf: Add new method to get the next segment from the S/G buffer

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.2 KB
 
1/* $Id: sg.cpp 38539 2011-08-25 22:06:11Z 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.alldomusa.eu.org. 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(void *) RTSgBufGetNextSegment(PRTSGBUF pSgBuf, size_t *pcbSeg)
114{
115 AssertPtrReturn(pSgBuf, NULL);
116 AssertPtrReturn(pcbSeg, NULL);
117
118 if (!*pcbSeg)
119 *pcbSeg = pSgBuf->cbSegLeft;
120
121 return sgBufGet(pSgBuf, pcbSeg);
122}
123
124
125RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy)
126{
127 AssertPtrReturn(pSgBufDst, 0);
128 AssertPtrReturn(pSgBufSrc, 0);
129
130 size_t cbLeft = cbCopy;
131
132 while (cbLeft)
133 {
134 size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
135 size_t cbTmp = cbThisCopy;
136 void *pvBufDst;
137 void *pvBufSrc;
138
139 if (!cbThisCopy)
140 break;
141
142 pvBufDst = sgBufGet(pSgBufDst, &cbTmp);
143 Assert(cbTmp == cbThisCopy);
144 pvBufSrc = sgBufGet(pSgBufSrc, &cbTmp);
145 Assert(cbTmp == cbThisCopy);
146
147 memcpy(pvBufDst, pvBufSrc, cbThisCopy);
148
149 cbLeft -= cbThisCopy;
150 }
151
152 return cbCopy - cbLeft;
153}
154
155
156RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp)
157{
158 AssertPtrReturn(pSgBuf1, 0);
159 AssertPtrReturn(pSgBuf2, 0);
160
161 size_t cbLeft = cbCmp;
162 RTSGBUF SgBuf1;
163 RTSGBUF SgBuf2;
164
165 /* Set up the temporary buffers */
166 RTSgBufClone(&SgBuf1, pSgBuf1);
167 RTSgBufClone(&SgBuf2, pSgBuf2);
168
169 while (cbLeft)
170 {
171 size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
172 size_t cbTmp = cbThisCmp;
173 void *pvBuf1;
174 void *pvBuf2;
175
176 if (!cbCmp)
177 break;
178
179 pvBuf1 = sgBufGet(&SgBuf1, &cbTmp);
180 Assert(cbTmp == cbThisCmp);
181 pvBuf2 = sgBufGet(&SgBuf2, &cbTmp);
182 Assert(cbTmp == cbThisCmp);
183
184 int rc = memcmp(pvBuf1, pvBuf2, cbThisCmp);
185 if (rc)
186 return rc;
187
188 cbLeft -= cbThisCmp;
189 }
190
191 return 0;
192}
193
194
195RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
196 size_t *pcbOff, bool fAdvance)
197{
198 AssertPtrReturn(pSgBuf1, 0);
199 AssertPtrReturn(pSgBuf2, 0);
200
201 size_t cbLeft = cbCmp;
202 size_t cbOff = 0;
203 RTSGBUF SgBuf1Tmp;
204 RTSGBUF SgBuf2Tmp;
205 PRTSGBUF pSgBuf1Tmp;
206 PRTSGBUF pSgBuf2Tmp;
207
208 if (!fAdvance)
209 {
210 /* Set up the temporary buffers */
211 RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
212 RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
213 pSgBuf1Tmp = &SgBuf1Tmp;
214 pSgBuf2Tmp = &SgBuf2Tmp;
215 }
216 else
217 {
218 pSgBuf1Tmp = pSgBuf1;
219 pSgBuf2Tmp = pSgBuf2;
220 }
221
222 while (cbLeft)
223 {
224 size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
225 size_t cbTmp = cbThisCmp;
226 uint8_t *pbBuf1;
227 uint8_t *pbBuf2;
228
229 if (!cbCmp)
230 break;
231
232 pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
233 Assert(cbTmp == cbThisCmp);
234 pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
235 Assert(cbTmp == cbThisCmp);
236
237 int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
238 if (rc)
239 {
240 if (pcbOff)
241 {
242 /* Search for the correct offset */
243 while ( cbThisCmp-- > 0
244 && (*pbBuf1 == *pbBuf2))
245 {
246 pbBuf1++;
247 pbBuf2++;
248 cbOff++;
249 }
250
251 *pcbOff = cbOff;
252 }
253 return rc;
254 }
255
256 cbLeft -= cbThisCmp;
257 cbOff += cbThisCmp;
258 }
259
260 return 0;
261}
262
263
264RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
265{
266 AssertPtrReturn(pSgBuf, 0);
267
268 size_t cbLeft = cbSet;
269
270 while (cbLeft)
271 {
272 size_t cbThisSet = cbLeft;
273 void *pvBuf = sgBufGet(pSgBuf, &cbThisSet);
274
275 if (!cbThisSet)
276 break;
277
278 memset(pvBuf, ubFill, cbThisSet);
279
280 cbLeft -= cbThisSet;
281 }
282
283 return cbSet - cbLeft;
284}
285
286
287RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
288{
289 AssertPtrReturn(pSgBuf, 0);
290 AssertPtrReturn(pvBuf, 0);
291
292 size_t cbLeft = cbCopy;
293
294 while (cbLeft)
295 {
296 size_t cbThisCopy = cbLeft;
297 void *pvSrc = sgBufGet(pSgBuf, &cbThisCopy);
298
299 if (!cbThisCopy)
300 break;
301
302 memcpy(pvBuf, pvSrc, cbThisCopy);
303
304 cbLeft -= cbThisCopy;
305 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
306 }
307
308 return cbCopy - cbLeft;
309}
310
311
312RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
313{
314 AssertPtrReturn(pSgBuf, 0);
315 AssertPtrReturn(pvBuf, 0);
316
317 size_t cbLeft = cbCopy;
318
319 while (cbLeft)
320 {
321 size_t cbThisCopy = cbLeft;
322 void *pvDst = sgBufGet(pSgBuf, &cbThisCopy);
323
324 if (!cbThisCopy)
325 break;
326
327 memcpy(pvDst, pvBuf, cbThisCopy);
328
329 cbLeft -= cbThisCopy;
330 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
331 }
332
333 return cbCopy - cbLeft;
334}
335
336
337RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance)
338{
339 AssertPtrReturn(pSgBuf, 0);
340
341 size_t cbLeft = cbAdvance;
342
343 while (cbLeft)
344 {
345 size_t cbThisAdvance = cbLeft;
346 void *pv = sgBufGet(pSgBuf, &cbThisAdvance);
347
348 NOREF(pv);
349
350 if (!cbThisAdvance)
351 break;
352
353 cbLeft -= cbThisAdvance;
354 }
355
356 return cbAdvance - cbLeft;
357}
358
359
360RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData)
361{
362 AssertPtrReturn(pSgBuf, 0);
363 AssertPtrReturn(pcSeg, 0);
364
365 unsigned cSeg = 0;
366 size_t cb = 0;
367
368 if (!paSeg)
369 {
370 if (pSgBuf->cbSegLeft > 0)
371 {
372 size_t idx = pSgBuf->idxSeg;
373 cSeg = 1;
374
375 cb += RT_MIN(pSgBuf->cbSegLeft, cbData);
376 cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);
377
378 while ( cbData
379 && idx < pSgBuf->cSegs - 1)
380 {
381 idx++;
382 cSeg++;
383 cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
384 cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
385 }
386 }
387 }
388 else
389 {
390 while ( cbData
391 && cSeg < *pcSeg)
392 {
393 size_t cbThisSeg = cbData;
394 void *pvSeg = NULL;
395
396 pvSeg = sgBufGet(pSgBuf, &cbThisSeg);
397
398 if (!cbThisSeg)
399 {
400 Assert(!pvSeg);
401 break;
402 }
403
404 AssertMsg(cbThisSeg <= cbData, ("Impossible!\n"));
405
406 paSeg[cSeg].cbSeg = cbThisSeg;
407 paSeg[cSeg].pvSeg = pvSeg;
408 cSeg++;
409 cbData -= cbThisSeg;
410 cb += cbThisSeg;
411 }
412 }
413
414 *pcSeg = cSeg;
415
416 return cb;
417}
418
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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