Changeset 99960 in vbox for trunk/src/VBox/Runtime
- Timestamp:
- May 24, 2023 9:55:50 PM (19 months ago)
- Location:
- trunk/src/VBox/Runtime
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/sg.cpp
r98103 r99960 45 45 46 46 47 /** 48 * Gets the next continuous block of buffer data (up to @a *pcbData bytes) and 49 * advances the buffer position past it. 50 */ 47 51 static void *rtSgBufGet(PRTSGBUF pSgBuf, size_t *pcbData) 48 52 { 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 } 53 /* 54 * Check that the S/G buffer has memory left (!RTSgIsEnd(pSgBuf)). 55 */ 56 unsigned const idxSeg = pSgBuf->idxSeg; 57 unsigned const cSegs = pSgBuf->cSegs; 58 if (RT_LIKELY( idxSeg < cSegs 59 && ( pSgBuf->cbSegLeft > 0 /* paranoia, this condition shouldn't occur. */ 60 || idxSeg + 1 < cSegs))) 61 { 62 /* 63 * Grab the requested segment chunk. 64 */ 65 void * const pvBuf = pSgBuf->pvSegCur; 66 size_t const cbSegLeft = pSgBuf->cbSegLeft; 67 size_t const cbData = RT_MIN(*pcbData, cbSegLeft); 59 68 60 69 #ifndef RDESKTOP 61 AssertMsg(pSgBuf->cbSegLeft <= 128 * _1M62 && (uintptr_t)pSgBuf->pvSegCur>= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg63 && (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));70 AssertMsg( pSgBuf->cbSegLeft <= 128 * _1M 71 && (uintptr_t)pSgBuf->pvSegCur >= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg 72 && (uintptr_t)pSgBuf->pvSegCur + cbSegLeft <= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg + pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg, 73 ("pSgBuf->idxSeg=%d pSgBuf->cSegs=%d pSgBuf->pvSegCur=%p cbSegLeft=%zd pSgBuf->paSegs[%d].pvSeg=%p pSgBuf->paSegs[%d].cbSeg=%zd\n", 74 pSgBuf->idxSeg, pSgBuf->cSegs, pSgBuf->pvSegCur, cbSegLeft, pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg, pSgBuf->idxSeg, 75 pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg)); 67 76 #endif 68 77 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) 78 /* 79 * Advance ... 80 */ 81 if (cbSegLeft == cbData) 79 82 { 80 pSgBuf->pvSegCur = pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg; 81 pSgBuf->cbSegLeft = pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg; 83 *pcbData = cbData; 84 85 /* ... to the next segment. */ 86 unsigned idxSegNew = idxSeg + 1; 87 if (idxSegNew < pSgBuf->cSegs) 88 { 89 do 90 { 91 PCRTSGSEG const pNewSeg = &pSgBuf->paSegs[idxSegNew]; 92 if (pNewSeg->cbSeg > 0) 93 { 94 pSgBuf->idxSeg = idxSegNew; 95 pSgBuf->cbSegLeft = pNewSeg->cbSeg; 96 pSgBuf->pvSegCur = pNewSeg->pvSeg; 97 return pvBuf; 98 } 99 /* Empty segment, skip it. */ 100 idxSegNew++; 101 } while (idxSegNew < pSgBuf->cSegs); 102 } 103 pSgBuf->idxSeg = idxSegNew; 104 pSgBuf->cbSegLeft = 0; 105 pSgBuf->pvSegCur = NULL; 82 106 } 83 84 *pcbData = cbData; 85 } 86 else 87 pSgBuf->pvSegCur = (uint8_t *)pSgBuf->pvSegCur + cbData; 88 89 return pvBuf; 107 else 108 { 109 /* ... within the current segment. */ 110 Assert(*pcbData == cbData); 111 pSgBuf->cbSegLeft = cbSegLeft - cbData; 112 pSgBuf->pvSegCur = (uint8_t *)pvBuf + cbData; 113 } 114 115 return pvBuf; 116 } 117 118 Assert(pSgBuf->cbSegLeft == 0); 119 *pcbData = 0; 120 return NULL; 90 121 } 91 122 … … 208 239 Assert(cbTmp == cbThisCmp); 209 240 210 int rc= memcmp(pvBuf1, pvBuf2, cbThisCmp);211 if ( rc)212 return rc;241 int iDiff = memcmp(pvBuf1, pvBuf2, cbThisCmp); 242 if (iDiff) 243 return iDiff; 213 244 214 245 cbLeft -= cbThisCmp; … … 284 315 285 316 286 RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)287 { 288 AssertPtrReturn(pSgBuf, 0); 289 290 size_t cbLeft = cb Set;317 RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t bFill, size_t cbToSet) 318 { 319 AssertPtrReturn(pSgBuf, 0); 320 321 size_t cbLeft = cbToSet; 291 322 292 323 while (cbLeft) … … 294 325 size_t cbThisSet = cbLeft; 295 326 void *pvBuf = rtSgBufGet(pSgBuf, &cbThisSet); 296 297 if (!cbThisSet) 298 break; 299 300 memset(pvBuf, ubFill, cbThisSet); 327 if (!pvBuf) 328 break; 329 330 memset(pvBuf, bFill, cbThisSet); 301 331 302 332 cbLeft -= cbThisSet; 303 333 } 304 334 305 return cb Set - cbLeft;335 return cbToSet - cbLeft; 306 336 } 307 337 … … 317 347 { 318 348 size_t cbThisCopy = cbLeft; 319 void *pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy); 320 321 if (!cbThisCopy) 349 void * const pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy); 350 if (!pvSrc) 322 351 break; 323 352 324 353 memcpy(pvBuf, pvSrc, cbThisCopy); 325 354 355 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy); 326 356 cbLeft -= cbThisCopy; 327 pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);328 357 } 329 358 … … 342 371 { 343 372 size_t cbThisCopy = cbLeft; 344 void *pvDst = rtSgBufGet(pSgBuf, &cbThisCopy); 345 346 if (!cbThisCopy) 373 void * const pvDst = rtSgBufGet(pSgBuf, &cbThisCopy); 374 if (!pvDst) 347 375 break; 348 376 349 377 memcpy(pvDst, pvBuf, cbThisCopy); 350 378 379 pvBuf = (const void *)((uintptr_t)pvBuf + cbThisCopy); 351 380 cbLeft -= cbThisCopy; 352 pvBuf = (const void *)((uintptr_t)pvBuf + cbThisCopy);353 381 } 354 382 … … 367 395 { 368 396 size_t cbThisCopy = cbLeft; 369 void *pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy); 370 371 if (!cbThisCopy) 397 void * const pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy); 398 if (!pvSrc) 372 399 break; 373 400 … … 392 419 { 393 420 size_t cbThisCopy = cbLeft; 394 void *pvDst = rtSgBufGet(pSgBuf, &cbThisCopy); 395 396 if (!cbThisCopy) 421 void * const pvDst = rtSgBufGet(pSgBuf, &cbThisCopy); 422 if (!pvDst) 397 423 break; 398 424 … … 412 438 413 439 size_t cbLeft = cbAdvance; 440 414 441 while (cbLeft) 415 442 { 416 443 size_t cbThisAdvance = cbLeft; 417 rtSgBufGet(pSgBuf, &cbThisAdvance); 418 if (!cbThisAdvance) 444 if (!rtSgBufGet(pSgBuf, &cbThisAdvance)) 419 445 break; 420 446 … … 426 452 427 453 428 RTDECL(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;454 RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSegs, size_t cbData) 455 { 456 AssertPtrReturn(pSgBuf, 0); 457 AssertPtrReturn(pcSegs, 0); 458 459 unsigned cSegs = 0; 460 size_t cbRet = 0; 435 461 436 462 if (!paSeg) … … 439 465 { 440 466 size_t idx = pSgBuf->idxSeg; 441 cSeg = 1; 442 443 c b += RT_MIN(pSgBuf->cbSegLeft, cbData);444 cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);467 468 cbRet = RT_MIN(pSgBuf->cbSegLeft, cbData); 469 cSegs = cbRet != 0; 470 cbData -= cbRet; 445 471 446 472 while ( cbData … … 448 474 { 449 475 idx++; 450 cSeg++; 451 cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData); 452 cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData); 476 size_t cbThisSeg = RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData); 477 if (cbThisSeg) 478 { 479 cbRet += cbThisSeg; 480 cbData -= cbThisSeg; 481 cSegs++; 482 } 453 483 } 454 484 } … … 457 487 { 458 488 while ( cbData 459 && cSeg < *pcSeg)489 && cSegs < *pcSegs) 460 490 { 461 size_t cbThisSeg = cbData; 462 void *pvSeg = rtSgBufGet(pSgBuf, &cbThisSeg); 463 464 if (!cbThisSeg) 465 { 466 Assert(!pvSeg); 491 size_t cbThisSeg = cbData; 492 void * const pvSeg = rtSgBufGet(pSgBuf, &cbThisSeg); 493 if (!pvSeg) 467 494 break; 468 }469 495 470 496 AssertMsg(cbThisSeg <= cbData, ("Impossible!\n")); 471 497 472 paSeg[cSeg ].cbSeg = cbThisSeg;473 paSeg[cSeg ].pvSeg = pvSeg;474 cSeg ++;498 paSeg[cSegs].cbSeg = cbThisSeg; 499 paSeg[cSegs].pvSeg = pvSeg; 500 cSegs++; 475 501 cbData -= cbThisSeg; 476 cb 502 cbRet += cbThisSeg; 477 503 } 478 504 } 479 505 480 *pcSeg = cSeg;481 482 return cb ;506 *pcSegs = cSegs; 507 508 return cbRet; 483 509 } 484 510 … … 494 520 { 495 521 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; 522 void * const pvBuf = rtSgBufGet(&SgBufTmp, &cbThisCheck); 523 if (!pvBuf) 524 break; 525 if (cbThisCheck > 0) 526 { 527 fIsZero = ASMMemIsZero(pvBuf, cbThisCheck); 528 if (!fIsZero) 529 break; 530 cbLeft -= cbThisCheck; 531 } 503 532 } 504 533 -
trunk/src/VBox/Runtime/testcase/Makefile.kmk
r99801 r99960 142 142 tstRTSemRW \ 143 143 tstRTSemXRoads \ 144 tstRTSg \ 144 145 tstRTSort \ 145 146 tstRTStrAlloc \ … … 830 831 tstRTSemXRoads_SOURCES = tstRTSemXRoads.cpp 831 832 833 tstRTSg_TEMPLATE = VBoxR3TstExe 834 tstRTSg_SOURCES = tstRTSg.cpp 835 832 836 tstRTSort_TEMPLATE = VBoxR3TstExe 833 837 tstRTSort_SOURCES = tstRTSort.cpp
Note:
See TracChangeset
for help on using the changeset viewer.