1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QRINGBUFFER_P_H |
41 | #define QRINGBUFFER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists for the convenience |
48 | // of a number of Qt sources files. This header file may change from |
49 | // version to version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/private/qglobal_p.h> |
55 | #include <QtCore/qbytearray.h> |
56 | #include <QtCore/qlist.h> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | #ifndef QRINGBUFFER_CHUNKSIZE |
61 | #define QRINGBUFFER_CHUNKSIZE 4096 |
62 | #endif |
63 | |
64 | class QRingChunk |
65 | { |
66 | public: |
67 | // initialization and cleanup |
68 | inline QRingChunk() noexcept : |
69 | headOffset(0), tailOffset(0) |
70 | { |
71 | } |
72 | inline QRingChunk(const QRingChunk &other) noexcept : |
73 | chunk(other.chunk), headOffset(other.headOffset), tailOffset(other.tailOffset) |
74 | { |
75 | } |
76 | explicit inline QRingChunk(int alloc) : |
77 | chunk(alloc, Qt::Uninitialized), headOffset(0), tailOffset(0) |
78 | { |
79 | } |
80 | explicit inline QRingChunk(const QByteArray &qba) noexcept : |
81 | chunk(qba), headOffset(0), tailOffset(qba.size()) |
82 | { |
83 | } |
84 | |
85 | inline QRingChunk &operator=(const QRingChunk &other) noexcept |
86 | { |
87 | chunk = other.chunk; |
88 | headOffset = other.headOffset; |
89 | tailOffset = other.tailOffset; |
90 | return *this; |
91 | } |
92 | inline QRingChunk(QRingChunk &&other) noexcept : |
93 | chunk(other.chunk), headOffset(other.headOffset), tailOffset(other.tailOffset) |
94 | { |
95 | other.headOffset = other.tailOffset = 0; |
96 | } |
97 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QRingChunk) |
98 | |
99 | inline void swap(QRingChunk &other) noexcept |
100 | { |
101 | chunk.swap(other.chunk); |
102 | qSwap(headOffset, other.headOffset); |
103 | qSwap(tailOffset, other.tailOffset); |
104 | } |
105 | |
106 | // allocating and sharing |
107 | void allocate(int alloc); |
108 | inline bool isShared() const |
109 | { |
110 | return !chunk.isDetached(); |
111 | } |
112 | Q_CORE_EXPORT void detach(); |
113 | QByteArray toByteArray(); |
114 | |
115 | // getters |
116 | inline int head() const |
117 | { |
118 | return headOffset; |
119 | } |
120 | inline int size() const |
121 | { |
122 | return tailOffset - headOffset; |
123 | } |
124 | inline int capacity() const |
125 | { |
126 | return chunk.size(); |
127 | } |
128 | inline int available() const |
129 | { |
130 | return chunk.size() - tailOffset; |
131 | } |
132 | inline const char *data() const |
133 | { |
134 | return chunk.constData() + headOffset; |
135 | } |
136 | inline char *data() |
137 | { |
138 | if (isShared()) |
139 | detach(); |
140 | return chunk.data() + headOffset; |
141 | } |
142 | |
143 | // array management |
144 | inline void advance(int offset) |
145 | { |
146 | Q_ASSERT(headOffset + offset >= 0); |
147 | Q_ASSERT(size() - offset > 0); |
148 | |
149 | headOffset += offset; |
150 | } |
151 | inline void grow(int offset) |
152 | { |
153 | Q_ASSERT(size() + offset > 0); |
154 | Q_ASSERT(head() + size() + offset <= capacity()); |
155 | |
156 | tailOffset += offset; |
157 | } |
158 | inline void assign(const QByteArray &qba) |
159 | { |
160 | chunk = qba; |
161 | headOffset = 0; |
162 | tailOffset = qba.size(); |
163 | } |
164 | inline void reset() |
165 | { |
166 | headOffset = tailOffset = 0; |
167 | } |
168 | inline void clear() |
169 | { |
170 | assign(QByteArray()); |
171 | } |
172 | |
173 | private: |
174 | QByteArray chunk; |
175 | int headOffset, tailOffset; |
176 | }; |
177 | |
178 | class QRingBuffer |
179 | { |
180 | public: |
181 | explicit inline QRingBuffer(int growth = QRINGBUFFER_CHUNKSIZE) : |
182 | bufferSize(0), basicBlockSize(growth) { } |
183 | |
184 | inline void setChunkSize(int size) { |
185 | basicBlockSize = size; |
186 | } |
187 | |
188 | inline int chunkSize() const { |
189 | return basicBlockSize; |
190 | } |
191 | |
192 | inline qint64 nextDataBlockSize() const { |
193 | return bufferSize == 0 ? Q_INT64_C(0) : buffers.first().size(); |
194 | } |
195 | |
196 | inline const char *readPointer() const { |
197 | return bufferSize == 0 ? nullptr : buffers.first().data(); |
198 | } |
199 | |
200 | Q_CORE_EXPORT const char *readPointerAtPosition(qint64 pos, qint64 &length) const; |
201 | Q_CORE_EXPORT void free(qint64 bytes); |
202 | Q_CORE_EXPORT char *reserve(qint64 bytes); |
203 | Q_CORE_EXPORT char *reserveFront(qint64 bytes); |
204 | |
205 | inline void truncate(qint64 pos) { |
206 | Q_ASSERT(pos >= 0 && pos <= size()); |
207 | |
208 | chop(size() - pos); |
209 | } |
210 | |
211 | Q_CORE_EXPORT void chop(qint64 bytes); |
212 | |
213 | inline bool isEmpty() const { |
214 | return bufferSize == 0; |
215 | } |
216 | |
217 | inline int getChar() { |
218 | if (isEmpty()) |
219 | return -1; |
220 | char c = *readPointer(); |
221 | free(1); |
222 | return int(uchar(c)); |
223 | } |
224 | |
225 | inline void putChar(char c) { |
226 | char *ptr = reserve(1); |
227 | *ptr = c; |
228 | } |
229 | |
230 | void ungetChar(char c) |
231 | { |
232 | char *ptr = reserveFront(1); |
233 | *ptr = c; |
234 | } |
235 | |
236 | |
237 | inline qint64 size() const { |
238 | return bufferSize; |
239 | } |
240 | |
241 | Q_CORE_EXPORT void clear(); |
242 | inline qint64 indexOf(char c) const { return indexOf(c, size()); } |
243 | Q_CORE_EXPORT qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const; |
244 | Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength); |
245 | Q_CORE_EXPORT QByteArray read(); |
246 | Q_CORE_EXPORT qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const; |
247 | Q_CORE_EXPORT void append(const char *data, qint64 size); |
248 | Q_CORE_EXPORT void append(const QByteArray &qba); |
249 | |
250 | inline qint64 skip(qint64 length) { |
251 | qint64 bytesToSkip = qMin(length, bufferSize); |
252 | |
253 | free(bytesToSkip); |
254 | return bytesToSkip; |
255 | } |
256 | |
257 | Q_CORE_EXPORT qint64 readLine(char *data, qint64 maxLength); |
258 | |
259 | inline bool canReadLine() const { |
260 | return indexOf('\n') >= 0; |
261 | } |
262 | |
263 | private: |
264 | QList<QRingChunk> buffers; |
265 | qint64 bufferSize; |
266 | int basicBlockSize; |
267 | }; |
268 | |
269 | Q_DECLARE_SHARED(QRingChunk) |
270 | Q_DECLARE_TYPEINFO(QRingBuffer, Q_MOVABLE_TYPE); |
271 | |
272 | QT_END_NAMESPACE |
273 | |
274 | #endif // QRINGBUFFER_P_H |
275 | |