Make QRingBuffer a 64-bit safe
According to I/O API, QIODevice and its inherited classes should be able to process a full 64-bit offsets and lengths. This requires 64-bit parameters in operations with internal buffers. Rework QRingBuffer to avoid implicit truncation of numbers and fix some 64-bit issues in code. Change-Id: Iadd6fd5fefd2d64e6c084e2feebb4dc2d6df66de Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
2d5210a684
commit
9dd0bb851b
@ -481,7 +481,7 @@ bool QFileDevicePrivate::putCharHelper(char c)
|
||||
#else
|
||||
|
||||
// Cutoff for code that doesn't only touch the buffer.
|
||||
int writeBufferSize = writeBuffer.size();
|
||||
qint64 writeBufferSize = writeBuffer.size();
|
||||
if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE
|
||||
#ifdef Q_OS_WIN
|
||||
|| ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE)
|
||||
|
@ -1912,12 +1912,12 @@ qint64 QProcess::readData(char *data, qint64 maxlen)
|
||||
return 1;
|
||||
}
|
||||
|
||||
qint64 bytesToRead = qint64(qMin(readBuffer->size(), (int)maxlen));
|
||||
qint64 bytesToRead = qMin(readBuffer->size(), maxlen);
|
||||
qint64 readSoFar = 0;
|
||||
while (readSoFar < bytesToRead) {
|
||||
const char *ptr = readBuffer->readPointer();
|
||||
int bytesToReadFromThisBlock = qMin<qint64>(bytesToRead - readSoFar,
|
||||
readBuffer->nextDataBlockSize());
|
||||
qint64 bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar,
|
||||
readBuffer->nextDataBlockSize());
|
||||
memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
|
||||
readSoFar += bytesToReadFromThisBlock;
|
||||
readBuffer->free(bytesToReadFromThisBlock);
|
||||
|
@ -45,8 +45,8 @@ QWindowsPipeReader::QWindowsPipeReader(QObject *parent)
|
||||
handle(INVALID_HANDLE_VALUE),
|
||||
readBufferMaxSize(0),
|
||||
actualReadBufferSize(0),
|
||||
readSequenceStarted(false),
|
||||
emitReadyReadTimer(new QTimer(this)),
|
||||
readSequenceStarted(false),
|
||||
pipeBroken(false),
|
||||
readyReadEmitted(false)
|
||||
{
|
||||
@ -133,12 +133,12 @@ qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
|
||||
actualReadBufferSize--;
|
||||
readSoFar = 1;
|
||||
} else {
|
||||
qint64 bytesToRead = qMin(qint64(actualReadBufferSize), maxlen);
|
||||
qint64 bytesToRead = qMin(actualReadBufferSize, maxlen);
|
||||
readSoFar = 0;
|
||||
while (readSoFar < bytesToRead) {
|
||||
const char *ptr = readBuffer.readPointer();
|
||||
int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar,
|
||||
qint64(readBuffer.nextDataBlockSize()));
|
||||
qint64 bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar,
|
||||
readBuffer.nextDataBlockSize());
|
||||
memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
|
||||
readSoFar += bytesToReadFromThisBlock;
|
||||
readBuffer.free(bytesToReadFromThisBlock);
|
||||
|
@ -98,9 +98,9 @@ private:
|
||||
QWinOverlappedIoNotifier *dataReadNotifier;
|
||||
qint64 readBufferMaxSize;
|
||||
QRingBuffer readBuffer;
|
||||
int actualReadBufferSize;
|
||||
bool readSequenceStarted;
|
||||
qint64 actualReadBufferSize;
|
||||
QTimer *emitReadyReadTimer;
|
||||
bool readSequenceStarted;
|
||||
bool pipeBroken;
|
||||
bool readyReadEmitted;
|
||||
};
|
||||
|
@ -58,7 +58,7 @@ public:
|
||||
buffers.append(QByteArray());
|
||||
}
|
||||
|
||||
inline int nextDataBlockSize() const {
|
||||
inline qint64 nextDataBlockSize() const {
|
||||
return (tailBuffer == 0 ? tail : buffers.first().size()) - head;
|
||||
}
|
||||
|
||||
@ -86,9 +86,9 @@ public:
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline void free(int bytes) {
|
||||
inline void free(qint64 bytes) {
|
||||
while (bytes > 0) {
|
||||
int blockSize = buffers.first().size() - head;
|
||||
const qint64 blockSize = buffers.first().size() - head;
|
||||
|
||||
if (tailBuffer == 0 || blockSize > bytes) {
|
||||
// keep a single block around if it does not exceed
|
||||
@ -102,7 +102,8 @@ public:
|
||||
clear(); // try to minify/squeeze us
|
||||
}
|
||||
} else {
|
||||
head += bytes;
|
||||
Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
|
||||
head += int(bytes);
|
||||
bufferSize -= bytes;
|
||||
}
|
||||
return;
|
||||
@ -116,13 +117,15 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
inline char *reserve(int bytes) {
|
||||
if (bytes <= 0)
|
||||
inline char *reserve(qint64 bytes) {
|
||||
if (bytes <= 0 || quint64(bytes) >= QByteArray::MaxSize)
|
||||
return 0;
|
||||
|
||||
const qint64 newSize = bytes + tail;
|
||||
// if need buffer reallocation
|
||||
if (tail + bytes > buffers.last().size()) {
|
||||
if (tail + bytes > buffers.last().capacity() && tail >= basicBlockSize) {
|
||||
if (newSize > buffers.last().size()) {
|
||||
if (newSize > buffers.last().capacity() && (tail >= basicBlockSize
|
||||
|| quint64(newSize) >= QByteArray::MaxSize)) {
|
||||
// shrink this buffer to its current size
|
||||
buffers.last().resize(tail);
|
||||
|
||||
@ -131,21 +134,22 @@ public:
|
||||
++tailBuffer;
|
||||
tail = 0;
|
||||
}
|
||||
buffers.last().resize(qMax(basicBlockSize, tail + bytes));
|
||||
buffers.last().resize(qMax(basicBlockSize, tail + int(bytes)));
|
||||
}
|
||||
|
||||
char *writePtr = buffers.last().data() + tail;
|
||||
bufferSize += bytes;
|
||||
tail += bytes;
|
||||
Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
|
||||
tail += int(bytes);
|
||||
return writePtr;
|
||||
}
|
||||
|
||||
inline void truncate(int pos) {
|
||||
inline void truncate(qint64 pos) {
|
||||
if (pos < size())
|
||||
chop(size() - pos);
|
||||
}
|
||||
|
||||
inline void chop(int bytes) {
|
||||
inline void chop(qint64 bytes) {
|
||||
while (bytes > 0) {
|
||||
if (tailBuffer == 0 || tail > bytes) {
|
||||
// keep a single block around if it does not exceed
|
||||
@ -159,7 +163,8 @@ public:
|
||||
clear(); // try to minify/squeeze us
|
||||
}
|
||||
} else {
|
||||
tail -= bytes;
|
||||
Q_ASSERT(quint64(bytes) < QByteArray::MaxSize);
|
||||
tail -= int(bytes);
|
||||
bufferSize -= bytes;
|
||||
}
|
||||
return;
|
||||
@ -206,7 +211,7 @@ public:
|
||||
++bufferSize;
|
||||
}
|
||||
|
||||
inline int size() const {
|
||||
inline qint64 size() const {
|
||||
return bufferSize;
|
||||
}
|
||||
|
||||
@ -219,9 +224,9 @@ public:
|
||||
bufferSize = 0;
|
||||
}
|
||||
|
||||
inline int indexOf(char c) const {
|
||||
int index = 0;
|
||||
int j = head;
|
||||
inline qint64 indexOf(char c) const {
|
||||
qint64 index = 0;
|
||||
qint64 j = head;
|
||||
for (int i = 0; i < buffers.size(); ++i) {
|
||||
const char *ptr = buffers[i].constData() + j;
|
||||
j = index + (i == tailBuffer ? tail : buffers[i].size()) - j;
|
||||
@ -236,9 +241,9 @@ public:
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline int indexOf(char c, int maxLength) const {
|
||||
int index = 0;
|
||||
int j = head;
|
||||
inline qint64 indexOf(char c, qint64 maxLength) const {
|
||||
qint64 index = 0;
|
||||
qint64 j = head;
|
||||
for (int i = 0; index < maxLength && i < buffers.size(); ++i) {
|
||||
const char *ptr = buffers[i].constData() + j;
|
||||
j = qMin(index + (i == tailBuffer ? tail : buffers[i].size()) - j, maxLength);
|
||||
@ -253,11 +258,12 @@ public:
|
||||
return -1;
|
||||
}
|
||||
|
||||
inline int read(char *data, int maxLength) {
|
||||
int bytesToRead = qMin(size(), maxLength);
|
||||
int readSoFar = 0;
|
||||
inline qint64 read(char *data, qint64 maxLength) {
|
||||
const qint64 bytesToRead = qMin(size(), maxLength);
|
||||
qint64 readSoFar = 0;
|
||||
while (readSoFar < bytesToRead) {
|
||||
int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar, nextDataBlockSize());
|
||||
const qint64 bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar,
|
||||
nextDataBlockSize());
|
||||
if (data)
|
||||
memcpy(data + readSoFar, readPointer(), bytesToReadFromThisBlock);
|
||||
readSoFar += bytesToReadFromThisBlock;
|
||||
@ -300,15 +306,15 @@ public:
|
||||
bufferSize += tail;
|
||||
}
|
||||
|
||||
inline int skip(int length) {
|
||||
inline qint64 skip(qint64 length) {
|
||||
return read(0, length);
|
||||
}
|
||||
|
||||
inline int readLine(char *data, int maxLength) {
|
||||
inline qint64 readLine(char *data, qint64 maxLength) {
|
||||
if (!data || --maxLength <= 0)
|
||||
return -1;
|
||||
|
||||
int i = indexOf('\n', maxLength);
|
||||
qint64 i = indexOf('\n', maxLength);
|
||||
i = read(data, i >= 0 ? (i + 1) : maxLength);
|
||||
|
||||
// Terminate it.
|
||||
@ -325,7 +331,7 @@ private:
|
||||
int head, tail;
|
||||
int tailBuffer; // always buffers.size() - 1
|
||||
const int basicBlockSize;
|
||||
int bufferSize;
|
||||
qint64 bufferSize;
|
||||
};
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -812,7 +812,7 @@ bool QAbstractSocketPrivate::canWriteNotification()
|
||||
#if defined (QABSTRACTSOCKET_DEBUG)
|
||||
qDebug("QAbstractSocketPrivate::canWriteNotification() flushing");
|
||||
#endif
|
||||
int tmp = writeBuffer.size();
|
||||
qint64 tmp = writeBuffer.size();
|
||||
flush();
|
||||
|
||||
if (socketEngine) {
|
||||
@ -872,7 +872,7 @@ bool QAbstractSocketPrivate::flush()
|
||||
return false;
|
||||
}
|
||||
|
||||
int nextSize = writeBuffer.nextDataBlockSize();
|
||||
qint64 nextSize = writeBuffer.nextDataBlockSize();
|
||||
const char *ptr = writeBuffer.readPointer();
|
||||
|
||||
// Attempt to write it all in one chunk.
|
||||
@ -1707,9 +1707,9 @@ qint64 QAbstractSocket::bytesToWrite() const
|
||||
{
|
||||
Q_D(const QAbstractSocket);
|
||||
#if defined(QABSTRACTSOCKET_DEBUG)
|
||||
qDebug("QAbstractSocket::bytesToWrite() == %i", d->writeBuffer.size());
|
||||
qDebug("QAbstractSocket::bytesToWrite() == %lld", d->writeBuffer.size());
|
||||
#endif
|
||||
return (qint64)d->writeBuffer.size();
|
||||
return d->writeBuffer.size();
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -60,7 +60,7 @@ void tst_QRingBuffer::sizeWhenReserved()
|
||||
QRingBuffer ringBuffer;
|
||||
ringBuffer.reserve(5);
|
||||
|
||||
QCOMPARE(ringBuffer.size(), 5);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(5));
|
||||
}
|
||||
|
||||
void tst_QRingBuffer::sizeWhenReservedAndChopped()
|
||||
@ -69,14 +69,14 @@ void tst_QRingBuffer::sizeWhenReservedAndChopped()
|
||||
ringBuffer.reserve(31337);
|
||||
ringBuffer.chop(31337);
|
||||
|
||||
QCOMPARE(ringBuffer.size(), 0);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(0));
|
||||
}
|
||||
|
||||
void tst_QRingBuffer::sizeWhenEmpty()
|
||||
{
|
||||
QRingBuffer ringBuffer;
|
||||
|
||||
QCOMPARE(ringBuffer.size(), 0);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(0));
|
||||
}
|
||||
|
||||
void tst_QRingBuffer::readPointerAtPositionReadTooMuch()
|
||||
@ -101,22 +101,22 @@ void tst_QRingBuffer::readPointerAtPositionWithHead()
|
||||
qint64 length;
|
||||
const char* buf2 = ringBuffer.readPointerAtPosition(0, length);
|
||||
|
||||
QCOMPARE(length, qint64(2));
|
||||
QCOMPARE(length, Q_INT64_C(2));
|
||||
QVERIFY(*buf2 == '2');
|
||||
QVERIFY(*(buf2+1) == '3');
|
||||
|
||||
// advance 2 more, ringBuffer should be empty then
|
||||
ringBuffer.free(2);
|
||||
buf2 = ringBuffer.readPointerAtPosition(0, length);
|
||||
QCOMPARE(length, qint64(0));
|
||||
QCOMPARE(length, Q_INT64_C(0));
|
||||
QVERIFY(buf2 == 0);
|
||||
|
||||
// check buffer with 2 blocks
|
||||
memcpy(ringBuffer.reserve(4), "0123", 4);
|
||||
ringBuffer.append(QByteArray("45678", 5));
|
||||
ringBuffer.free(3);
|
||||
buf2 = ringBuffer.readPointerAtPosition(1, length);
|
||||
QCOMPARE(length, qint64(5));
|
||||
buf2 = ringBuffer.readPointerAtPosition(Q_INT64_C(1), length);
|
||||
QCOMPARE(length, Q_INT64_C(5));
|
||||
}
|
||||
|
||||
void tst_QRingBuffer::readPointerAtPositionEmptyRead()
|
||||
@ -149,14 +149,14 @@ void tst_QRingBuffer::readPointerAtPositionWriteRead()
|
||||
// write in chunks of 50 bytes
|
||||
// this ensures there will be multiple QByteArrays inside the QRingBuffer
|
||||
// since QRingBuffer is then only using individual arrays of around 4000 bytes
|
||||
qint64 thisWrite = qMin(remaining, qint64(50));
|
||||
qint64 thisWrite = qMin(remaining, Q_INT64_C(50));
|
||||
char *pos = ringBuffer.reserve(thisWrite);
|
||||
inData.read(pos, thisWrite);
|
||||
remaining -= thisWrite;
|
||||
}
|
||||
// was data put into it?
|
||||
QVERIFY(ringBuffer.size() > 0);
|
||||
QCOMPARE(qint64(ringBuffer.size()), inData.size());
|
||||
QCOMPARE(ringBuffer.size(), inData.size());
|
||||
|
||||
//read from the QRingBuffer in loop, put back into another QBuffer
|
||||
QBuffer outData;
|
||||
@ -187,12 +187,12 @@ void tst_QRingBuffer::free()
|
||||
ringBuffer.append(QByteArray("01234", 5));
|
||||
|
||||
ringBuffer.free(1);
|
||||
QCOMPARE(ringBuffer.size(), 4095 + 2048 + 5);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(4095 + 2048 + 5));
|
||||
ringBuffer.free(4096);
|
||||
QCOMPARE(ringBuffer.size(), 2047 + 5);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(2047 + 5));
|
||||
ringBuffer.free(48);
|
||||
ringBuffer.free(2000);
|
||||
QCOMPARE(ringBuffer.size(), 4);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(4));
|
||||
QVERIFY(memcmp(ringBuffer.readPointer(), "1234", 4) == 0);
|
||||
}
|
||||
|
||||
@ -211,8 +211,8 @@ void tst_QRingBuffer::reserveAndRead()
|
||||
for (int i = 1; i < 256; ++i) {
|
||||
QByteArray ba;
|
||||
ba.resize(i);
|
||||
int thisRead = ringBuffer.read(ba.data(), i);
|
||||
QCOMPARE(thisRead, i);
|
||||
qint64 thisRead = ringBuffer.read(ba.data(), i);
|
||||
QCOMPARE(thisRead, qint64(i));
|
||||
QVERIFY(ba.count(char(i)) == i);
|
||||
}
|
||||
QVERIFY(ringBuffer.size() == 0);
|
||||
@ -227,12 +227,12 @@ void tst_QRingBuffer::chop()
|
||||
ringBuffer.reserve(4096);
|
||||
|
||||
ringBuffer.chop(1);
|
||||
QCOMPARE(ringBuffer.size(), 5 + 2048 + 4095);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(5 + 2048 + 4095));
|
||||
ringBuffer.chop(4096);
|
||||
QCOMPARE(ringBuffer.size(), 5 + 2047);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(5 + 2047));
|
||||
ringBuffer.chop(48);
|
||||
ringBuffer.chop(2000);
|
||||
QCOMPARE(ringBuffer.size(), 4);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(4));
|
||||
QVERIFY(memcmp(ringBuffer.readPointer(), "0123", 4) == 0);
|
||||
}
|
||||
|
||||
@ -248,7 +248,7 @@ void tst_QRingBuffer::ungetChar()
|
||||
ringBuffer.getChar();
|
||||
ringBuffer.ungetChar(char(c)); // unget first char
|
||||
}
|
||||
QCOMPARE(ringBuffer.size(), 1);
|
||||
QCOMPARE(ringBuffer.size(), Q_INT64_C(1));
|
||||
}
|
||||
|
||||
void tst_QRingBuffer::indexOf()
|
||||
@ -258,8 +258,8 @@ void tst_QRingBuffer::indexOf()
|
||||
ringBuffer.putChar(char(i));
|
||||
|
||||
for (int i = 1; i < 256; ++i) {
|
||||
int index = ringBuffer.indexOf(char(i));
|
||||
QCOMPARE(i - 1, index);
|
||||
qint64 index = ringBuffer.indexOf(char(i));
|
||||
QCOMPARE(qint64(i - 1), index);
|
||||
QCOMPARE(index, ringBuffer.indexOf(char(i), i));
|
||||
QVERIFY(ringBuffer.indexOf(char(i), i - 1) == -1); // test for absent char
|
||||
}
|
||||
@ -298,7 +298,7 @@ void tst_QRingBuffer::readLine()
|
||||
|
||||
// check first empty string reading
|
||||
stringBuf[0] = char(0xFF);
|
||||
QCOMPARE(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2), ba2.size());
|
||||
QCOMPARE(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2), qint64(ba2.size()));
|
||||
QVERIFY(stringBuf[0] == ba2[0]);
|
||||
|
||||
QVERIFY(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2) == (ba3.size() + ba4.size()
|
||||
|
@ -48,10 +48,10 @@ void tst_qringbuffer::reserveAndRead()
|
||||
{
|
||||
QRingBuffer ringBuffer;
|
||||
QBENCHMARK {
|
||||
for (int i = 1; i < 256; ++i)
|
||||
for (qint64 i = 1; i < 256; ++i)
|
||||
ringBuffer.reserve(i);
|
||||
|
||||
for (int i = 1; i < 256; ++i)
|
||||
for (qint64 i = 1; i < 256; ++i)
|
||||
ringBuffer.read(0, i);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user