1 | /** @file
|
---|
2 | * IPRT - S/G buffer handling.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_sg_h
|
---|
31 | #define ___iprt_sg_h
|
---|
32 |
|
---|
33 | #include <iprt/types.h>
|
---|
34 |
|
---|
35 | RT_C_DECLS_BEGIN
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * A S/G entry.
|
---|
39 | */
|
---|
40 | typedef struct RTSGSEG
|
---|
41 | {
|
---|
42 | /** Pointer to the segment buffer. */
|
---|
43 | void *pvSeg;
|
---|
44 | /** Size of the segment buffer. */
|
---|
45 | size_t cbSeg;
|
---|
46 | } RTSGSEG;
|
---|
47 | /** Pointer to a S/G entry. */
|
---|
48 | typedef RTSGSEG *PRTSGSEG;
|
---|
49 | /** Pointer to a const S/G entry. */
|
---|
50 | typedef const RTSGSEG *PCRTSGSEG;
|
---|
51 | /** Pointer to a S/G entry pointer. */
|
---|
52 | typedef PRTSGSEG *PPRTSGSEG;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * A S/G buffer.
|
---|
56 | *
|
---|
57 | * The members should be treated as private.
|
---|
58 | */
|
---|
59 | typedef struct RTSGBUF
|
---|
60 | {
|
---|
61 | /** Pointer to the scatter/gather array. */
|
---|
62 | PCRTSGSEG pcaSeg;
|
---|
63 | /** Number of segments. */
|
---|
64 | unsigned cSeg;
|
---|
65 | /** Current segment we are in. */
|
---|
66 | unsigned idxSeg;
|
---|
67 | /** Pointer to the current segment start. */
|
---|
68 | void *pvSegCurr;
|
---|
69 | /** Number of bytes left in the current buffer. */
|
---|
70 | size_t cbSegLeft;
|
---|
71 | } RTSGBUF;
|
---|
72 | /** Pointer to a S/G entry. */
|
---|
73 | typedef RTSGBUF *PRTSGBUF;
|
---|
74 | /** Pointer to a const S/G entry. */
|
---|
75 | typedef const RTSGBUF *PCRTSGBUF;
|
---|
76 | /** Pointer to a S/G entry pointer. */
|
---|
77 | typedef PRTSGBUF *PPRTSGBUF;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Initialize a S/G buffer structure.
|
---|
81 | *
|
---|
82 | * @returns nothing.
|
---|
83 | * @param pSgBuf Pointer to the S/G buffer to initialize.
|
---|
84 | * @param pcaSeg Pointer to the start of the segment array.
|
---|
85 | * @param cSeg Number of segments in the array.
|
---|
86 | */
|
---|
87 | RTDECL(void) RTSgBufInit(PRTSGBUF pSgBuf, PCRTSGSEG pcaSeg, unsigned cSeg);
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Resets the internal buffer position of the S/G buffer to the beginning.
|
---|
91 | *
|
---|
92 | * @returns nothing.
|
---|
93 | * @param pSgBuf The S/G buffer to reset.
|
---|
94 | */
|
---|
95 | RTDECL(void) RTSgBufReset(PRTSGBUF pSgBuf);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Clones a given S/G buffer.
|
---|
99 | *
|
---|
100 | * @returns nothing.
|
---|
101 | * @param pSgBufNew The new S/G buffer to clone to.
|
---|
102 | * @param pSgBufOld The source S/G buffer to clone from.
|
---|
103 | *
|
---|
104 | * @note This is only a shallow copy. Both S/G buffers will point to the
|
---|
105 | * same segment array.
|
---|
106 | */
|
---|
107 | RTDECL(void) RTSgBufClone(PRTSGBUF pSgBufNew, PCRTSGBUF pSgBufOld);
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Copy data between two S/G buffers.
|
---|
111 | *
|
---|
112 | * @returns The number of bytes copied.
|
---|
113 | * @param pSgBufDst The destination S/G buffer.
|
---|
114 | * @param pSgBufSrc The source S/G buffer.
|
---|
115 | * @param cbCopy Number of bytes to copy.
|
---|
116 | *
|
---|
117 | * @note This operation advances the internal buffer pointer of both S/G buffers.
|
---|
118 | */
|
---|
119 | RTDECL(size_t) RTSgBufCopy(PRTSGBUF pSgBufDst, PRTSGBUF pSgBufSrc, size_t cbCopy);
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Compares the content of two S/G buffers.
|
---|
123 | *
|
---|
124 | * @returns Whatever memcmp returns.
|
---|
125 | * @param pSgBuf1 First S/G buffer.
|
---|
126 | * @param pSgBuf2 Second S/G buffer.
|
---|
127 | * @param cbCmp How many bytes to compare.
|
---|
128 | *
|
---|
129 | * @note This operation doesn't change the internal position of the S/G buffers.
|
---|
130 | */
|
---|
131 | RTDECL(int) RTSgBufCmp(PCRTSGBUF pSgBuf1, PCRTSGBUF pSgBuf2, size_t cbCmp);
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Fills an S/G buf with a constant byte.
|
---|
135 | *
|
---|
136 | * @returns The number of actually filled bytes.
|
---|
137 | * Can be less than than cbSet if the end of the S/G buffer was reached.
|
---|
138 | * @param pSgBuf The S/G buffer.
|
---|
139 | * @param ubFill The byte to fill the buffer with.
|
---|
140 | * @param cbSet How many bytes to set.
|
---|
141 | *
|
---|
142 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
143 | */
|
---|
144 | RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet);
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Copies data from an S/G buffer into a given non scattered buffer.
|
---|
148 | *
|
---|
149 | * @returns Number of bytes copied.
|
---|
150 | * @param pSgBuf The S/G buffer to copy from.
|
---|
151 | * @param pvBuf Buffer to copy the data into.
|
---|
152 | * @param cbCopy How many bytes to copy.
|
---|
153 | *
|
---|
154 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
155 | */
|
---|
156 | RTDECL(size_t) RTSgBufCopyToBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy);
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Copies data from a non scattered buffer into an S/G buffer.
|
---|
160 | *
|
---|
161 | * @returns Number of bytes copied.
|
---|
162 | * @param pSgBuf The S/G buffer to copy to.
|
---|
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) RTSgBufCopyFromBuf(PRTSGBUF pSgBuf, void *pvBuf, size_t cbCopy);
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Advances the internal buffer pointer.
|
---|
172 | *
|
---|
173 | * @returns Number of bytes the pointer was moved forward.
|
---|
174 | * @param pSgBuf The S/G buffer.
|
---|
175 | * @param cbAdvance Number of bytes to move forward.
|
---|
176 | */
|
---|
177 | RTDECL(size_t) RTSgBufAdvance(PRTSGBUF pSgBuf, size_t cbAdvance);
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Constructs a new segment array starting from the current position
|
---|
181 | * and describing the given number of bytes.
|
---|
182 | *
|
---|
183 | * @returns Number of bytes the array describes.
|
---|
184 | * @param pSgBuf The S/G buffer.
|
---|
185 | * @param paSeg The unitialized segment array.
|
---|
186 | * @param pcSeg The number of segments the given array has.
|
---|
187 | * This will hold the actual number of entries needed upon return.
|
---|
188 | * @param cbData Number of bytes the new array should describe.
|
---|
189 | *
|
---|
190 | * @note This operation advances the internal buffer pointer of the S/G buffer.
|
---|
191 | */
|
---|
192 | RTDECL(size_t) RTSgBufSegArrayCreate(PRTSGBUF pSgBuf, PRTSGSEG paSeg, unsigned *pcSeg, size_t cbData);
|
---|
193 |
|
---|
194 | RT_C_DECLS_END
|
---|
195 |
|
---|
196 | /** @} */
|
---|
197 |
|
---|
198 | #endif
|
---|
199 |
|
---|