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/qvector.h>
57
58QT_BEGIN_NAMESPACE
59
60#ifndef QRINGBUFFER_CHUNKSIZE
61#define QRINGBUFFER_CHUNKSIZE 4096
62#endif
63
64class QRingChunk
65{
66public:
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 inline QRingChunk &operator=(QRingChunk &&other) noexcept
98 {
99 swap(other);
100 return *this;
101 }
102
103 inline void swap(QRingChunk &other) noexcept
104 {
105 chunk.swap(other&: other.chunk);
106 qSwap(value1&: headOffset, value2&: other.headOffset);
107 qSwap(value1&: tailOffset, value2&: other.tailOffset);
108 }
109
110 // allocating and sharing
111 void allocate(int alloc);
112 inline bool isShared() const
113 {
114 return !chunk.isDetached();
115 }
116 Q_CORE_EXPORT void detach();
117 QByteArray toByteArray();
118
119 // getters
120 inline int head() const
121 {
122 return headOffset;
123 }
124 inline int size() const
125 {
126 return tailOffset - headOffset;
127 }
128 inline int capacity() const
129 {
130 return chunk.size();
131 }
132 inline int available() const
133 {
134 return chunk.size() - tailOffset;
135 }
136 inline const char *data() const
137 {
138 return chunk.constData() + headOffset;
139 }
140 inline char *data()
141 {
142 if (isShared())
143 detach();
144 return chunk.data() + headOffset;
145 }
146
147 // array management
148 inline void advance(int offset)
149 {
150 Q_ASSERT(headOffset + offset >= 0);
151 Q_ASSERT(size() - offset > 0);
152
153 headOffset += offset;
154 }
155 inline void grow(int offset)
156 {
157 Q_ASSERT(size() + offset > 0);
158 Q_ASSERT(head() + size() + offset <= capacity());
159
160 tailOffset += offset;
161 }
162 inline void assign(const QByteArray &qba)
163 {
164 chunk = qba;
165 headOffset = 0;
166 tailOffset = qba.size();
167 }
168 inline void reset()
169 {
170 headOffset = tailOffset = 0;
171 }
172 inline void clear()
173 {
174 assign(qba: QByteArray());
175 }
176
177private:
178 QByteArray chunk;
179 int headOffset, tailOffset;
180};
181
182class QRingBuffer
183{
184public:
185 explicit inline QRingBuffer(int growth = QRINGBUFFER_CHUNKSIZE) :
186 bufferSize(0), basicBlockSize(growth) { }
187
188 inline void setChunkSize(int size) {
189 basicBlockSize = size;
190 }
191
192 inline int chunkSize() const {
193 return basicBlockSize;
194 }
195
196 inline qint64 nextDataBlockSize() const {
197 return bufferSize == 0 ? Q_INT64_C(0) : buffers.first().size();
198 }
199
200 inline const char *readPointer() const {
201 return bufferSize == 0 ? nullptr : buffers.first().data();
202 }
203
204 Q_CORE_EXPORT const char *readPointerAtPosition(qint64 pos, qint64 &length) const;
205 Q_CORE_EXPORT void free(qint64 bytes);
206 Q_CORE_EXPORT char *reserve(qint64 bytes);
207 Q_CORE_EXPORT char *reserveFront(qint64 bytes);
208
209 inline void truncate(qint64 pos) {
210 Q_ASSERT(pos >= 0 && pos <= size());
211
212 chop(bytes: size() - pos);
213 }
214
215 Q_CORE_EXPORT void chop(qint64 bytes);
216
217 inline bool isEmpty() const {
218 return bufferSize == 0;
219 }
220
221 inline int getChar() {
222 if (isEmpty())
223 return -1;
224 char c = *readPointer();
225 free(bytes: 1);
226 return int(uchar(c));
227 }
228
229 inline void putChar(char c) {
230 char *ptr = reserve(bytes: 1);
231 *ptr = c;
232 }
233
234 void ungetChar(char c)
235 {
236 char *ptr = reserveFront(bytes: 1);
237 *ptr = c;
238 }
239
240
241 inline qint64 size() const {
242 return bufferSize;
243 }
244
245 Q_CORE_EXPORT void clear();
246 inline qint64 indexOf(char c) const { return indexOf(c, maxLength: size()); }
247 Q_CORE_EXPORT qint64 indexOf(char c, qint64 maxLength, qint64 pos = 0) const;
248 Q_CORE_EXPORT qint64 read(char *data, qint64 maxLength);
249 Q_CORE_EXPORT QByteArray read();
250 Q_CORE_EXPORT qint64 peek(char *data, qint64 maxLength, qint64 pos = 0) const;
251 Q_CORE_EXPORT void append(const char *data, qint64 size);
252 Q_CORE_EXPORT void append(const QByteArray &qba);
253
254 inline qint64 skip(qint64 length) {
255 qint64 bytesToSkip = qMin(a: length, b: bufferSize);
256
257 free(bytes: bytesToSkip);
258 return bytesToSkip;
259 }
260
261 Q_CORE_EXPORT qint64 readLine(char *data, qint64 maxLength);
262
263 inline bool canReadLine() const {
264 return indexOf(c: '\n') >= 0;
265 }
266
267private:
268 QVector<QRingChunk> buffers;
269 qint64 bufferSize;
270 int basicBlockSize;
271};
272
273Q_DECLARE_SHARED(QRingChunk)
274Q_DECLARE_TYPEINFO(QRingBuffer, Q_MOVABLE_TYPE);
275
276QT_END_NAMESPACE
277
278#endif // QRINGBUFFER_P_H
279

source code of qtbase/src/corelib/tools/qringbuffer_p.h