From d013aa16ef4255d01a6704102e00555fc7167b03 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 5 Feb 2020 15:11:54 +0100 Subject: Extend QContiguousCache to use qsizetype for size and indices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow for more than 2^31 items and large offsets. Change-Id: I42f7bf20ce0e4af43dbb2e2083abf0e232e68282 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qcontiguouscache.cpp | 4 +- src/corelib/tools/qcontiguouscache.h | 72 +++++++++++----------- .../qcontiguouscache/tst_qcontiguouscache.cpp | 26 ++++---- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/corelib/tools/qcontiguouscache.cpp b/src/corelib/tools/qcontiguouscache.cpp index 64cbd7df4b..cc3510ef88 100644 --- a/src/corelib/tools/qcontiguouscache.cpp +++ b/src/corelib/tools/qcontiguouscache.cpp @@ -54,9 +54,9 @@ void QContiguousCacheData::dump() const } #endif -QContiguousCacheData *QContiguousCacheData::allocateData(int size, int alignment) +QContiguousCacheData *QContiguousCacheData::allocateData(qsizetype size, qsizetype alignment) { - return static_cast(qMallocAligned(size, alignment)); + return static_cast(qMallocAligned(size_t(size), size_t(alignment))); } void QContiguousCacheData::freeData(QContiguousCacheData *data) diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h index eb3194c752..df76c9fb50 100644 --- a/src/corelib/tools/qcontiguouscache.h +++ b/src/corelib/tools/qcontiguouscache.h @@ -52,12 +52,12 @@ QT_BEGIN_NAMESPACE struct Q_CORE_EXPORT QContiguousCacheData { QBasicAtomicInt ref; - int alloc; - int count; - int start; - int offset; + qsizetype alloc; + qsizetype count; + qsizetype start; + qsizetype offset; - static QContiguousCacheData *allocateData(int size, int alignment); + static QContiguousCacheData *allocateData(qsizetype size, qsizetype alignment); static void freeData(QContiguousCacheData *data); #ifdef QT_QCONTIGUOUSCACHE_DEBUG @@ -83,9 +83,9 @@ public: typedef value_type& reference; typedef const value_type& const_reference; typedef qptrdiff difference_type; - typedef int size_type; + typedef qsizetype size_type; - explicit QContiguousCache(int capacity = 0); + explicit QContiguousCache(qsizetype capacity = 0); QContiguousCache(const QContiguousCache &v) : d(v.d) { d->ref.ref(); } inline ~QContiguousCache() { if (!d) return; if (!d->ref.deref()) freeData(d); } @@ -100,32 +100,32 @@ public: bool operator==(const QContiguousCache &other) const; inline bool operator!=(const QContiguousCache &other) const { return !(*this == other); } - inline int capacity() const {return d->alloc; } - inline int count() const { return d->count; } - inline int size() const { return d->count; } + inline qsizetype capacity() const {return d->alloc; } + inline qsizetype count() const { return d->count; } + inline qsizetype size() const { return d->count; } inline bool isEmpty() const { return d->count == 0; } inline bool isFull() const { return d->count == d->alloc; } - inline int available() const { return d->alloc - d->count; } + inline qsizetype available() const { return d->alloc - d->count; } void clear(); - void setCapacity(int size); + void setCapacity(qsizetype size); - const T &at(int pos) const; - T &operator[](int i); - const T &operator[](int i) const; + const T &at(qsizetype pos) const; + T &operator[](qsizetype i); + const T &operator[](qsizetype i) const; void append(T &&value); void append(const T &value); void prepend(T &&value); void prepend(const T &value); - void insert(int pos, T &&value); - void insert(int pos, const T &value); + void insert(qsizetype pos, T &&value); + void insert(qsizetype pos, const T &value); - inline bool containsIndex(int pos) const { return pos >= d->offset && pos - d->offset < d->count; } - inline int firstIndex() const { return d->offset; } - inline int lastIndex() const { return d->offset + d->count - 1; } + inline bool containsIndex(qsizetype pos) const { return pos >= d->offset && pos - d->offset < d->count; } + inline qsizetype firstIndex() const { return d->offset; } + inline qsizetype lastIndex() const { return d->offset + d->count - 1; } inline const T &first() const { Q_ASSERT(!isEmpty()); return d->array[d->start]; } inline const T &last() const { Q_ASSERT(!isEmpty()); return d->array[(d->start + d->count -1) % d->alloc]; } @@ -138,7 +138,7 @@ public: T takeLast(); inline bool areIndexesValid() const - { return d->offset >= 0 && d->offset < INT_MAX - d->count && (d->offset % d->alloc) == d->start; } + { return d->offset >= 0 && d->offset < std::numeric_limits::max() - d->count && (d->offset % d->alloc) == d->start; } inline void normalizeIndexes() { d->offset = d->start; } @@ -148,7 +148,7 @@ public: private: void detach_helper(); - Data *allocateData(int aalloc); + Data *allocateData(qsizetype aalloc); void freeData(Data *x); }; @@ -164,7 +164,7 @@ void QContiguousCache::detach_helper() T *dest = x->array + x->start; T *src = d->array + d->start; - int oldcount = x->count; + qsizetype oldcount = x->count; while (oldcount--) { new (dest) T(*src); dest++; @@ -181,7 +181,7 @@ void QContiguousCache::detach_helper() } template -void QContiguousCache::setCapacity(int asize) +void QContiguousCache::setCapacity(qsizetype asize) { Q_ASSERT(asize >= 0); if (asize == d->alloc) @@ -197,7 +197,7 @@ void QContiguousCache::setCapacity(int asize) else x->start = 0; - int oldcount = x->count; + qsizetype oldcount = x->count; if(oldcount) { T *dest = x->array + (x->start + x->count-1) % x->alloc; @@ -222,7 +222,7 @@ void QContiguousCache::clear() { if (d->ref.loadRelaxed() == 1) { if (QTypeInfo::isComplex) { - int oldcount = d->count; + qsizetype oldcount = d->count; T * i = d->array + d->start; T * e = d->array + d->alloc; while (oldcount--) { @@ -245,13 +245,13 @@ void QContiguousCache::clear() } template -inline typename QContiguousCache::Data *QContiguousCache::allocateData(int aalloc) +inline typename QContiguousCache::Data *QContiguousCache::allocateData(qsizetype aalloc) { return static_cast(QContiguousCacheData::allocateData(sizeof(Data) + (aalloc - 1) * sizeof(T), alignof(Data))); } template -QContiguousCache::QContiguousCache(int cap) +QContiguousCache::QContiguousCache(qsizetype cap) { Q_ASSERT(cap >= 0); d = allocateData(cap); @@ -280,7 +280,7 @@ bool QContiguousCache::operator==(const QContiguousCache &other) const || other.d->offset != d->offset || other.d->alloc != d->alloc) return false; - for (int i = firstIndex(); i <= lastIndex(); ++i) + for (qsizetype i = firstIndex(); i <= lastIndex(); ++i) if (!(at(i) == other.at(i))) return false; return true; @@ -290,7 +290,7 @@ template void QContiguousCache::freeData(Data *x) { if (QTypeInfo::isComplex) { - int oldcount = d->count; + qsizetype oldcount = d->count; T * i = d->array + d->start; T * e = d->array + d->alloc; while (oldcount--) { @@ -383,9 +383,9 @@ void QContiguousCache::prepend(const T &value) } template -void QContiguousCache::insert(int pos, T &&value) +void QContiguousCache::insert(qsizetype pos, T &&value) { - Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache::insert", "index out of range"); + Q_ASSERT_X(pos >= 0, "QContiguousCache::insert", "index out of range"); if (!d->alloc) return; // zero capacity detach(); @@ -406,19 +406,19 @@ void QContiguousCache::insert(int pos, T &&value) } template -void QContiguousCache::insert(int pos, const T &value) +void QContiguousCache::insert(qsizetype pos, const T &value) { return insert(pos, T(value)); } template -inline const T &QContiguousCache::at(int pos) const +inline const T &QContiguousCache::at(qsizetype pos) const { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache::at", "index out of range"); return d->array[pos % d->alloc]; } template -inline const T &QContiguousCache::operator[](int pos) const +inline const T &QContiguousCache::operator[](qsizetype pos) const { return at(pos); } template -inline T &QContiguousCache::operator[](int pos) +inline T &QContiguousCache::operator[](qsizetype pos) { detach(); if (!containsIndex(pos)) diff --git a/tests/auto/corelib/tools/qcontiguouscache/tst_qcontiguouscache.cpp b/tests/auto/corelib/tools/qcontiguouscache/tst_qcontiguouscache.cpp index f305d63d46..b25ed55648 100644 --- a/tests/auto/corelib/tools/qcontiguouscache/tst_qcontiguouscache.cpp +++ b/tests/auto/corelib/tools/qcontiguouscache/tst_qcontiguouscache.cpp @@ -106,25 +106,25 @@ void tst_QContiguousCache::swap() void tst_QContiguousCache::append_data() { - QTest::addColumn("start"); - QTest::addColumn("count"); - QTest::addColumn("cacheSize"); + QTest::addColumn("start"); + QTest::addColumn("count"); + QTest::addColumn("cacheSize"); QTest::addColumn("invalidIndexes"); - QTest::newRow("0+30[10]") << 0 << 30 << 10 << false; - QTest::newRow("300+30[10]") << 300 << 30 << 10 << false; - QTest::newRow("MAX-10+30[10]") << INT_MAX-10 << 30 << 10 << true; + QTest::newRow("0+30[10]") << qsizetype(0) << qsizetype(30) << qsizetype(10) << false; + QTest::newRow("300+30[10]") << qsizetype(300) << qsizetype(30) << qsizetype(10) << false; + QTest::newRow("MAX-10+30[10]") << std::numeric_limits::max()-10 << qsizetype(30) << qsizetype(10) << true; } void tst_QContiguousCache::append() { - QFETCH(int, start); - QFETCH(int, count); - QFETCH(int, cacheSize); + QFETCH(qsizetype, start); + QFETCH(qsizetype, count); + QFETCH(qsizetype, cacheSize); QFETCH(bool, invalidIndexes); - int i, j; - QContiguousCache c(cacheSize); + qsizetype i, j; + QContiguousCache c(cacheSize); i = 1; QCOMPARE(c.available(), cacheSize); @@ -134,8 +134,8 @@ void tst_QContiguousCache::append() c.insert(start, i++); while (i < count) { c.append(i); - QCOMPARE(c.available(), qMax(0, cacheSize - i)); - QCOMPARE(c.first(), qMax(1, i-cacheSize+1)); + QCOMPARE(c.available(), qMax(qsizetype(0), cacheSize - i)); + QCOMPARE(c.first(), qMax(qsizetype(1), i-cacheSize+1)); QCOMPARE(c.last(), i); QCOMPARE(c.count(), qMin(i, cacheSize)); QCOMPARE(c.isFull(), i >= cacheSize); -- cgit v1.2.3