sg.cpp revision 13493ab7596e827b8d0caab2c89e635dd65f78f9
1N/A/* $Id$ */
1N/A/** @file
1N/A * IPRT - S/G buffer handling.
1N/A */
1N/A
1N/A/*
1N/A * Copyright (C) 2010-2013 Oracle Corporation
1N/A *
1N/A * This file is part of VirtualBox Open Source Edition (OSE), as
1N/A * available from http://www.virtualbox.org. This file is free software;
1N/A * you can redistribute it and/or modify it under the terms of the GNU
1N/A * General Public License (GPL) as published by the Free Software
1N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
1N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1N/A *
1N/A * The contents of this file may alternatively be used under the terms
1N/A * of the Common Development and Distribution License Version 1.0
1N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1N/A * VirtualBox OSE distribution, in which case the provisions of the
1N/A * CDDL are applicable instead of those of the GPL.
1N/A *
1N/A * You may elect to license modified versions of this file under the
1N/A * terms and conditions of either the GPL or the CDDL or both.
1N/A */
1N/A
1N/A
1N/A/*******************************************************************************
1N/A* Header Files *
1N/A*******************************************************************************/
1N/A#include <iprt/sg.h>
1N/A#include <iprt/string.h>
1N/A#include <iprt/assert.h>
1N/A#include <iprt/asm.h>
1N/A
1N/A
1N/Astatic void *sgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
1N/A{
1N/A size_t cbData;
1N/A void *pvBuf;
1N/A
1N/A /* Check that the S/G buffer has memory left. */
1N/A if (RT_UNLIKELY( pSgBuf->idxSeg == pSgBuf->cSegs
1N/A && !pSgBuf->cbSegLeft))
1N/A {
1N/A *pcbData = 0;
1N/A return NULL;
1N/A }
1N/A
1N/A#ifndef RDESKTOP
1N/A AssertReleaseMsg( pSgBuf->cbSegLeft <= 32 * _1M
1N/A && (uintptr_t)pSgBuf->pvSegCur >= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg
1N/A && (uintptr_t)pSgBuf->pvSegCur + pSgBuf->cbSegLeft <= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg + pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg,
1N/A ("pSgBuf->idxSeg=%d pSgBuf->cSegs=%d pSgBuf->pvSegCur=%p pSgBuf->cbSegLeft=%zd pSgBuf->paSegs[%d].pvSeg=%p pSgBuf->paSegs[%d].cbSeg=%zd\n",
1N/A pSgBuf->idxSeg, pSgBuf->cSegs, pSgBuf->pvSegCur, pSgBuf->cbSegLeft,
1N/A pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg, pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg));
1N/A#endif
1N/A
1N/A cbData = RT_MIN(*pcbData, pSgBuf->cbSegLeft);
1N/A pvBuf = pSgBuf->pvSegCur;
1N/A pSgBuf->cbSegLeft -= cbData;
1N/A
1N/A /* Advance to the next segment if required. */
1N/A if (!pSgBuf->cbSegLeft)
1N/A {
1N/A pSgBuf->idxSeg++;
1N/A
1N/A if (pSgBuf->idxSeg < pSgBuf->cSegs)
1N/A {
1N/A pSgBuf->pvSegCur = pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg;
1N/A pSgBuf->cbSegLeft = pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg;
1N/A }
1N/A
1N/A *pcbData = cbData;
1N/A }
1N/A else
1N/A pSgBuf->pvSegCur = (uint8_t *)pSgBuf->pvSegCur + cbData;
1N/A
1N/A return pvBuf;
1N/A}
1N/A
1N/A
1N/ARTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs)
1N/A{
1N/A AssertPtr(pSgBuf);
1N/A AssertPtr(paSegs);
1N/A Assert(cSegs > 0);
1N/A Assert(cSegs < (~(unsigned)0 >> 1));
1N/A
1N/A pSgBuf->paSegs = paSegs;
1N/A pSgBuf->cSegs = (unsigned)cSegs;
1N/A pSgBuf->idxSeg = 0;
1N/A pSgBuf->pvSegCur = paSegs[0].pvSeg;
1N/A pSgBuf->cbSegLeft = paSegs[0].cbSeg;
1N/A}
1N/A
1N/A
1N/ARTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf)
1N/A{
1N/A AssertPtrReturnVoid(pSgBuf);
1N/A
1N/A pSgBuf->idxSeg = 0;
1N/A pSgBuf->pvSegCur = pSgBuf->paSegs[0].pvSeg;
1N/A pSgBuf->cbSegLeft = pSgBuf->paSegs[0].cbSeg;
1N/A}
1N/A
1N/A
1N/ARTDECL(void) RTSgBufClone(PRTSGBUF pSgBufTo, PCRTSGBUF pSgBufFrom)
1N/A{
1N/A AssertPtr(pSgBufTo);
1N/A AssertPtr(pSgBufFrom);
1N/A
1N/A pSgBufTo->paSegs = pSgBufFrom->paSegs;
1N/A pSgBufTo->cSegs = pSgBufFrom->cSegs;
1N/A pSgBufTo->idxSeg = pSgBufFrom->idxSeg;
1N/A pSgBufTo->pvSegCur = pSgBufFrom->pvSegCur;
1N/A pSgBufTo->cbSegLeft = pSgBufFrom->cbSegLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(void *) RTSgBufGetNextSegment(PRTSGBUF pSgBuf, size_t *pcbSeg)
1N/A{
1N/A AssertPtrReturn(pSgBuf, NULL);
1N/A AssertPtrReturn(pcbSeg, NULL);
1N/A
1N/A if (!*pcbSeg)
1N/A *pcbSeg = pSgBuf->cbSegLeft;
1N/A
1N/A return sgBufGet(pSgBuf, pcbSeg);
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy)
1N/A{
1N/A AssertPtrReturn(pSgBufDst, 0);
1N/A AssertPtrReturn(pSgBufSrc, 0);
1N/A
1N/A size_t cbLeft = cbCopy;
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
1N/A size_t cbTmp = cbThisCopy;
1N/A void *pvBufDst;
1N/A void *pvBufSrc;
1N/A
1N/A if (!cbThisCopy)
1N/A break;
1N/A
1N/A pvBufDst = sgBufGet(pSgBufDst, &cbTmp);
1N/A Assert(cbTmp == cbThisCopy);
1N/A pvBufSrc = sgBufGet(pSgBufSrc, &cbTmp);
1N/A Assert(cbTmp == cbThisCopy);
1N/A
1N/A memcpy(pvBufDst, pvBufSrc, cbThisCopy);
1N/A
1N/A cbLeft -= cbThisCopy;
1N/A }
1N/A
1N/A return cbCopy - cbLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp)
1N/A{
1N/A AssertPtrReturn(pSgBuf1, 0);
1N/A AssertPtrReturn(pSgBuf2, 0);
1N/A
1N/A size_t cbLeft = cbCmp;
1N/A RTSGBUF SgBuf1;
1N/A RTSGBUF SgBuf2;
1N/A
1N/A /* Set up the temporary buffers */
1N/A RTSgBufClone(&SgBuf1, pSgBuf1);
1N/A RTSgBufClone(&SgBuf2, pSgBuf2);
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
1N/A size_t cbTmp = cbThisCmp;
1N/A void *pvBuf1;
1N/A void *pvBuf2;
1N/A
1N/A if (!cbCmp)
1N/A break;
1N/A
1N/A pvBuf1 = sgBufGet(&SgBuf1, &cbTmp);
1N/A Assert(cbTmp == cbThisCmp);
1N/A pvBuf2 = sgBufGet(&SgBuf2, &cbTmp);
1N/A Assert(cbTmp == cbThisCmp);
1N/A
1N/A int rc = memcmp(pvBuf1, pvBuf2, cbThisCmp);
1N/A if (rc)
1N/A return rc;
1N/A
1N/A cbLeft -= cbThisCmp;
1N/A }
1N/A
1N/A return 0;
1N/A}
1N/A
1N/A
1N/ARTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
1N/A size_t *pcbOff, bool fAdvance)
1N/A{
1N/A AssertPtrReturn(pSgBuf1, 0);
1N/A AssertPtrReturn(pSgBuf2, 0);
1N/A
1N/A size_t cbLeft = cbCmp;
1N/A size_t cbOff = 0;
1N/A RTSGBUF SgBuf1Tmp;
1N/A RTSGBUF SgBuf2Tmp;
1N/A PRTSGBUF pSgBuf1Tmp;
1N/A PRTSGBUF pSgBuf2Tmp;
1N/A
1N/A if (!fAdvance)
1N/A {
1N/A /* Set up the temporary buffers */
1N/A RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
1N/A RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
1N/A pSgBuf1Tmp = &SgBuf1Tmp;
1N/A pSgBuf2Tmp = &SgBuf2Tmp;
1N/A }
1N/A else
1N/A {
1N/A pSgBuf1Tmp = pSgBuf1;
1N/A pSgBuf2Tmp = pSgBuf2;
1N/A }
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
1N/A size_t cbTmp = cbThisCmp;
1N/A uint8_t *pbBuf1;
1N/A uint8_t *pbBuf2;
1N/A
1N/A if (!cbCmp)
1N/A break;
1N/A
1N/A pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
1N/A Assert(cbTmp == cbThisCmp);
1N/A pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
1N/A Assert(cbTmp == cbThisCmp);
1N/A
1N/A int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
1N/A if (rc)
1N/A {
1N/A if (pcbOff)
1N/A {
1N/A /* Search for the correct offset */
1N/A while ( cbThisCmp-- > 0
1N/A && (*pbBuf1 == *pbBuf2))
1N/A {
1N/A pbBuf1++;
1N/A pbBuf2++;
1N/A cbOff++;
1N/A }
1N/A
1N/A *pcbOff = cbOff;
1N/A }
1N/A return rc;
1N/A }
1N/A
1N/A cbLeft -= cbThisCmp;
1N/A cbOff += cbThisCmp;
1N/A }
1N/A
1N/A return 0;
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
1N/A{
1N/A AssertPtrReturn(pSgBuf, 0);
1N/A
1N/A size_t cbLeft = cbSet;
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisSet = cbLeft;
1N/A void *pvBuf = sgBufGet(pSgBuf, &cbThisSet);
1N/A
1N/A if (!cbThisSet)
1N/A break;
1N/A
1N/A memset(pvBuf, ubFill, cbThisSet);
1N/A
1N/A cbLeft -= cbThisSet;
1N/A }
1N/A
1N/A return cbSet - cbLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
1N/A{
1N/A AssertPtrReturn(pSgBuf, 0);
1N/A AssertPtrReturn(pvBuf, 0);
1N/A
1N/A size_t cbLeft = cbCopy;
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCopy = cbLeft;
1N/A void *pvSrc = sgBufGet(pSgBuf, &cbThisCopy);
1N/A
1N/A if (!cbThisCopy)
1N/A break;
1N/A
1N/A memcpy(pvBuf, pvSrc, cbThisCopy);
1N/A
1N/A cbLeft -= cbThisCopy;
1N/A pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
1N/A }
1N/A
1N/A return cbCopy - cbLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, const void *pvBuf, size_t cbCopy)
1N/A{
1N/A AssertPtrReturn(pSgBuf, 0);
1N/A AssertPtrReturn(pvBuf, 0);
1N/A
1N/A size_t cbLeft = cbCopy;
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCopy = cbLeft;
1N/A void *pvDst = sgBufGet(pSgBuf, &cbThisCopy);
1N/A
1N/A if (!cbThisCopy)
1N/A break;
1N/A
1N/A memcpy(pvDst, pvBuf, cbThisCopy);
1N/A
1N/A cbLeft -= cbThisCopy;
1N/A pvBuf = (const void *)((uintptr_t)pvBuf + cbThisCopy);
1N/A }
1N/A
1N/A return cbCopy - cbLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance)
1N/A{
1N/A AssertPtrReturn(pSgBuf, 0);
1N/A
1N/A size_t cbLeft = cbAdvance;
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisAdvance = cbLeft;
1N/A void *pv = sgBufGet(pSgBuf, &cbThisAdvance);
1N/A
1N/A NOREF(pv);
1N/A
1N/A if (!cbThisAdvance)
1N/A break;
1N/A
1N/A cbLeft -= cbThisAdvance;
1N/A }
1N/A
1N/A return cbAdvance - cbLeft;
1N/A}
1N/A
1N/A
1N/ARTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData)
1N/A{
1N/A AssertPtrReturn(pSgBuf, 0);
1N/A AssertPtrReturn(pcSeg, 0);
1N/A
1N/A unsigned cSeg = 0;
1N/A size_t cb = 0;
1N/A
1N/A if (!paSeg)
1N/A {
1N/A if (pSgBuf->cbSegLeft > 0)
1N/A {
1N/A size_t idx = pSgBuf->idxSeg;
1N/A cSeg = 1;
1N/A
1N/A cb += RT_MIN(pSgBuf->cbSegLeft, cbData);
1N/A cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);
1N/A
1N/A while ( cbData
1N/A && idx < pSgBuf->cSegs - 1)
1N/A {
1N/A idx++;
1N/A cSeg++;
1N/A cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
1N/A cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
1N/A }
1N/A }
1N/A }
1N/A else
1N/A {
1N/A while ( cbData
1N/A && cSeg < *pcSeg)
1N/A {
1N/A size_t cbThisSeg = cbData;
1N/A void *pvSeg = NULL;
1N/A
1N/A pvSeg = sgBufGet(pSgBuf, &cbThisSeg);
1N/A
1N/A if (!cbThisSeg)
1N/A {
1N/A Assert(!pvSeg);
1N/A break;
1N/A }
1N/A
1N/A AssertMsg(cbThisSeg <= cbData, ("Impossible!\n"));
1N/A
1N/A paSeg[cSeg].cbSeg = cbThisSeg;
1N/A paSeg[cSeg].pvSeg = pvSeg;
1N/A cSeg++;
1N/A cbData -= cbThisSeg;
1N/A cb += cbThisSeg;
1N/A }
1N/A }
1N/A
1N/A *pcSeg = cSeg;
1N/A
1N/A return cb;
1N/A}
1N/A
1N/ARTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck)
1N/A{
1N/A bool fIsZero = true;
1N/A size_t cbLeft = cbCheck;
1N/A RTSGBUF SgBufTmp;
1N/A
1N/A RTSgBufClone(&SgBufTmp, pSgBuf);
1N/A
1N/A while (cbLeft)
1N/A {
1N/A size_t cbThisCheck = cbLeft;
1N/A void *pvBuf = sgBufGet(&SgBufTmp, &cbThisCheck);
1N/A
1N/A if (!cbThisCheck)
1N/A break;
1N/A
1N/A /* Use optimized inline assembler if possible. */
1N/A if ( !(cbThisCheck % 4)
1N/A && (cbThisCheck * 8 <= UINT32_MAX))
1N/A {
1N/A if (ASMBitFirstSet((volatile void *)pvBuf, (uint32_t)cbThisCheck * 8) != -1)
1N/A {
1N/A fIsZero = false;
1N/A break;
1N/A }
1N/A }
1N/A else
1N/A {
1N/A for (unsigned i = 0; i < cbThisCheck; i++)
1N/A {
1N/A char *pbBuf = (char *)pvBuf;
1N/A if (*pbBuf)
1N/A {
1N/A fIsZero = false;
1N/A break;
1N/A }
1N/A pvBuf = pbBuf + 1;
1N/A }
1N/A
1N/A if (!fIsZero)
1N/A break;
1N/A }
1N/A
1N/A cbLeft -= cbThisCheck;
1N/A }
1N/A
1N/A return fIsZero;
1N/A}
1N/A
1N/A