sg.cpp revision 89c28c87cc69edce638703dbfa181d6d67daf17b
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync/* $Id$ */
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync/** @file
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync * IPRT - S/G buffer handling.
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync */
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync/*
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync * Copyright (C) 2010 Oracle Corporation
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync *
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * available from http://www.virtualbox.org. This file is free software;
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * The contents of this file may alternatively be used under the terms
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * of the Common Development and Distribution License Version 1.0
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution, in which case the provisions of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * CDDL are applicable instead of those of the GPL.
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync *
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * You may elect to license modified versions of this file under the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * terms and conditions of either the GPL or the CDDL or both.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync */
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync/*******************************************************************************
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync* Header Files *
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync*******************************************************************************/
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync#include <iprt/sg.h>
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync#include <iprt/string.h>
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync#include <iprt/assert.h>
cf57145d4697dceca3f0542b370c20f7a2c5c6e8vboxsync
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsyncstatic void *sgBufGet(PRTSGBUF pSgBuf, size_t *pcbData)
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync{
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync size_t cbData;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync void *pvBuf;
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync /* Check that the S/G buffer has memory left. */
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync if (RT_UNLIKELY( pSgBuf->idxSeg == pSgBuf->cSegs
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync && !pSgBuf->cbSegLeft))
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync {
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync *pcbData = 0;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync return NULL;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync }
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
c320aee99660824809e157bbad368db3542a9e90vboxsync AssertReleaseMsg( pSgBuf->cbSegLeft <= 5 * _1M
c320aee99660824809e157bbad368db3542a9e90vboxsync && (uintptr_t)pSgBuf->pvSegCur >= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg
c320aee99660824809e157bbad368db3542a9e90vboxsync && (uintptr_t)pSgBuf->pvSegCur + pSgBuf->cbSegLeft <= (uintptr_t)pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg + pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg,
c320aee99660824809e157bbad368db3542a9e90vboxsync ("pSgBuf->idxSeg=%d pSgBuf->cSegs=%d pSgBuf->pvSegCur=%p pSgBuf->cbSegLeft=%zd pSgBuf->paSegs[%d].pvSeg=%p pSgBuf->paSegs[%d].cbSeg=%zd\n",
c320aee99660824809e157bbad368db3542a9e90vboxsync pSgBuf->idxSeg, pSgBuf->cSegs, pSgBuf->pvSegCur, pSgBuf->cbSegLeft,
c320aee99660824809e157bbad368db3542a9e90vboxsync pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg, pSgBuf->idxSeg, pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg));
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync
3a4a6501d0ccd629d9951b644d380c7bb2d46086vboxsync cbData = RT_MIN(*pcbData, pSgBuf->cbSegLeft);
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync pvBuf = pSgBuf->pvSegCur;
ea7d33962ceb90ea90e67e427d60b5788f34268avboxsync pSgBuf->cbSegLeft -= cbData;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync /* Advance to the next segment if required. */
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync if (!pSgBuf->cbSegLeft)
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync {
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync pSgBuf->idxSeg++;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync if (pSgBuf->idxSeg < pSgBuf->cSegs)
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync {
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync pSgBuf->pvSegCur = pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg;
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync pSgBuf->cbSegLeft = pSgBuf->paSegs[pSgBuf->idxSeg].cbSeg;
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync }
b6dc9545438e63d05fff621e85ddb7dc39cf14e7vboxsync
20593760b116c90f3e439552763eef632a3bbb17vboxsync *pcbData = cbData;
20593760b116c90f3e439552763eef632a3bbb17vboxsync }
20593760b116c90f3e439552763eef632a3bbb17vboxsync else
20593760b116c90f3e439552763eef632a3bbb17vboxsync pSgBuf->pvSegCur = (uint8_t *)pSgBuf->pvSegCur + cbData;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync return pvBuf;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync}
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
6fea4abcc6ee0f2797ac01ef79c374d506aedc02vboxsync
6fea4abcc6ee0f2797ac01ef79c374d506aedc02vboxsyncRTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs)
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync{
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync AssertPtr(pSgBuf);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync AssertPtr(paSegs);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync Assert(cSegs > 0);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync Assert(cSegs < (~(unsigned)0 >> 1));
5a4155f3cdda48bce86222313348e5fb35561f84vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->paSegs = paSegs;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->cSegs = (unsigned)cSegs;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->idxSeg = 0;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->pvSegCur = paSegs[0].pvSeg;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->cbSegLeft = paSegs[0].cbSeg;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync}
0b4890533cbae4f30a0241d0780d4ff9daa304f4vboxsync
0b4890533cbae4f30a0241d0780d4ff9daa304f4vboxsync
0b4890533cbae4f30a0241d0780d4ff9daa304f4vboxsyncRTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf)
0b4890533cbae4f30a0241d0780d4ff9daa304f4vboxsync{
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync AssertPtrReturnVoid(pSgBuf);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->idxSeg = 0;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBuf->pvSegCur = pSgBuf->paSegs[0].pvSeg;
20593760b116c90f3e439552763eef632a3bbb17vboxsync pSgBuf->cbSegLeft = pSgBuf->paSegs[0].cbSeg;
20593760b116c90f3e439552763eef632a3bbb17vboxsync}
20593760b116c90f3e439552763eef632a3bbb17vboxsync
20593760b116c90f3e439552763eef632a3bbb17vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsyncRTDECL(void) RTSgBufClone(PRTSGBUF pSgBufTo, PCRTSGBUF pSgBufFrom)
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync{
9cabb72c6d6feb65e839ce50765643b98bb9a301vboxsync AssertPtr(pSgBufTo);
9cabb72c6d6feb65e839ce50765643b98bb9a301vboxsync AssertPtr(pSgBufFrom);
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBufTo->paSegs = pSgBufFrom->paSegs;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBufTo->cSegs = pSgBufFrom->cSegs;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBufTo->idxSeg = pSgBufFrom->idxSeg;
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync pSgBufTo->pvSegCur = pSgBufFrom->pvSegCur;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync pSgBufTo->cbSegLeft = pSgBufFrom->cbSegLeft;
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync}
d1ae44110ca869b6cb4a708dc6d676e10f4167bavboxsync
2cf084f1df635fcf1e1e40547a96c33f723471cbvboxsync
2cf084f1df635fcf1e1e40547a96c33f723471cbvboxsyncRTDECL(void *) RTSgBufGetNextSegment(PRTSGBUF pSgBuf, size_t *pcbSeg)
2cf084f1df635fcf1e1e40547a96c33f723471cbvboxsync{
2cf084f1df635fcf1e1e40547a96c33f723471cbvboxsync AssertPtrReturn(pSgBuf, NULL);
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync AssertPtrReturn(pcbSeg, NULL);
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync if (!*pcbSeg)
65b0f0cd3c38007138720b7eaa43c963a9ee1244vboxsync *pcbSeg = pSgBuf->cbSegLeft;
1c2c968fd241148110002d75b2c0fdeddc211e14vboxsync
return sgBufGet(pSgBuf, pcbSeg);
}
RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy)
{
AssertPtrReturn(pSgBufDst, 0);
AssertPtrReturn(pSgBufSrc, 0);
size_t cbLeft = cbCopy;
while (cbLeft)
{
size_t cbThisCopy = RT_MIN(RT_MIN(pSgBufDst->cbSegLeft, cbLeft), pSgBufSrc->cbSegLeft);
size_t cbTmp = cbThisCopy;
void *pvBufDst;
void *pvBufSrc;
if (!cbThisCopy)
break;
pvBufDst = sgBufGet(pSgBufDst, &cbTmp);
Assert(cbTmp == cbThisCopy);
pvBufSrc = sgBufGet(pSgBufSrc, &cbTmp);
Assert(cbTmp == cbThisCopy);
memcpy(pvBufDst, pvBufSrc, cbThisCopy);
cbLeft -= cbThisCopy;
}
return cbCopy - cbLeft;
}
RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp)
{
AssertPtrReturn(pSgBuf1, 0);
AssertPtrReturn(pSgBuf2, 0);
size_t cbLeft = cbCmp;
RTSGBUF SgBuf1;
RTSGBUF SgBuf2;
/* Set up the temporary buffers */
RTSgBufClone(&SgBuf1, pSgBuf1);
RTSgBufClone(&SgBuf2, pSgBuf2);
while (cbLeft)
{
size_t cbThisCmp = RT_MIN(RT_MIN(SgBuf1.cbSegLeft, cbLeft), SgBuf2.cbSegLeft);
size_t cbTmp = cbThisCmp;
void *pvBuf1;
void *pvBuf2;
if (!cbCmp)
break;
pvBuf1 = sgBufGet(&SgBuf1, &cbTmp);
Assert(cbTmp == cbThisCmp);
pvBuf2 = sgBufGet(&SgBuf2, &cbTmp);
Assert(cbTmp == cbThisCmp);
int rc = memcmp(pvBuf1, pvBuf2, cbThisCmp);
if (rc)
return rc;
cbLeft -= cbThisCmp;
}
return 0;
}
RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
size_t *pcbOff, bool fAdvance)
{
AssertPtrReturn(pSgBuf1, 0);
AssertPtrReturn(pSgBuf2, 0);
size_t cbLeft = cbCmp;
size_t cbOff = 0;
RTSGBUF SgBuf1Tmp;
RTSGBUF SgBuf2Tmp;
PRTSGBUF pSgBuf1Tmp;
PRTSGBUF pSgBuf2Tmp;
if (!fAdvance)
{
/* Set up the temporary buffers */
RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
pSgBuf1Tmp = &SgBuf1Tmp;
pSgBuf2Tmp = &SgBuf2Tmp;
}
else
{
pSgBuf1Tmp = pSgBuf1;
pSgBuf2Tmp = pSgBuf2;
}
while (cbLeft)
{
size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
size_t cbTmp = cbThisCmp;
uint8_t *pbBuf1;
uint8_t *pbBuf2;
if (!cbCmp)
break;
pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
Assert(cbTmp == cbThisCmp);
pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
Assert(cbTmp == cbThisCmp);
int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
if (rc)
{
if (pcbOff)
{
/* Search for the correct offset */
while ( cbThisCmp-- > 0
&& (*pbBuf1 == *pbBuf2))
{
pbBuf1++;
pbBuf2++;
cbOff++;
}
*pcbOff = cbOff;
}
return rc;
}
cbLeft -= cbThisCmp;
cbOff += cbThisCmp;
}
return 0;
}
RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
{
AssertPtrReturn(pSgBuf, 0);
size_t cbLeft = cbSet;
while (cbLeft)
{
size_t cbThisSet = cbLeft;
void *pvBuf = sgBufGet(pSgBuf, &cbThisSet);
if (!cbThisSet)
break;
memset(pvBuf, ubFill, cbThisSet);
cbLeft -= cbThisSet;
}
return cbSet - cbLeft;
}
RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
{
AssertPtrReturn(pSgBuf, 0);
AssertPtrReturn(pvBuf, 0);
size_t cbLeft = cbCopy;
while (cbLeft)
{
size_t cbThisCopy = cbLeft;
void *pvSrc = sgBufGet(pSgBuf, &cbThisCopy);
if (!cbThisCopy)
break;
memcpy(pvBuf, pvSrc, cbThisCopy);
cbLeft -= cbThisCopy;
pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
}
return cbCopy - cbLeft;
}
RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy)
{
AssertPtrReturn(pSgBuf, 0);
AssertPtrReturn(pvBuf, 0);
size_t cbLeft = cbCopy;
while (cbLeft)
{
size_t cbThisCopy = cbLeft;
void *pvDst = sgBufGet(pSgBuf, &cbThisCopy);
if (!cbThisCopy)
break;
memcpy(pvDst, pvBuf, cbThisCopy);
cbLeft -= cbThisCopy;
pvBuf = (void *)((uintptr_t)pvBuf + cbThisCopy);
}
return cbCopy - cbLeft;
}
RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance)
{
AssertPtrReturn(pSgBuf, 0);
size_t cbLeft = cbAdvance;
while (cbLeft)
{
size_t cbThisAdvance = cbLeft;
void *pv = sgBufGet(pSgBuf, &cbThisAdvance);
NOREF(pv);
if (!cbThisAdvance)
break;
cbLeft -= cbThisAdvance;
}
return cbAdvance - cbLeft;
}
RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData)
{
AssertPtrReturn(pSgBuf, 0);
AssertPtrReturn(pcSeg, 0);
unsigned cSeg = 0;
size_t cb = 0;
if (!paSeg)
{
if (pSgBuf->cbSegLeft > 0)
{
size_t idx = pSgBuf->idxSeg;
cSeg = 1;
cb += RT_MIN(pSgBuf->cbSegLeft, cbData);
cbData -= RT_MIN(pSgBuf->cbSegLeft, cbData);
while ( cbData
&& idx < pSgBuf->cSegs - 1)
{
idx++;
cSeg++;
cb += RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
cbData -= RT_MIN(pSgBuf->paSegs[idx].cbSeg, cbData);
}
}
}
else
{
while ( cbData
&& cSeg < *pcSeg)
{
size_t cbThisSeg = cbData;
void *pvSeg = NULL;
pvSeg = sgBufGet(pSgBuf, &cbThisSeg);
if (!cbThisSeg)
{
Assert(!pvSeg);
break;
}
AssertMsg(cbThisSeg <= cbData, ("Impossible!\n"));
paSeg[cSeg].cbSeg = cbThisSeg;
paSeg[cSeg].pvSeg = pvSeg;
cSeg++;
cbData -= cbThisSeg;
cb += cbThisSeg;
}
}
*pcSeg = cSeg;
return cb;
}