1 | /** @file
|
---|
2 | * IPRT - S/G buffer handling.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_sg_h
|
---|
27 | #define ___iprt_sg_h
|
---|
28 |
|
---|
29 | #include <iprt/types.h>
|
---|
30 |
|
---|
31 | RT_C_DECLS_BEGIN
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * A S/G entry.
|
---|
35 | */
|
---|
36 | typedef struct RTSGSEG
|
---|
37 | {
|
---|
38 | /** Pointer to the segment buffer. */
|
---|
39 | void *pvSeg;
|
---|
40 | /** Size of the segment buffer. */
|
---|
41 | size_t cbSeg;
|
---|
42 | } RTSGSEG;
|
---|
43 | /** Pointer to a S/G entry. */
|
---|
44 | typedef RTSGSEG *PRTSGSEG;
|
---|
45 | /** Pointer to a const S/G entry. */
|
---|
46 | typedef const RTSGSEG *PCRTSGSEG;
|
---|
47 | /** Pointer to a S/G entry pointer. */
|
---|
48 | typedef PRTSGSEG *PPRTSGSEG;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * A S/G buffer.
|
---|
52 | *
|
---|
53 | * The members should be treated as private.
|
---|
54 | */
|
---|
55 | typedef struct RTSGBUF
|
---|
56 | {
|
---|
57 | /** Pointer to the scatter/gather array. */
|
---|
58 | PCRTSGSEG paSegs;
|
---|
59 | /** Number of segments. */
|
---|
60 | unsigned cSegs;
|
---|
61 | /** Current segment we are in. */
|
---|
62 | unsigned idxSeg;
|
---|
63 | /** Pointer to the current segment start. */
|
---|
64 | void *pvSegCur;
|
---|
65 | /** Number of bytes left in the current buffer. */
|
---|
66 | size_t cbSegLeft;
|
---|
67 | } RTSGBUF;
|
---|
68 | /** Pointer to a S/G entry. */
|
---|
69 | typedef RTSGBUF *PRTSGBUF;
|
---|
70 | /** Pointer to a const S/G entry. */
|
---|
71 | typedef const RTSGBUF *PCRTSGBUF;
|
---|
72 | /** Pointer to a S/G entry pointer. */
|
---|
73 | typedef PRTSGBUF *PPRTSGBUF;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Initialize a S/G buffer structure.
|
---|
77 | *
|
---|
78 | * @returns nothing.
|
---|
79 | * @param pSgBuf Pointer to the S/G buffer to initialize.
|
---|
80 | * @param paSegs Pointer to the start of the segment array.
|
---|
81 | * @param cSegs Number of segments in the array.
|
---|
82 | */
|
---|
83 | RTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Resets the internal buffer position of the S/G buffer to the beginning.
|
---|
87 | *
|
---|
88 | * @returns nothing.
|
---|
89 | * @param pSgBuf The S/G buffer to reset.
|
---|
90 | */
|
---|
91 | RTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Clones a given S/G buffer.
|
---|
95 | *
|
---|
96 | * @returns nothing.
|
---|
97 | * @param pSgBufNew The new S/G buffer to clone to.
|
---|
98 | * @param pSgBufOld The source S/G buffer to clone from.
|
---|
99 | *
|
---|
100 | * @note This is only a shallow copy. Both S/G buffers will point to the
|
---|
101 | * same segment array.
|
---|
102 | */
|
---|
103 | RTDECL(void) RTSgBufClone(PRTSGBUF pSgBufNew, PCRTSGBUF pSgBufOld);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Copy data between two S/G buffers.
|
---|
107 | *
|
---|
108 | * @returns The number of bytes copied.
|
---|
109 | * @param pSgBufDst The destination S/G buffer.
|
---|
110 | * @param pSgBufSrc The source S/G buffer.
|
---|
111 | * @param cbCopy Number of bytes to copy.
|
---|
112 | *
|
---|
113 | * @note This operation advances the internal buffer pointer of both S/G buffers.
|
---|
114 | */
|
---|
115 | RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy);
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Compares the content of two S/G buffers.
|
---|
119 | *
|
---|
120 | * @returns Whatever memcmp returns.
|
---|
121 | * @param pSgBuf1 First S/G buffer.
|
---|
122 | * @param pSgBuf2 Second S/G buffer.
|
---|
123 | * @param cbCmp How many bytes to compare.
|
---|
124 | *
|
---|
125 | * @note This operation doesn't change the internal position of the S/G buffers.
|
---|
126 | */
|
---|
127 | RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp);
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Compares the content of two S/G buffers - advanced version.
|
---|
131 | *
|
---|
132 | * @returns Whatever memcmp returns.
|
---|
133 | * @param pSgBuf1 First S/G buffer.
|
---|
134 | * @param pSgBuf2 Second S/G buffer.
|
---|
135 | * @param cbCmp How many bytes to compare.
|
---|
136 | * @param pcbOff Where to store the offset of the first different byte
|
---|
137 | * in the buffer starting from the position of the S/G
|
---|
138 | * buffer before this call.
|
---|
139 | * @param fAdvance Flag whether the internal buffer position should be advanced.
|
---|
140 | *
|
---|
141 | */
|
---|
142 | RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
|
---|
143 | size_t *pcbOff, bool fAdvance);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Fills an S/G buf with a constant byte.
|
---|
147 | *
|
---|
148 | * @returns The number of actually filled bytes.
|
---|
149 | * Can be less than than cbSet if the end of the S/G buffer was reached.
|
---|
150 | * @param pSgBuf The S/G buffer.
|
---|
151 | * @param ubFill The byte to fill the buffer with.
|
---|
152 | * @param cbSet How many bytes to set.
|
---|
153 | *
|
---|
154 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
155 | */
|
---|
156 | RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet);
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Copies data from an S/G buffer into a given non scattered buffer.
|
---|
160 | *
|
---|
161 | * @returns Number of bytes copied.
|
---|
162 | * @param pSgBuf The S/G buffer to copy from.
|
---|
163 | * @param pvBuf Buffer to copy the data into.
|
---|
164 | * @param cbCopy How many bytes to copy.
|
---|
165 | *
|
---|
166 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
167 | */
|
---|
168 | RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Copies data from a non scattered buffer into an S/G buffer.
|
---|
172 | *
|
---|
173 | * @returns Number of bytes copied.
|
---|
174 | * @param pSgBuf The S/G buffer to copy to.
|
---|
175 | * @param pvBuf Buffer to copy the data into.
|
---|
176 | * @param cbCopy How many bytes to copy.
|
---|
177 | *
|
---|
178 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
179 | */
|
---|
180 | RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Advances the internal buffer pointer.
|
---|
184 | *
|
---|
185 | * @returns Number of bytes the pointer was moved forward.
|
---|
186 | * @param pSgBuf The S/G buffer.
|
---|
187 | * @param cbAdvance Number of bytes to move forward.
|
---|
188 | */
|
---|
189 | RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance);
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Constructs a new segment array starting from the current position
|
---|
193 | * and describing the given number of bytes.
|
---|
194 | *
|
---|
195 | * @returns Number of bytes the array describes.
|
---|
196 | * @param pSgBuf The S/G buffer.
|
---|
197 | * @param paSeg The unitialized segment array.
|
---|
198 | * @param pcSeg The number of segments the given array has.
|
---|
199 | * This will hold the actual number of entries needed upon return.
|
---|
200 | * @param cbData Number of bytes the new array should describe.
|
---|
201 | *
|
---|
202 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
203 | */
|
---|
204 | RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData);
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Maps the given S/G buffer to a segment array of another type
|
---|
208 | * (for example to iovec on Posix or WSABUF on Windows).
|
---|
209 | *
|
---|
210 | * @param paMapped Where to store the pointer to the start of the native array or NULL.
|
---|
211 | * The memory needs to be freed with RTMemTmpFree().
|
---|
212 | * @param pSgBuf The S/G buffer to map.
|
---|
213 | * @param Struct Struct used as the destination.
|
---|
214 | * @param pvBuf Name of the field holding the pointer to a buffer.
|
---|
215 | * @param TypeBufPtr Type of the buffer pointer.
|
---|
216 | * @param cbBuf Name of the field holding the size of the buffer.
|
---|
217 | * @param TypeBufSize Type of the field for the buffer size.
|
---|
218 | * @param cSegsMapped Where to store the number of segments the native array has.
|
---|
219 | *
|
---|
220 | * @note This operation maps the whole S/G buffer starting at the current internal position.
|
---|
221 | * The internal buffer position is unchanged by this operation.
|
---|
222 | *
|
---|
223 | * @remark Usage is a bit ugly but saves a few lines of duplicated code somewhere else and makes it possible to
|
---|
224 | * keep the S/G buffer members private without going through RTSgBufSegArrayCreate() first.
|
---|
225 | */
|
---|
226 | #define RTSgBufMapToNative(paMapped, pSgBuf, Struct, pvBuf, TypeBufPtr, cbBuf, TypeBufSize, cSegsMapped) \
|
---|
227 | do \
|
---|
228 | { \
|
---|
229 | AssertCompileMemberSize(Struct, pvBuf, RT_SIZEOFMEMB(RTSGSEG, pvSeg)); \
|
---|
230 | /*AssertCompile(RT_SIZEOFMEMB(Struct, cbBuf) >= RT_SIZEOFMEMB(RTSGSEG, cbSeg));*/ \
|
---|
231 | cSegsMapped = pSgBuf->cSegs - pSgBuf->idxSeg; \
|
---|
232 | \
|
---|
233 | paMapped = NULL; \
|
---|
234 | \
|
---|
235 | /* We need room for at least one segment. */ \
|
---|
236 | if (pSgBuf->cSegs == pSgBuf->idxSeg) \
|
---|
237 | cSegsMapped++; \
|
---|
238 | \
|
---|
239 | paMapped = (Struct *)RTMemTmpAllocZ(cSegsMapped * sizeof(Struct)); \
|
---|
240 | if (paMapped) \
|
---|
241 | { \
|
---|
242 | /* The first buffer is special because we could be in the middle of a segment. */ \
|
---|
243 | paMapped[0].pvBuf = (TypeBufPtr)pSgBuf->pvSegCur; \
|
---|
244 | paMapped[0].cbBuf = (TypeBufSize)pSgBuf->cbSegLeft; \
|
---|
245 | \
|
---|
246 | for (unsigned i = 1; i < cSegsMapped; i++) \
|
---|
247 | { \
|
---|
248 | paMsg[i].pvBuf = (TypeBufPtr)pSgBuf->paSegs[pSgBuf->idxSeg + i].pvSeg; \
|
---|
249 | paMsg[i].cbBuf = (TypeBufSize)pSgBuf->paSegs[pSgBuf->idxSeg + i].cbSeg; \
|
---|
250 | } \
|
---|
251 | } \
|
---|
252 | } \
|
---|
253 | while (0)
|
---|
254 |
|
---|
255 | RT_C_DECLS_END
|
---|
256 |
|
---|
257 | /** @} */
|
---|
258 |
|
---|
259 | #endif
|
---|
260 |
|
---|