QRingBuffer: allow to change the chunk size of the buffer dynamically

Change-Id: I0ac55713c7bb8c48d2c9c774376543caef781980
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Alex Trotsenko 2016-07-13 15:39:38 +03:00
parent b91f86a212
commit 3605fc653b
3 changed files with 16 additions and 0 deletions

View File

@ -92,6 +92,8 @@ public:
friend class QIODevicePrivate;
public:
// wrap functions from QRingBuffer
inline void setChunkSize(int size) { Q_ASSERT(m_buf); m_buf->setChunkSize(size); }
inline int chunkSize() const { Q_ASSERT(m_buf); return m_buf->chunkSize(); }
inline qint64 nextDataBlockSize() const { return (m_buf ? m_buf->nextDataBlockSize() : Q_INT64_C(0)); }
inline const char *readPointer() const { return (m_buf ? m_buf->readPointer() : Q_NULLPTR); }
inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const { Q_ASSERT(m_buf); return m_buf->readPointerAtPosition(pos, length); }

View File

@ -67,6 +67,14 @@ public:
explicit inline QRingBuffer(int growth = QRINGBUFFER_CHUNKSIZE) :
head(0), tail(0), tailBuffer(0), basicBlockSize(growth), bufferSize(0) { }
inline void setChunkSize(int size) {
basicBlockSize = size;
}
inline int chunkSize() const {
return basicBlockSize;
}
inline qint64 nextDataBlockSize() const {
return (tailBuffer == 0 ? tail : buffers.first().size()) - head;
}

View File

@ -58,6 +58,12 @@ void tst_QRingBuffer::constructing()
{
QRingBuffer ringBuffer;
const int chunkSize = ringBuffer.chunkSize();
ringBuffer.setChunkSize(0);
QCOMPARE(ringBuffer.chunkSize(), Q_INT64_C(0));
ringBuffer.setChunkSize(chunkSize);
QCOMPARE(ringBuffer.chunkSize(), chunkSize);
QCOMPARE(ringBuffer.size(), Q_INT64_C(0));
QVERIFY(ringBuffer.isEmpty());
QCOMPARE(ringBuffer.nextDataBlockSize(), Q_INT64_C(0));