From bf349cbb8604585bc3536b8ae8486e8afc91de49 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sat, 11 Jan 2020 23:07:24 +0000 Subject: [PATCH] introduce QT_BUFFSIZE Signed-off-by: Ivailo Monev --- src/core/global/qplatformdefs.h | 2 ++ src/core/io/qdatastream.cpp | 3 ++- src/core/io/qfile.cpp | 12 +++++----- src/core/io/qiodevice_p.h | 4 ++-- src/core/io/qtextstream.cpp | 10 ++++----- src/core/tools/qringbuffer_p.h | 5 +++-- src/gui/image/qgifhandler.cpp | 14 +++++------- src/gui/image/qjpeghandler.cpp | 17 +++++++------- .../access/qhttpnetworkconnectionchannel.cpp | 2 +- src/network/access/qhttpnetworkreply.cpp | 2 +- src/network/access/qhttpnetworkreply_p.h | 1 - .../access/qnetworkaccessdebugpipebackend.cpp | 15 +++++-------- src/svg/qsvgtinydocument.cpp | 9 ++++---- .../socket/qtcpserver/tst_qtcpserver.cpp | 22 +++++++------------ util/normalize/main.cpp | 3 ++- 15 files changed, 53 insertions(+), 68 deletions(-) diff --git a/src/core/global/qplatformdefs.h b/src/core/global/qplatformdefs.h index 273b1ff3d..69d0dcd05 100644 --- a/src/core/global/qplatformdefs.h +++ b/src/core/global/qplatformdefs.h @@ -196,4 +196,6 @@ #define QT_VSNPRINTF ::vsnprintf #endif +#define QT_BUFFSIZE BUFSIZ + #endif // include guard diff --git a/src/core/io/qdatastream.cpp b/src/core/io/qdatastream.cpp index fe5205713..135610de4 100644 --- a/src/core/io/qdatastream.cpp +++ b/src/core/io/qdatastream.cpp @@ -38,6 +38,7 @@ #include "qbuffer.h" #include "qstring.h" #include "qendian.h" +#include "qplatformdefs.h" #include #include @@ -1120,7 +1121,7 @@ int QDataStream::skipRawData(int len) CHECK_STREAM_PRECOND(-1) if (dev->isSequential()) { - char buf[4096]; + char buf[QT_BUFFSIZE]; int sumRead = 0; while (len > 0) { diff --git a/src/core/io/qfile.cpp b/src/core/io/qfile.cpp index 95e97a349..fd46d63bc 100644 --- a/src/core/io/qfile.cpp +++ b/src/core/io/qfile.cpp @@ -47,12 +47,10 @@ QT_BEGIN_NAMESPACE -static const int QFILE_WRITEBUFFER_SIZE = 16384; - //************* QFilePrivate QFilePrivate::QFilePrivate() : fileEngine(0), lastWasWrite(false), - writeBuffer(QFILE_WRITEBUFFER_SIZE), error(QFile::NoError), + writeBuffer(QT_BUFFSIZE), error(QFile::NoError), cachedSize(0) { } @@ -628,7 +626,7 @@ QFile::rename(const QString &newName) if (open(QIODevice::ReadOnly)) { if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { bool error = false; - char block[4096]; + char block[QT_BUFFSIZE]; qint64 bytes; while ((bytes = read(block, sizeof(block))) > 0) { if (bytes != out.write(block, bytes)) { @@ -1383,7 +1381,7 @@ bool QFilePrivate::putCharHelper(char c) // Cutoff for code that doesn't only touch the buffer. int writeBufferSize = writeBuffer.size(); - if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE) { + if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QT_BUFFSIZE) { return QIODevicePrivate::putCharHelper(c); } @@ -1428,14 +1426,14 @@ QFile::writeData(const char *data, qint64 len) bool buffered = !(d->openMode & Unbuffered); // Flush buffered data if this read will overflow. - if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) { + if (buffered && (d->writeBuffer.size() + len) > QT_BUFFSIZE) { if (!flush()) return -1; } // Write directly to the engine if the block size is larger than // the write buffer size. - if (!buffered || len > QFILE_WRITEBUFFER_SIZE) { + if (!buffered || len > QT_BUFFSIZE) { qint64 ret = d->fileEngine->write(data, len); if(ret < 0) { QFile::FileError err = d->fileEngine->error(); diff --git a/src/core/io/qiodevice_p.h b/src/core/io/qiodevice_p.h index 5cbfd0866..cd233b8c4 100644 --- a/src/core/io/qiodevice_p.h +++ b/src/core/io/qiodevice_p.h @@ -49,14 +49,14 @@ #include "QtCore/qbytearray.h" #include "QtCore/qobjectdefs.h" #include "QtCore/qstring.h" +#include "QtCore/qplatformdefs.h" #ifndef QT_NO_QOBJECT #include "qobject_p.h" #endif QT_BEGIN_NAMESPACE -// BUFSIZ is defined in stdio.h -#define QIODEVICE_BUFFERSIZE qint64(BUFSIZ) +#define QIODEVICE_BUFFERSIZE qint64(QT_BUFFSIZE) // This is QIODevice's read buffer, optimized for read(), isEmpty() and getChar() class QIODevicePrivateLinearBuffer diff --git a/src/core/io/qtextstream.cpp b/src/core/io/qtextstream.cpp index 6121cd828..9b6eef1e9 100644 --- a/src/core/io/qtextstream.cpp +++ b/src/core/io/qtextstream.cpp @@ -32,7 +32,6 @@ ****************************************************************************/ //#define QTEXTSTREAM_DEBUG -static const int QTEXTSTREAM_BUFFERSIZE = 16384; /*! \class QTextStream @@ -220,6 +219,7 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384; #include "qbuffer.h" #include "qfile.h" #include "qnumeric.h" +#include "qplatformdefs.h" #include "qlocale_p.h" #ifndef QT_NO_TEXTCODEC #include "qtextcodec.h" @@ -503,8 +503,8 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes) device->setTextModeEnabled(false); // read raw data into a temporary buffer - if (maxBytes < 1 || maxBytes > QTEXTSTREAM_BUFFERSIZE) - maxBytes = QTEXTSTREAM_BUFFERSIZE; + if (maxBytes < 1 || maxBytes > QT_BUFFSIZE) + maxBytes = QT_BUFFSIZE; const QByteArray buffer = device->read(maxBytes); int bytesRead = buffer.size(); @@ -808,7 +808,7 @@ inline void QTextStreamPrivate::consume(int size) readBufferOffset = 0; readBuffer.clear(); saveConverterState(device->pos()); - } else if (readBufferOffset > QTEXTSTREAM_BUFFERSIZE) { + } else if (readBufferOffset > QT_BUFFSIZE) { readBuffer = readBuffer.remove(0,readBufferOffset); readConverterSavedStateOffset += readBufferOffset; readBufferOffset = 0; @@ -856,7 +856,7 @@ inline void QTextStreamPrivate::write(const QString &data) string->append(data); } else { writeBuffer += data; - if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE) + if (writeBuffer.size() > QT_BUFFSIZE) flushWriteBuffer(); } } diff --git a/src/core/tools/qringbuffer_p.h b/src/core/tools/qringbuffer_p.h index a6b0b4bf3..dd91126ea 100644 --- a/src/core/tools/qringbuffer_p.h +++ b/src/core/tools/qringbuffer_p.h @@ -45,14 +45,15 @@ // We mean it. // -#include +#include "qbytearray.h" +#include "qplatformdefs.h" QT_BEGIN_NAMESPACE class QRingBuffer { public: - inline QRingBuffer(int growth = 4096) : basicBlockSize(growth) { + inline QRingBuffer(int growth = QT_BUFFSIZE) : basicBlockSize(growth) { buffers << QByteArray(); clear(); } diff --git a/src/gui/image/qgifhandler.cpp b/src/gui/image/qgifhandler.cpp index 44252d5e0..6c31665d7 100644 --- a/src/gui/image/qgifhandler.cpp +++ b/src/gui/image/qgifhandler.cpp @@ -36,6 +36,7 @@ #include "qimage.h" #include "qiodevice.h" #include "qvariant.h" +#include "qplatformdefs.h" QT_BEGIN_NAMESPACE @@ -678,8 +679,7 @@ void QGIFFormat::scan(QIODevice *device, QVector *imageSizes, int *loopCo uchar hold[16]; State state = Header; - const int readBufferSize = 40960; // 40k read buffer - QByteArray readBuffer(device->read(readBufferSize)); + QByteArray readBuffer(device->read(QT_BUFFSIZE)); if (readBuffer.isEmpty()) { device->seek(oldPos); @@ -912,7 +912,7 @@ void QGIFFormat::scan(QIODevice *device, QVector *imageSizes, int *loopCo return; } } - readBuffer = device->read(readBufferSize); + readBuffer = device->read(QT_BUFFSIZE); } device->seek(oldPos); return; @@ -1053,11 +1053,9 @@ QGifHandler::~QGifHandler() bool QGifHandler::imageIsComing() const { - const int GifChunkSize = 4096; - while (!gifFormat->partialNewFrame) { if (buffer.isEmpty()) { - buffer += device()->read(GifChunkSize); + buffer += device()->read(QT_BUFFSIZE); if (buffer.isEmpty()) break; } @@ -1097,11 +1095,9 @@ bool QGifHandler::canRead(QIODevice *device) bool QGifHandler::read(QImage *image) { - const int GifChunkSize = 4096; - while (!gifFormat->newFrame) { if (buffer.isEmpty()) { - buffer += device()->read(GifChunkSize); + buffer += device()->read(QT_BUFFSIZE); if (buffer.isEmpty()) break; } diff --git a/src/gui/image/qjpeghandler.cpp b/src/gui/image/qjpeghandler.cpp index 36a29ed45..15dc7df5f 100644 --- a/src/gui/image/qjpeghandler.cpp +++ b/src/gui/image/qjpeghandler.cpp @@ -37,6 +37,7 @@ #include "qvariant.h" #include "qvector.h" #include "qbuffer.h" +#include "qplatformdefs.h" #include // jpeglib needs this to be pre-included #include @@ -96,12 +97,10 @@ static void my_error_exit (j_common_ptr cinfo) #endif -static const int max_buf = 4096; - struct my_jpeg_source_mgr : public jpeg_source_mgr { // Nothing dynamic - cannot rely on destruction over longjump QIODevice *device; - JOCTET buffer[max_buf]; + JOCTET buffer[QT_BUFFSIZE]; const QBuffer *memDevice; public: @@ -126,7 +125,7 @@ static boolean qt_fill_input_buffer(j_decompress_ptr cinfo) src->device->seek(src->memDevice->data().size()); } else { src->next_input_byte = src->buffer; - num_read = src->device->read((char*)src->buffer, max_buf); + num_read = src->device->read((char*)src->buffer, QT_BUFFSIZE); } if (num_read <= 0) { // Insert a fake EOI marker - as per jpeglib recommendation @@ -439,7 +438,7 @@ static bool read_jpeg_image(QImage *outImage, struct my_jpeg_destination_mgr : public jpeg_destination_mgr { // Nothing dynamic - cannot rely on destruction over longjump QIODevice *device; - JOCTET buffer[max_buf]; + JOCTET buffer[QT_BUFFSIZE]; public: my_jpeg_destination_mgr(QIODevice *); @@ -458,12 +457,12 @@ static boolean qt_empty_output_buffer(j_compress_ptr cinfo) { my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; - int written = dest->device->write((char*)dest->buffer, max_buf); + int written = dest->device->write((char*)dest->buffer, QT_BUFFSIZE); if (written == -1) (*cinfo->err->error_exit)((j_common_ptr)cinfo); dest->next_output_byte = dest->buffer; - dest->free_in_buffer = max_buf; + dest->free_in_buffer = QT_BUFFSIZE; return TRUE; } @@ -471,7 +470,7 @@ static boolean qt_empty_output_buffer(j_compress_ptr cinfo) static void qt_term_destination(j_compress_ptr cinfo) { my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; - qint64 n = max_buf - dest->free_in_buffer; + qint64 n = QT_BUFFSIZE - dest->free_in_buffer; qint64 written = dest->device->write((char*)dest->buffer, n); if (written == -1) @@ -489,7 +488,7 @@ inline my_jpeg_destination_mgr::my_jpeg_destination_mgr(QIODevice *device) jpeg_destination_mgr::term_destination = qt_term_destination; this->device = device; next_output_byte = buffer; - free_in_buffer = max_buf; + free_in_buffer = QT_BUFFSIZE; } diff --git a/src/network/access/qhttpnetworkconnectionchannel.cpp b/src/network/access/qhttpnetworkconnectionchannel.cpp index d8b546ba2..4603dd16c 100644 --- a/src/network/access/qhttpnetworkconnectionchannel.cpp +++ b/src/network/access/qhttpnetworkconnectionchannel.cpp @@ -635,7 +635,7 @@ bool QHttpNetworkConnectionChannel::expand(bool dataComplete) Q_ASSERT(reply); qint64 total = reply->d_func()->compressedData.size(); - if (total >= CHUNK || dataComplete) { + if (total >= QT_BUFFSIZE || dataComplete) { // uncompress the data QByteArray content, inflated; content = reply->d_func()->compressedData; diff --git a/src/network/access/qhttpnetworkreply.cpp b/src/network/access/qhttpnetworkreply.cpp index 15240a0bc..e394a07f7 100644 --- a/src/network/access/qhttpnetworkreply.cpp +++ b/src/network/access/qhttpnetworkreply.cpp @@ -427,7 +427,7 @@ int QHttpNetworkReplyPrivate::gunzipBodyPartially(QByteArray &compressed, QByteA { int ret = Z_DATA_ERROR; unsigned have; - unsigned char out[CHUNK]; + unsigned char out[QT_BUFFSIZE]; int pos = -1; if (!initInflate) { diff --git a/src/network/access/qhttpnetworkreply_p.h b/src/network/access/qhttpnetworkreply_p.h index f9b38f76b..25f456a2f 100644 --- a/src/network/access/qhttpnetworkreply_p.h +++ b/src/network/access/qhttpnetworkreply_p.h @@ -55,7 +55,6 @@ static const unsigned char gz_magic[2] = {0x1f, 0x8b}; // gzip magic header #define ORIG_NAME 0x08 // bit 3 set: original file name present #define COMMENT 0x10 // bit 4 set: file comment present #define RESERVED 0xE0 // bits 5..7: reserved -#define CHUNK 16384 #include // it's safe to include these even if SSL support is not enabled diff --git a/src/network/access/qnetworkaccessdebugpipebackend.cpp b/src/network/access/qnetworkaccessdebugpipebackend.cpp index 45ba32592..268d7b105 100644 --- a/src/network/access/qnetworkaccessdebugpipebackend.cpp +++ b/src/network/access/qnetworkaccessdebugpipebackend.cpp @@ -40,11 +40,6 @@ QT_BEGIN_NAMESPACE #ifdef QT_BUILD_INTERNAL -enum { - ReadBufferSize = 16384, - WriteBufferSize = ReadBufferSize -}; - QNetworkAccessBackend * QNetworkAccessDebugPipeBackendFactory::create(QNetworkAccessManager::Operation op, const QNetworkRequest &request) const @@ -81,7 +76,7 @@ QNetworkAccessDebugPipeBackend::~QNetworkAccessDebugPipeBackend() void QNetworkAccessDebugPipeBackend::open() { socket.connectToHost(url().host(), url().port(12345)); - socket.setReadBufferSize(ReadBufferSize); + socket.setReadBufferSize(QT_BUFFSIZE); // socket ready read -> we can push from socket to downstream connect(&socket, SIGNAL(readyRead()), SLOT(socketReadyRead())); @@ -132,8 +127,8 @@ void QNetworkAccessDebugPipeBackend::pushFromSocketToDownstream() if (hasDownloadFinished) return; - buffer.resize(ReadBufferSize); - qint64 haveRead = socket.read(buffer.data(), ReadBufferSize); + buffer.resize(QT_BUFFSIZE); + qint64 haveRead = socket.read(buffer.data(), QT_BUFFSIZE); if (haveRead == -1) { hasDownloadFinished = true; @@ -164,11 +159,11 @@ void QNetworkAccessDebugPipeBackend::pushFromUpstreamToSocket() return; forever { - if (socket.bytesToWrite() >= WriteBufferSize) + if (socket.bytesToWrite() >= QT_BUFFSIZE) return; qint64 haveRead; - const char *readPointer = uploadByteDevice->readPointer(WriteBufferSize, haveRead); + const char *readPointer = uploadByteDevice->readPointer(QT_BUFFSIZE, haveRead); if (haveRead == -1) { // EOF hasUploadFinished = true; diff --git a/src/svg/qsvgtinydocument.cpp b/src/svg/qsvgtinydocument.cpp index d341a48d2..2dc22e8f3 100644 --- a/src/svg/qsvgtinydocument.cpp +++ b/src/svg/qsvgtinydocument.cpp @@ -37,7 +37,7 @@ #include "qsvghandler_p.h" #include "qsvgfont_p.h" - +#include "qplatformdefs.h" #include "qpainter.h" #include "qfile.h" #include "qbuffer.h" @@ -74,7 +74,6 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device) Q_ASSERT(device->isOpen() && device->isReadable()); - static const int CHUNK_SIZE = 4096; int zlibResult = Z_OK; QByteArray source; @@ -100,7 +99,7 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device) while (stillMoreWorkToDo) { if (!zlibStream.avail_in) { - source = device->read(CHUNK_SIZE); + source = device->read(QT_BUFFSIZE); if (source.isEmpty()) break; @@ -112,10 +111,10 @@ static QByteArray qt_inflateGZipDataFrom(QIODevice *device) do { // Prepare the destination buffer int oldSize = destination.size(); - destination.resize(oldSize + CHUNK_SIZE); + destination.resize(oldSize + QT_BUFFSIZE); zlibStream.next_out = reinterpret_cast( destination.data() + oldSize - zlibStream.avail_out); - zlibStream.avail_out += CHUNK_SIZE; + zlibStream.avail_out += QT_BUFFSIZE; zlibResult = inflate(&zlibStream, Z_NO_FLUSH); switch (zlibResult) { diff --git a/tests/benchmarks/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/benchmarks/network/socket/qtcpserver/tst_qtcpserver.cpp index b6cb35d60..82009277d 100644 --- a/tests/benchmarks/network/socket/qtcpserver/tst_qtcpserver.cpp +++ b/tests/benchmarks/network/socket/qtcpserver/tst_qtcpserver.cpp @@ -31,12 +31,7 @@ ** ****************************************************************************/ -// Just to get Q_OS_SYMBIAN -#include - #include - - #include #include #include @@ -45,12 +40,11 @@ #include #include #include - #include #include #include - #include + Q_DECLARE_METATYPE(QNetworkProxy) Q_DECLARE_METATYPE(QList) @@ -157,7 +151,7 @@ void tst_QTcpServer::ipv4LoopbackPerformanceTest() QTcpSocket *clientB = server.nextPendingConnection(); QVERIFY(clientB); - QByteArray buffer(16384, '@'); + QByteArray buffer(QT_BUFFSIZE, '@'); QTime stopWatch; stopWatch.start(); qlonglong totalWritten = 0; @@ -166,7 +160,7 @@ void tst_QTcpServer::ipv4LoopbackPerformanceTest() clientA.flush(); totalWritten += buffer.size(); while (clientB->waitForReadyRead(100)) { - if (clientB->bytesAvailable() == 16384) + if (clientB->bytesAvailable() == QT_BUFFSIZE) break; } clientB->read(buffer.data(), buffer.size()); @@ -174,7 +168,7 @@ void tst_QTcpServer::ipv4LoopbackPerformanceTest() clientB->flush(); totalWritten += buffer.size(); while (clientA.waitForReadyRead(100)) { - if (clientA.bytesAvailable() == 16384) + if (clientA.bytesAvailable() == QT_BUFFSIZE) break; } clientA.read(buffer.data(), buffer.size()); @@ -211,7 +205,7 @@ void tst_QTcpServer::ipv6LoopbackPerformanceTest() QTcpSocket *clientB = server.nextPendingConnection(); QVERIFY(clientB); - QByteArray buffer(16384, '@'); + QByteArray buffer(QT_BUFFSIZE, '@'); QTime stopWatch; stopWatch.start(); qlonglong totalWritten = 0; @@ -220,7 +214,7 @@ void tst_QTcpServer::ipv6LoopbackPerformanceTest() clientA.flush(); totalWritten += buffer.size(); while (clientB->waitForReadyRead(100)) { - if (clientB->bytesAvailable() == 16384) + if (clientB->bytesAvailable() == QT_BUFFSIZE) break; } clientB->read(buffer.data(), buffer.size()); @@ -228,7 +222,7 @@ void tst_QTcpServer::ipv6LoopbackPerformanceTest() clientB->flush(); totalWritten += buffer.size(); while (clientA.waitForReadyRead(100)) { - if (clientA.bytesAvailable() == 16384) + if (clientA.bytesAvailable() == QT_BUFFSIZE) break; } clientA.read(buffer.data(), buffer.size()); @@ -266,7 +260,7 @@ void tst_QTcpServer::ipv4PerformanceTest() QTcpSocket *clientB = server.nextPendingConnection(); QVERIFY(clientB); - QByteArray buffer(16384, '@'); + QByteArray buffer(QT_BUFFSIZE, '@'); QTime stopWatch; stopWatch.start(); qlonglong totalWritten = 0; diff --git a/util/normalize/main.cpp b/util/normalize/main.cpp index 795fe2059..0ccb47502 100644 --- a/util/normalize/main.cpp +++ b/util/normalize/main.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -113,7 +114,7 @@ void check(const QString &fileName) QStringList lines; bool found = false; while (true) { - QByteArray bline = file.readLine(16384); + QByteArray bline = file.readLine(QT_BUFFSIZE); if (bline.isEmpty()) break; QString line = QString::fromLocal8Bit(bline);