VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VSCSI/VSCSISgBuf.cpp@ 27976

Last change on this file since 27976 was 27976, checked in by vboxsync, 15 years ago

*: scm cleans up whitespace and adds a new line at the end of ApplianceimplPrivate.h.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/* $Id: VSCSISgBuf.cpp 27976 2010-04-04 14:16:32Z vboxsync $ */
2/** @file
3 * Virtual SCSI driver: S/G list handling
4 */
5
6/*
7 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21#define LOG_GROUP LOG_GROUP_VSCSI
22#include <VBox/log.h>
23#include <iprt/assert.h>
24#include <iprt/string.h>
25
26#include "VSCSIInternal.h"
27
28
29void vscsiIoMemCtxInit(PVSCSIIOMEMCTX pIoMemCtx, PCPDMDATASEG paDataSeg, size_t cSegments)
30{
31 if (RT_UNLIKELY(!cSegments))
32 {
33 pIoMemCtx->paDataSeg = NULL;
34 pIoMemCtx->cSegments = 0;
35 pIoMemCtx->iSegIdx = 0;
36 pIoMemCtx->pbBuf = NULL;
37 pIoMemCtx->cbBufLeft = 0;
38 }
39 else
40 {
41 pIoMemCtx->paDataSeg = paDataSeg;
42 pIoMemCtx->cSegments = cSegments;
43 pIoMemCtx->iSegIdx = 0;
44 pIoMemCtx->pbBuf = (uint8_t *)paDataSeg[0].pvSeg;
45 pIoMemCtx->cbBufLeft = paDataSeg[0].cbSeg;
46 }
47}
48
49
50uint8_t *vscsiIoMemCtxGetBuffer(PVSCSIIOMEMCTX pIoMemCtx, size_t *pcbData)
51{
52 size_t cbData = RT_MIN(*pcbData, pIoMemCtx->cbBufLeft);
53 uint8_t *pbBuf = pIoMemCtx->pbBuf;
54
55 pIoMemCtx->cbBufLeft -= cbData;
56
57 /* Advance to the next segment if required. */
58 if (!pIoMemCtx->cbBufLeft)
59 {
60 pIoMemCtx->iSegIdx++;
61
62 if (RT_UNLIKELY(pIoMemCtx->iSegIdx == pIoMemCtx->cSegments))
63 {
64 pIoMemCtx->cbBufLeft = 0;
65 pIoMemCtx->pbBuf = NULL;
66 }
67 else
68 {
69 pIoMemCtx->pbBuf = (uint8_t *)pIoMemCtx->paDataSeg[pIoMemCtx->iSegIdx].pvSeg;
70 pIoMemCtx->cbBufLeft = pIoMemCtx->paDataSeg[pIoMemCtx->iSegIdx].cbSeg;
71 }
72
73 *pcbData = cbData;
74 }
75 else
76 pIoMemCtx->pbBuf += cbData;
77
78 return pbBuf;
79}
80
81
82size_t vscsiCopyToIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData)
83{
84 size_t cbLeft = cbData;
85
86 while (cbLeft)
87 {
88 size_t cbCopy = cbLeft;
89 uint8_t *pbBuf = vscsiIoMemCtxGetBuffer(pIoMemCtx, &cbCopy);
90
91 if (!cbCopy)
92 break;
93
94 memcpy(pbBuf, pbData, cbCopy);
95
96 cbLeft -= cbCopy;
97 pbData += cbCopy;
98 }
99
100 return cbData - cbLeft;
101}
102
103
104size_t vscsiCopyFromIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData)
105{
106 size_t cbLeft = cbData;
107
108 while (cbLeft)
109 {
110 size_t cbCopy = cbData;
111 uint8_t *pbBuf = vscsiIoMemCtxGetBuffer(pIoMemCtx, &cbCopy);
112
113 if (!cbCopy)
114 break;
115
116 memcpy(pbData, pbBuf, cbCopy);
117
118 cbData -= cbCopy;
119 pbData += cbCopy;
120 }
121
122 return cbData - cbLeft;
123}
124
Note: See TracBrowser for help on using the repository browser.

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