Adapt QFileDevice to use QIODevice's write buffer

Also, bump the TypeInformationVersion field in qtHookData, to notify
the Qt Creator developers that the offset of QFilePrivate::fileName
was changed and dumpers should be adapted.

Change-Id: I71bc5f509b733c0ab3430cd47ff08961f0388839
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
Alex Trotsenko 2015-09-12 21:29:13 +03:00
parent f416561702
commit 8f92baf5c9
4 changed files with 17 additions and 20 deletions

View File

@ -67,7 +67,7 @@ quintptr Q_CORE_EXPORT qtHookData[] = {
// The required sizes and offsets are tested in tests/auto/other/toolsupport. // The required sizes and offsets are tested in tests/auto/other/toolsupport.
// When this fails and the change was intentional, adjust the test and // When this fails and the change was intentional, adjust the test and
// adjust this value here. // adjust this value here.
3 4
}; };
Q_STATIC_ASSERT(QHooks::LastHookIndex == sizeof(qtHookData) / sizeof(qtHookData[0])); Q_STATIC_ASSERT(QHooks::LastHookIndex == sizeof(qtHookData) / sizeof(qtHookData[0]));

View File

@ -48,13 +48,16 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
static const int QFILE_WRITEBUFFER_SIZE = 16384; #ifndef QFILE_WRITEBUFFER_SIZE
#define QFILE_WRITEBUFFER_SIZE 16384
#endif
QFileDevicePrivate::QFileDevicePrivate() QFileDevicePrivate::QFileDevicePrivate()
: fileEngine(0), : fileEngine(0),
writeBuffer(QFILE_WRITEBUFFER_SIZE), cachedSize(0), cachedSize(0),
error(QFile::NoError), lastWasWrite(false) error(QFile::NoError), lastWasWrite(false)
{ {
writeBufferChunkSize = QFILE_WRITEBUFFER_SIZE;
} }
QFileDevicePrivate::~QFileDevicePrivate() QFileDevicePrivate::~QFileDevicePrivate()
@ -274,14 +277,6 @@ QString QFileDevice::fileName() const
return QString(); return QString();
} }
static inline qint64 _qfile_writeData(QAbstractFileEngine *engine, QRingBuffer *buffer)
{
qint64 ret = engine->write(buffer->readPointer(), buffer->nextDataBlockSize());
if (ret > 0)
buffer->free(ret);
return ret;
}
/*! /*!
Flushes any buffered data to the file. Returns \c true if successful; Flushes any buffered data to the file. Returns \c true if successful;
otherwise returns \c false. otherwise returns \c false.
@ -295,8 +290,11 @@ bool QFileDevice::flush()
} }
if (!d->writeBuffer.isEmpty()) { if (!d->writeBuffer.isEmpty()) {
qint64 size = d->writeBuffer.size(); qint64 size = d->writeBuffer.nextDataBlockSize();
if (_qfile_writeData(d->fileEngine, &d->writeBuffer) != size) { qint64 written = d->fileEngine->write(d->writeBuffer.readPointer(), size);
if (written > 0)
d->writeBuffer.free(written);
if (written != size) {
QFileDevice::FileError err = d->fileEngine->error(); QFileDevice::FileError err = d->fileEngine->error();
if (err == QFileDevice::UnspecifiedError) if (err == QFileDevice::UnspecifiedError)
err = QFileDevice::WriteError; err = QFileDevice::WriteError;
@ -488,9 +486,10 @@ bool QFileDevicePrivate::putCharHelper(char c)
// Cutoff for code that doesn't only touch the buffer. // Cutoff for code that doesn't only touch the buffer.
qint64 writeBufferSize = writeBuffer.size(); qint64 writeBufferSize = writeBuffer.size();
if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= QFILE_WRITEBUFFER_SIZE if ((openMode & QIODevice::Unbuffered) || writeBufferSize + 1 >= writeBufferChunkSize
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
|| ((openMode & QIODevice::Text) && c == '\n' && writeBufferSize + 2 >= QFILE_WRITEBUFFER_SIZE) || ((openMode & QIODevice::Text) && c == '\n'
&& writeBufferSize + 2 >= writeBufferChunkSize)
#endif #endif
) { ) {
return QIODevicePrivate::putCharHelper(c); return QIODevicePrivate::putCharHelper(c);
@ -544,14 +543,14 @@ qint64 QFileDevice::writeData(const char *data, qint64 len)
bool buffered = !(d->openMode & Unbuffered); bool buffered = !(d->openMode & Unbuffered);
// Flush buffered data if this read will overflow. // Flush buffered data if this read will overflow.
if (buffered && (d->writeBuffer.size() + len) > QFILE_WRITEBUFFER_SIZE) { if (buffered && (d->writeBuffer.size() + len) > d->writeBufferChunkSize) {
if (!flush()) if (!flush())
return -1; return -1;
} }
// Write directly to the engine if the block size is larger than // Write directly to the engine if the block size is larger than
// the write buffer size. // the write buffer size.
if (!buffered || len > QFILE_WRITEBUFFER_SIZE) { if (!buffered || len > d->writeBufferChunkSize) {
const qint64 ret = d->fileEngine->write(data, len); const qint64 ret = d->fileEngine->write(data, len);
if (ret < 0) { if (ret < 0) {
QFileDevice::FileError err = d->fileEngine->error(); QFileDevice::FileError err = d->fileEngine->error();

View File

@ -52,7 +52,6 @@
// //
#include "private/qiodevice_p.h" #include "private/qiodevice_p.h"
#include "private/qringbuffer_p.h"
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -77,7 +76,6 @@ protected:
void setError(QFileDevice::FileError err, int errNum); void setError(QFileDevice::FileError err, int errNum);
mutable QAbstractFileEngine *fileEngine; mutable QAbstractFileEngine *fileEngine;
QRingBuffer writeBuffer;
mutable qint64 cachedSize; mutable qint64 cachedSize;
QFileDevice::FileHandleFlags handleFlags; QFileDevice::FileHandleFlags handleFlags;

View File

@ -124,7 +124,7 @@ void tst_toolsupport::offsets_data()
{ {
QTestData &data = QTest::newRow("QFilePrivate::fileName") QTestData &data = QTest::newRow("QFilePrivate::fileName")
<< pmm_to_offsetof(&QFilePrivate::fileName); << pmm_to_offsetof(&QFilePrivate::fileName);
data << 196 << 280; data << 168 << 248;
} }
#endif #endif