VirtualBox

Changeset 44241 in vbox


Ignore:
Timestamp:
Jan 7, 2013 8:17:19 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
83097
Message:

Runtime/sg.cpp: Add API to check a buffer for zeros

Location:
trunk
Files:
3 edited

Legend:

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

    r43291 r44241  
    11731173# define RTSgBufCopyToBuf                               RT_MANGLER(RTSgBufCopyToBuf)
    11741174# define RTSgBufInit                                    RT_MANGLER(RTSgBufInit)
     1175# define RTSgBufIsZero                                  RT_MANGLER(RTSgBufIsZero)
    11751176# define RTSgBufReset                                   RT_MANGLER(RTSgBufReset)
    11761177# define RTSgBufSegArrayCreate                          RT_MANGLER(RTSgBufSegArrayCreate)
  • trunk/include/iprt/sg.h

    r38539 r44241  
    220220 */
    221221RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData);
     222
     223/**
     224 * Returns whether the given S/G buffer is zeroed out from the current position
     225 * upto the number of bytes to check.
     226 *
     227 * @returns true if the buffer has only zeros
     228 *          false otherwise.
     229 * @param   pSgBuf      The S/G buffer.
     230 * @param   cbCheck     Number of bytes to check.
     231 */
     232RTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck);
    222233
    223234/**
  • trunk/src/VBox/Runtime/common/misc/sg.cpp

    r44135 r44241  
    3232#include <iprt/string.h>
    3333#include <iprt/assert.h>
     34#include <iprt/asm.h>
    3435
    3536
     
    422423}
    423424
     425RTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck)
     426{
     427    bool fIsZero = true;
     428    size_t cbLeft = cbCheck;
     429    RTSGBUF SgBufTmp;
     430
     431    RTSgBufClone(&SgBufTmp, pSgBuf);
     432
     433    while (cbLeft)
     434    {
     435        size_t cbThisCheck = cbLeft;
     436        void *pvBuf = sgBufGet(&SgBufTmp, &cbThisCheck);
     437
     438        if (!cbThisCheck)
     439            break;
     440
     441        /* Use optimized inline assembler if possible. */
     442        if (   !(cbThisCheck % 4)
     443            && (cbThisCheck * 8 <= UINT32_MAX))
     444        {
     445            if (ASMBitFirstSet((volatile void *)pvBuf, cbThisCheck * 8) != -1)
     446            {
     447                fIsZero = false;
     448                break;
     449            }
     450        }
     451        else
     452        {
     453            for (unsigned i = 0; i < cbThisCheck; i++)
     454            {
     455                char *pbBuf = (char *)pvBuf;
     456                if (*pbBuf)
     457                {
     458                    fIsZero = false;
     459                    break;
     460                }
     461                pvBuf = pbBuf + 1;
     462            }
     463
     464            if (!fIsZero)
     465                break;
     466        }
     467
     468        cbLeft -= cbThisCheck;
     469    }
     470
     471    return fIsZero;
     472}
     473
Note: See TracChangeset for help on using the changeset viewer.

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