VirtualBox

Changeset 59745 in vbox


Ignore:
Timestamp:
Feb 19, 2016 9:54:11 PM (9 years ago)
Author:
vboxsync
Message:

sg.cpp/h: Some cleanups/optimizations.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/sg.h

    r56291 r59745  
    149149 * @param   pSgBuf2      Second S/G buffer.
    150150 * @param   cbCmp        How many bytes to compare.
    151  * @param   pcbOff       Where to store the offset of the first different byte
     151 * @param   poffDiff     Where to store the offset of the first different byte
    152152 *                       in the buffer starting from the position of the S/G
    153153 *                       buffer before this call.
  • trunk/src/VBox/Runtime/common/misc/sg.cpp

    r58334 r59745  
    3535
    3636
    37 static void *sgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
     37static void *rtSgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
    3838{
    3939    size_t cbData;
     
    127127        *pcbSeg = pSgBuf->cbSegLeft;
    128128
    129     return sgBufGet(pSgBuf, pcbSeg);
     129    return rtSgBufGet(pSgBuf, pcbSeg);
    130130}
    131131
     
    137137
    138138    size_t cbLeft = cbCopy;
    139 
    140139    while (cbLeft)
    141140    {
    142141        size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
     142        if (!cbThisCopy)
     143            break;
     144
    143145        size_t cbTmp = cbThisCopy;
    144         void *pvBufDst;
    145         void *pvBufSrc;
    146 
    147         if (!cbThisCopy)
    148             break;
    149 
    150         pvBufDst = sgBufGet(pSgBufDst, &cbTmp);
     146        void *pvBufDst = rtSgBufGet(pSgBufDst, &cbTmp);
    151147        Assert(cbTmp == cbThisCopy);
    152         pvBufSrc = sgBufGet(pSgBufSrc, &cbTmp);
     148        void *pvBufSrc = rtSgBufGet(pSgBufSrc, &cbTmp);
    153149        Assert(cbTmp == cbThisCopy);
    154150
     
    167163    AssertPtrReturn(pSgBuf2, 0);
    168164
     165    /* Set up the temporary buffers */
     166    RTSGBUF SgBuf1;
     167    RTSgBufClone(&SgBuf1, pSgBuf1);
     168    RTSGBUF SgBuf2;
     169    RTSgBufClone(&SgBuf2, pSgBuf2);
     170
    169171    size_t cbLeft = cbCmp;
    170     RTSGBUF SgBuf1;
    171     RTSGBUF SgBuf2;
    172 
    173     /* Set up the temporary buffers */
    174     RTSgBufClone(&SgBuf1, pSgBuf1);
    175     RTSgBufClone(&SgBuf2, pSgBuf2);
    176 
    177172    while (cbLeft)
    178173    {
    179174        size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
     175        if (!cbThisCmp)
     176            break;
     177
    180178        size_t cbTmp = cbThisCmp;
    181         void *pvBuf1;
    182         void *pvBuf2;
    183 
    184         if (!cbCmp)
    185             break;
    186 
    187         pvBuf1 = sgBufGet(&SgBuf1, &cbTmp);
     179        void *pvBuf1 = rtSgBufGet(&SgBuf1, &cbTmp);
    188180        Assert(cbTmp == cbThisCmp);
    189         pvBuf2 = sgBufGet(&SgBuf2, &cbTmp);
     181        void *pvBuf2 = rtSgBufGet(&SgBuf2, &cbTmp);
    190182        Assert(cbTmp == cbThisCmp);
    191183
     
    201193
    202194
    203 RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
    204                          size_t *pcbOff, bool fAdvance)
     195RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp, size_t *poffDiff, bool fAdvance)
    205196{
    206197    AssertPtrReturn(pSgBuf1, 0);
    207198    AssertPtrReturn(pSgBuf2, 0);
    208199
    209     size_t cbLeft = cbCmp;
    210     size_t cbOff  = 0;
    211200    RTSGBUF SgBuf1Tmp;
    212201    RTSGBUF SgBuf2Tmp;
     
    228217    }
    229218
     219    size_t cbLeft = cbCmp;
     220    size_t off    = 0;
    230221    while (cbLeft)
    231222    {
    232223        size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
     224        if (!cbThisCmp)
     225            break;
     226
    233227        size_t cbTmp = cbThisCmp;
    234         uint8_t *pbBuf1;
    235         uint8_t *pbBuf2;
    236 
    237         if (!cbCmp)
    238             break;
    239 
    240         pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
     228        uint8_t *pbBuf1 = (uint8_t *)rtSgBufGet(pSgBuf1Tmp, &cbTmp);
    241229        Assert(cbTmp == cbThisCmp);
    242         pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
     230        uint8_t *pbBuf2 = (uint8_t *)rtSgBufGet(pSgBuf2Tmp, &cbTmp);
    243231        Assert(cbTmp == cbThisCmp);
    244232
    245         int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
    246         if (rc)
    247         {
    248             if (pcbOff)
     233        int iDiff = memcmp(pbBuf1, pbBuf2, cbThisCmp);
     234        if (iDiff)
     235        {
     236            /* Locate the first byte that differs if the caller requested this. */
     237            if (poffDiff)
    249238            {
    250                 /* Search for the correct offset */
    251239                while (   cbThisCmp-- > 0
    252                        && (*pbBuf1 == *pbBuf2))
     240                       && *pbBuf1 == *pbBuf2)
    253241                {
    254242                    pbBuf1++;
    255243                    pbBuf2++;
    256                     cbOff++;
     244                    off++;
    257245                }
    258246
    259                 *pcbOff = cbOff;
     247                *poffDiff = off;
    260248            }
    261             return rc;
     249            return iDiff;
    262250        }
    263251
    264252        cbLeft -= cbThisCmp;
    265         cbOff  += cbThisCmp;
     253        off    += cbThisCmp;
    266254    }
    267255
     
    279267    {
    280268        size_t cbThisSet = cbLeft;
    281         void *pvBuf = sgBufGet(pSgBuf, &cbThisSet);
     269        void *pvBuf = rtSgBufGet(pSgBuf, &cbThisSet);
    282270
    283271        if (!cbThisSet)
     
    303291    {
    304292        size_t cbThisCopy = cbLeft;
    305         void *pvSrc = sgBufGet(pSgBuf, &cbThisCopy);
     293        void *pvSrc = rtSgBufGet(pSgBuf, &cbThisCopy);
    306294
    307295        if (!cbThisCopy)
     
    328316    {
    329317        size_t cbThisCopy = cbLeft;
    330         void *pvDst = sgBufGet(pSgBuf, &cbThisCopy);
     318        void *pvDst = rtSgBufGet(pSgBuf, &cbThisCopy);
    331319
    332320        if (!cbThisCopy)
     
    348336
    349337    size_t cbLeft = cbAdvance;
    350 
    351338    while (cbLeft)
    352339    {
    353340        size_t cbThisAdvance = cbLeft;
    354         void *pv = sgBufGet(pSgBuf, &cbThisAdvance);
    355 
    356         NOREF(pv);
    357 
     341        rtSgBufGet(pSgBuf, &cbThisAdvance);
    358342        if (!cbThisAdvance)
    359343            break;
     
    400384        {
    401385            size_t  cbThisSeg = cbData;
    402             void   *pvSeg     = NULL;
    403 
    404             pvSeg = sgBufGet(pSgBuf, &cbThisSeg);
     386            void *pvSeg = rtSgBufGet(pSgBuf, &cbThisSeg);
    405387
    406388            if (!cbThisSeg)
     
    427409RTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck)
    428410{
    429     bool fIsZero = true;
    430     size_t cbLeft = cbCheck;
    431411    RTSGBUF SgBufTmp;
    432 
    433412    RTSgBufClone(&SgBufTmp, pSgBuf);
    434413
     414    bool   fIsZero = true;
     415    size_t cbLeft  = cbCheck;
    435416    while (cbLeft)
    436417    {
    437418        size_t cbThisCheck = cbLeft;
    438         void *pvBuf = sgBufGet(&SgBufTmp, &cbThisCheck);
    439 
     419        void *pvBuf = rtSgBufGet(&SgBufTmp, &cbThisCheck);
    440420        if (!cbThisCheck)
    441421            break;
    442422
     423/** @todo fix this after asm.h gets updated.    */
    443424        /* Use optimized inline assembler if possible. */
    444425        if (   !(cbThisCheck % 4)
    445             && (cbThisCheck * 8 <= UINT32_MAX))
     426            && cbThisCheck * 8 <= UINT32_MAX)
    446427        {
    447428            if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbThisCheck * 8) != -1)
     
    453434        else
    454435        {
    455             for (unsigned i = 0; i < cbThisCheck; i++)
     436            for (size_t i = 0; i < cbThisCheck; i++)
    456437            {
    457438                char *pbBuf = (char *)pvBuf;
Note: See TracChangeset for help on using the changeset viewer.

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