VirtualBox

source: vbox/trunk/include/iprt/sg.h@ 99841

Last change on this file since 99841 was 99739, checked in by vboxsync, 21 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1/** @file
2 * IPRT - S/G buffer handling.
3 */
4
5/*
6 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_sg_h
37#define IPRT_INCLUDED_sg_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/types.h>
43
44RT_C_DECLS_BEGIN
45
46/** @defgroup grp_rt_sgbuf RTSgBuf - Scatter / Gather Buffers
47 * @ingroup grp_rt
48 * @{
49 */
50
51/** Pointer to a const S/G entry. */
52typedef const struct RTSGBUF *PCRTSGBUF;
53
54/**
55 * Callback for RTSgBufCopyToFn() called on every segment of the given S/G buffer.
56 *
57 * @returns Number of bytes copied for this segment, a value smaller than cbSrc will stop the copy operation.
58 * @param pSgBuf The S/G buffer for reference.
59 * @param pvSrc Where to copy from.
60 * @param cbSrc The number of bytes in the source buffer.
61 * @param pvUser Opaque user data passed in RTSgBufCopyToFn().
62 */
63typedef DECLCALLBACKTYPE(size_t, FNRTSGBUFCOPYTO, (PCRTSGBUF pSgBuf, const void *pvSrc, size_t cbSrc, void *pvUser));
64/** Pointer to a FNRTSGBUFCOPYTO. */
65typedef FNRTSGBUFCOPYTO *PFNRTSGBUFCOPYTO;
66
67/**
68 * Callback for RTSgBufCopyFromFn() called on every segment of the given S/G buffer.
69 *
70 * @returns Number of bytes copied for this segment, a value smaller than cbDst will stop the copy operation.
71 * @param pSgBuf The S/G buffer for reference.
72 * @param pvDst Where to copy to.
73 * @param cbDst The number of bytes in the destination buffer.
74 * @param pvUser Opaque user data passed in RTSgBufCopyFromFn().
75 */
76typedef DECLCALLBACKTYPE(size_t, FNRTSGBUFCOPYFROM, (PCRTSGBUF pSgBuf, void *pvDst, size_t cbDst, void *pvUser));
77/** Pointer to a FNRTSGBUFCOPYFROM. */
78typedef FNRTSGBUFCOPYFROM *PFNRTSGBUFCOPYFROM;
79
80/**
81 * A S/G entry.
82 */
83typedef struct RTSGSEG
84{
85 /** Pointer to the segment buffer. */
86 void *pvSeg;
87 /** Size of the segment buffer. */
88 size_t cbSeg;
89} RTSGSEG;
90/** Pointer to a S/G entry. */
91typedef RTSGSEG *PRTSGSEG;
92/** Pointer to a const S/G entry. */
93typedef const RTSGSEG *PCRTSGSEG;
94/** Pointer to a S/G entry pointer. */
95typedef PRTSGSEG *PPRTSGSEG;
96
97/**
98 * A S/G buffer.
99 *
100 * The members should be treated as private.
101 *
102 * @warning There is a lot of code, especially in the VFS area of IPRT, that
103 * totally ignores the idxSeg, pvSegCur and cbSegLeft members! So,
104 * it is not recommended to pass buffers that aren't fully reset or
105 * where cbSegLeft is shorter than what paSegs describes.
106 */
107typedef struct RTSGBUF
108{
109 /** Pointer to the scatter/gather array. */
110 PCRTSGSEG paSegs;
111 /** Number of segments. */
112 unsigned cSegs;
113
114 /** Current segment we are in. */
115 unsigned idxSeg;
116 /** Pointer to current byte within the current segment. */
117 void *pvSegCur;
118 /** Number of bytes left in the current segment. */
119 size_t cbSegLeft;
120} RTSGBUF;
121/** Pointer to a S/G entry. */
122typedef RTSGBUF *PRTSGBUF;
123/** Pointer to a S/G entry pointer. */
124typedef PRTSGBUF *PPRTSGBUF;
125
126
127/**
128 * Sums up the length of all the segments.
129 *
130 * @returns The complete segment length.
131 * @param pSgBuf The S/G buffer to check out.
132 */
133DECLINLINE(size_t) RTSgBufCalcTotalLength(PCRTSGBUF pSgBuf)
134{
135 size_t cb = 0;
136 unsigned i = pSgBuf->cSegs;
137 while (i-- > 0)
138 cb += pSgBuf->paSegs[i].cbSeg;
139 return cb;
140}
141
142/**
143 * Sums up the number of bytes left from the current position.
144 *
145 * @returns Number of bytes left.
146 * @param pSgBuf The S/G buffer to check out.
147 */
148DECLINLINE(size_t) RTSgBufCalcLengthLeft(PCRTSGBUF pSgBuf)
149{
150 size_t cb = pSgBuf->cbSegLeft;
151 unsigned i = pSgBuf->cSegs;
152 while (i-- > pSgBuf->idxSeg + 1)
153 cb += pSgBuf->paSegs[i].cbSeg;
154 return cb;
155}
156
157/**
158 * Checks if the current buffer position is at the start of the first segment.
159 *
160 * @returns true / false.
161 * @param pSgBuf The S/G buffer to check out.
162 */
163DECLINLINE(bool) RTSgBufIsAtStart(PCRTSGBUF pSgBuf)
164{
165 return pSgBuf->idxSeg == 0
166 && ( pSgBuf->cSegs == 0
167 || pSgBuf->pvSegCur == pSgBuf->paSegs[0].pvSeg);
168}
169
170/**
171 * Checks if the current buffer position is at the end of all the segments.
172 *
173 * @returns true / false.
174 * @param pSgBuf The S/G buffer to check out.
175 */
176DECLINLINE(bool) RTSgBufIsAtEnd(PCRTSGBUF pSgBuf)
177{
178 return pSgBuf->idxSeg > pSgBuf->cSegs
179 || ( pSgBuf->idxSeg == pSgBuf->cSegs
180 && pSgBuf->cbSegLeft == 0);
181}
182
183/**
184 * Checks if the current buffer position is at the start of the current segment.
185 *
186 * @returns true / false.
187 * @param pSgBuf The S/G buffer to check out.
188 */
189DECLINLINE(bool) RTSgBufIsAtStartOfSegment(PCRTSGBUF pSgBuf)
190{
191 return pSgBuf->idxSeg < pSgBuf->cSegs
192 && pSgBuf->paSegs[pSgBuf->idxSeg].pvSeg == pSgBuf->pvSegCur;
193}
194
195/**
196 * Initialize a S/G buffer structure.
197 *
198 * @param pSgBuf Pointer to the S/G buffer to initialize.
199 * @param paSegs Pointer to the start of the segment array.
200 * @param cSegs Number of segments in the array.
201 *
202 * @note paSegs and cSegs can be NULL and 0 respectively to indicate an empty
203 * S/G buffer. Operations on the S/G buffer will not do anything in this
204 * case.
205 */
206RTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG paSegs, size_t cSegs);
207
208/**
209 * Resets the internal buffer position of the S/G buffer to the beginning.
210 *
211 * @param pSgBuf The S/G buffer to reset.
212 */
213RTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf);
214
215/**
216 * Clones a given S/G buffer.
217 *
218 * @param pSgBufNew The new S/G buffer to clone to.
219 * @param pSgBufOld The source S/G buffer to clone from.
220 *
221 * @note This is only a shallow copy. Both S/G buffers will point to the
222 * same segment array.
223 */
224RTDECL(void) RTSgBufClone(PRTSGBUF pSgBufNew, PCRTSGBUF pSgBufOld);
225
226/**
227 * Returns the next segment in the S/G buffer or NULL if no segments left.
228 *
229 * @returns Pointer to the next segment in the S/G buffer.
230 * @param pSgBuf The S/G buffer.
231 * @param cbDesired The max number of bytes to get.
232 * @param pcbSeg Where to store the size of the returned segment, this is
233 * equal or smaller than @a cbDesired.
234 *
235 * @note Use RTSgBufAdvance() to advance after read/writing into the buffer.
236 */
237DECLINLINE(void *) RTSgBufGetCurrentSegment(PRTSGBUF pSgBuf, size_t cbDesired, size_t *pcbSeg)
238{
239 if (!RTSgBufIsAtEnd(pSgBuf))
240 {
241 *pcbSeg = RT_MIN(cbDesired, pSgBuf->cbSegLeft);
242 return pSgBuf->pvSegCur;
243 }
244 *pcbSeg = 0;
245 return NULL;
246}
247
248/**
249 * Returns the next segment in the S/G buffer or NULL if no segment is left.
250 *
251 * @returns Pointer to the next segment in the S/G buffer.
252 * @param pSgBuf The S/G buffer.
253 * @param pcbSeg Where to store the size of the returned segment.
254 * Holds the number of bytes requested initially or 0 to
255 * indicate that the size doesn't matter.
256 * This may contain fewer bytes on success if the current segment
257 * is smaller than the amount of bytes requested.
258 *
259 * @note This operation advances the internal buffer pointer of both S/G buffers.
260 */
261RTDECL(void *) RTSgBufGetNextSegment(PRTSGBUF pSgBuf, size_t *pcbSeg);
262
263/**
264 * Copy data between two S/G buffers.
265 *
266 * @returns The number of bytes copied.
267 * @param pSgBufDst The destination S/G buffer.
268 * @param pSgBufSrc The source S/G buffer.
269 * @param cbCopy Number of bytes to copy.
270 *
271 * @note This operation advances the internal buffer pointer of both S/G buffers.
272 */
273RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy);
274
275/**
276 * Compares the content of two S/G buffers.
277 *
278 * @returns Whatever memcmp returns.
279 * @param pSgBuf1 First S/G buffer.
280 * @param pSgBuf2 Second S/G buffer.
281 * @param cbCmp How many bytes to compare.
282 *
283 * @note This operation doesn't change the internal position of the S/G buffers.
284 */
285RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp);
286
287/**
288 * Compares the content of two S/G buffers - advanced version.
289 *
290 * @returns Whatever memcmp returns.
291 * @param pSgBuf1 First S/G buffer.
292 * @param pSgBuf2 Second S/G buffer.
293 * @param cbCmp How many bytes to compare.
294 * @param poffDiff Where to store the offset of the first different byte
295 * in the buffer starting from the position of the S/G
296 * buffer before this call.
297 * @param fAdvance Flag whether the internal buffer position should be advanced.
298 *
299 */
300RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp, size_t *poffDiff, bool fAdvance);
301
302/**
303 * Fills an S/G buf with a constant byte.
304 *
305 * @returns The number of actually filled bytes.
306 * Can be less than than cbSet if the end of the S/G buffer was reached.
307 * @param pSgBuf The S/G buffer.
308 * @param ubFill The byte to fill the buffer with.
309 * @param cbSet How many bytes to set.
310 *
311 * @note This operation advances the internal buffer pointer of the S/G buffer.
312 */
313RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet);
314
315/**
316 * Copies data from an S/G buffer into a given non scattered buffer.
317 *
318 * @returns Number of bytes copied.
319 * @param pSgBuf The S/G buffer to copy from.
320 * @param pvBuf Buffer to copy the data into.
321 * @param cbCopy How many bytes to copy.
322 *
323 * @note This operation advances the internal buffer pointer of the S/G buffer.
324 */
325RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy);
326
327/**
328 * Copies data from a non scattered buffer into an S/G buffer.
329 *
330 * @returns Number of bytes copied.
331 * @param pSgBuf The S/G buffer to copy to.
332 * @param pvBuf Buffer to copy the data from.
333 * @param cbCopy How many bytes to copy.
334 *
335 * @note This operation advances the internal buffer pointer of the S/G buffer.
336 */
337RTDECL(size_t) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, const void *pvBuf, size_t cbCopy);
338
339/**
340 * Copies data from the given S/G buffer to a destination handled by the given callback.
341 *
342 * @returns Number of bytes copied.
343 * @param pSgBuf The S/G buffer to copy from.
344 * @param cbCopy How many bytes to copy.
345 * @param pfnCopyTo The callback to call on every S/G buffer segment until the operation finished.
346 * @param pvUser Opaque user data to pass in the given callback.
347 *
348 * @note This operation advances the internal buffer pointer of the S/G buffer.
349 */
350RTDECL(size_t) RTSgBufCopyToFn(PRTSGBUF pSgBuf, size_t cbCopy, PFNRTSGBUFCOPYTO pfnCopyTo, void *pvUser);
351
352/**
353 * Copies data to the given S/G buffer from a destination handled by the given callback.
354 *
355 * @returns Number of bytes copied.
356 * @param pSgBuf The S/G buffer to copy to.
357 * @param cbCopy How many bytes to copy.
358 * @param pfnCopyFrom The callback to call on every S/G buffer segment until the operation finished.
359 * @param pvUser Opaque user data to pass in the given callback.
360 *
361 * @note This operation advances the internal buffer pointer of the S/G buffer.
362 */
363RTDECL(size_t) RTSgBufCopyFromFn(PRTSGBUF pSgBuf, size_t cbCopy, PFNRTSGBUFCOPYFROM pfnCopyFrom, void *pvUser);
364
365/**
366 * Advances the internal buffer pointer.
367 *
368 * @returns Number of bytes the pointer was moved forward.
369 * @param pSgBuf The S/G buffer.
370 * @param cbAdvance Number of bytes to move forward.
371 */
372RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance);
373
374/**
375 * Constructs a new segment array starting from the current position
376 * and describing the given number of bytes.
377 *
378 * @returns Number of bytes the array describes.
379 * @param pSgBuf The S/G buffer.
380 * @param paSeg The uninitialized segment array.
381 * If NULL pcSeg will contain the number of segments needed
382 * to describe the requested amount of data.
383 * @param pcSeg The number of segments the given array has.
384 * This will hold the actual number of entries needed upon return.
385 * @param cbData Number of bytes the new array should describe.
386 *
387 * @note This operation advances the internal buffer pointer of the S/G buffer if paSeg is not NULL.
388 */
389RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData);
390
391/**
392 * Returns whether the given S/G buffer is zeroed out from the current position
393 * upto the number of bytes to check.
394 *
395 * @returns true if the buffer has only zeros
396 * false otherwise.
397 * @param pSgBuf The S/G buffer.
398 * @param cbCheck Number of bytes to check.
399 */
400RTDECL(bool) RTSgBufIsZero(PRTSGBUF pSgBuf, size_t cbCheck);
401
402/**
403 * Maps the given S/G buffer to a segment array of another type (for example to
404 * iovec on POSIX or WSABUF on Windows).
405 *
406 * @param paMapped Where to store the pointer to the start of the native
407 * array or NULL. The memory needs to be freed with
408 * RTMemTmpFree().
409 * @param pSgBuf The S/G buffer to map.
410 * @param Struct Struct used as the destination.
411 * @param pvBufField Name of the field holding the pointer to a buffer.
412 * @param TypeBufPtr Type of the buffer pointer.
413 * @param cbBufField Name of the field holding the size of the buffer.
414 * @param TypeBufSize Type of the field for the buffer size.
415 * @param cSegsMapped Where to store the number of segments the native array
416 * has.
417 *
418 * @note This operation maps the whole S/G buffer starting at the current
419 * internal position. The internal buffer position is unchanged by
420 * this operation.
421 *
422 * @remark Usage is a bit ugly but saves a few lines of duplicated code
423 * somewhere else and makes it possible to keep the S/G buffer members
424 * private without going through RTSgBufSegArrayCreate() first.
425 */
426#define RTSgBufMapToNative(paMapped, pSgBuf, Struct, pvBufField, TypeBufPtr, cbBufField, TypeBufSize, cSegsMapped) \
427 do \
428 { \
429 AssertCompileMemberSize(Struct, pvBufField, RT_SIZEOFMEMB(RTSGSEG, pvSeg)); \
430 /*AssertCompile(RT_SIZEOFMEMB(Struct, cbBufField) >= RT_SIZEOFMEMB(RTSGSEG, cbSeg));*/ \
431 (cSegsMapped) = (pSgBuf)->cSegs - (pSgBuf)->idxSeg; \
432 \
433 /* We need room for at least one segment. */ \
434 if ((pSgBuf)->cSegs == (pSgBuf)->idxSeg) \
435 (cSegsMapped)++; \
436 \
437 (paMapped) = (Struct *)RTMemTmpAllocZ((cSegsMapped) * sizeof(Struct)); \
438 if ((paMapped)) \
439 { \
440 /* The first buffer is special because we could be in the middle of a segment. */ \
441 (paMapped)[0].pvBufField = (TypeBufPtr)(pSgBuf)->pvSegCur; \
442 (paMapped)[0].cbBufField = (TypeBufSize)(pSgBuf)->cbSegLeft; \
443 \
444 for (unsigned i = 1; i < (cSegsMapped); i++) \
445 { \
446 (paMapped)[i].pvBufField = (TypeBufPtr)(pSgBuf)->paSegs[(pSgBuf)->idxSeg + i].pvSeg; \
447 (paMapped)[i].cbBufField = (TypeBufSize)(pSgBuf)->paSegs[(pSgBuf)->idxSeg + i].cbSeg; \
448 } \
449 } \
450 } while (0)
451
452RT_C_DECLS_END
453
454/** @} */
455
456#endif /* !IPRT_INCLUDED_sg_h */
457
Note: See TracBrowser for help on using the repository browser.

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