1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
36
// This file is not part of the Qt API. It exists for the convenience
37
// of a number of Qt sources files. This header file may change from
38
// version to version without notice, or even be removed.
43
#include "qnamespace.h"
45
#include "qiodevice.h"
46
#include "qbytearray.h"
52
class QCircularBuffer {
54
uint curr_used, start_off, start_buff, curr_buff, buff_growth;
56
inline QCircularBuffer(uint growth) : curr_used(0), start_off(0), start_buff(0),
57
curr_buff(0), buff_growth(growth) { }
59
char *alloc(uint buflen);
60
char *take(uint maxsize, uint *realsize=0);
61
inline void free(uint buflen);
63
inline void truncate(uint len) { curr_used -= len; }
65
inline int growth() const { return buff_growth; }
66
inline int numBuffers() const { return 2; }
67
inline bool isEmpty() const { return !used(); }
68
inline uint used() const { return curr_used; }
69
inline void clear() { if(!isEmpty()) free(used()); }
72
inline char *QCircularBuffer::alloc(uint size)
74
if(buf[curr_buff].size() <
75
(int)(curr_used+size+(curr_buff == start_buff ? start_off : 0))) {
76
if(curr_buff == start_buff && buf[curr_buff].size()) {
77
buf[curr_buff].resize(start_off + curr_used);
78
curr_buff = !curr_buff;
79
if(!buf[curr_buff].size())
80
buf[curr_buff].resize(buff_growth*2);
82
int sz = buf[curr_buff].size();
83
buf[curr_buff].resize(qMax((uint)sz + (sz / 2), (buff_growth*2)));
88
if(curr_buff != start_buff)
89
off -= buf[start_buff].size() - start_off;
92
return buf[curr_buff].data()+off;
94
inline char *QCircularBuffer::take(uint size, uint *real_size)
96
if(size > curr_used) {
97
qWarning("Warning: asked to take too much %d [%d]", size, curr_used);
101
*real_size = qMin(size, buf[start_buff].size() - start_off);
102
return buf[start_buff].data()+start_off;
105
inline void QCircularBuffer::free(uint size)
107
if(size > curr_used) {
108
qWarning("Warning: asked to free too much %d [%d]", size, curr_used);
113
curr_buff = start_buff = start_off = 0;
117
uint start_size = buf[start_buff].size() - start_off;
118
if(start_size > size) {
120
} else if(start_buff != curr_buff) {
121
start_buff = curr_buff;
122
start_off = start_size - size;
128
inline void QCircularBuffer::push(char ch)
131
buf[start_buff].insert(start_off, ch);
134
class Q_CORE_EXPORT QRingBuffer
137
QRingBuffer(int growth = 4096);
139
int nextDataBlockSize() const;
140
char *readPointer() const;
141
void free(int bytes);
142
char *reserve(int bytes);
143
void truncate(int bytes);
145
bool isEmpty() const;
148
void putChar(char c);
149
void ungetChar(char c);
153
int indexOf(char c) const;
154
int readLine(char *data, int maxLength);
155
bool canReadLine() const;
158
QList<QByteArray> buffers;
165
#endif // QINTERNAL_P_H