From 8fb45ae5b8b8ad276aeb9bc9e40f351f47523087 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 4 Jun 2012 22:04:13 +0200 Subject: Introduce QArrayData::allocatedCapacity() and use it instead of d->alloc In almost all cases, use d->allocatedCapacity() or d->constAllocatedCapacity() instead of d->alloc, since they do the same thing (right now). In the future, the functions will be changed. There is a separate const version because most const code should not need to know the allocation size -- only mutating code should need to know that There are a few cases where d->alloc was replaced with a better alternative, like d->size. The one case that remains in the code will be replaced by a different test when it's available. Change-Id: I48135469db4caf150f82df93fff42d2309b23719 Reviewed-by: Simon Hausmann --- src/corelib/tools/qvector.h | 53 +++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 26 deletions(-) (limited to 'src/corelib/tools/qvector.h') diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index d64673cc16..2be7fa01e0 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -110,7 +110,7 @@ public: void resize(int size); - inline int capacity() const { return int(d->alloc); } + inline int capacity() const { return d->constAllocatedCapacity(); } void reserve(int size); inline void squeeze() { @@ -303,7 +303,7 @@ public: private: // ### Qt6: remove methods, they are unused void reallocData(const int size, const int alloc, QArrayData::ArrayOptions options = QArrayData::DefaultAllocationFlags); - void reallocData(const int sz) { reallocData(sz, d->alloc); } + void reallocData(const int sz) { reallocData(sz, d->allocatedCapacity()); } void realloc(int alloc, QArrayData::ArrayOptions options = QArrayData::DefaultAllocationFlags); void freeData(Data *d); void defaultConstruct(T *from, T *to); @@ -372,14 +372,14 @@ inline QVector::QVector(const QVector &v) d = v.d; } else { if (v.d->flags & Data::CapacityReserved) { - d = Data::allocate(v.d->alloc); + d = Data::allocate(v.d->allocatedCapacity()); Q_CHECK_PTR(d); d->flags |= Data::CapacityReserved; } else { d = Data::allocate(v.d->size); Q_CHECK_PTR(d); } - if (d->alloc) { + if (v.d->size) { copyConstruct(v.d->begin(), v.d->end(), d->begin()); d->size = v.d->size; } @@ -397,18 +397,18 @@ void QVector::detach() return; if (!isDetached()) - realloc(int(d->alloc)); + realloc(d->allocatedCapacity()); Q_ASSERT(isDetached()); } template void QVector::reserve(int asize) { - if (asize > int(d->alloc)) - realloc(asize); - if (isDetached()) + if (asize > int(d->allocatedCapacity())) + realloc(asize, typename Data::ArrayOptions(d->flags | Data::CapacityReserved)); + else if (isDetached()) d->flags |= Data::CapacityReserved; - Q_ASSERT(capacity() >= asize); + Q_ASSERT(int(d->allocatedCapacity()) >= asize); } template @@ -416,9 +416,10 @@ void QVector::resize(int asize) { if (asize == d->size) return detach(); - if (asize > int(d->alloc) || !isDetached()) { // there is not enough space - QArrayData::ArrayOptions opt = asize > int(d->alloc) ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags; - realloc(qMax(int(d->alloc), asize), opt); + int oldAlloc = d->allocatedCapacity(); + if (asize > oldAlloc || !isDetached()) { // there is not enough space + QArrayData::ArrayOptions opt = asize > oldAlloc ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags; + realloc(qMax(oldAlloc, asize), opt); } if (asize < d->size) destruct(begin() + asize, end()); @@ -583,7 +584,7 @@ void QVector::reallocData(const int asize, const int aalloc, QArrayData::Arra const bool isShared = d->ref.isShared(); if (aalloc != 0) { - if (aalloc != int(d->alloc) || isShared) { + if (aalloc != int(d->allocatedCapacity()) || isShared) { QT_TRY { // allocate memory x = Data::allocate(aalloc, options); @@ -640,7 +641,7 @@ void QVector::reallocData(const int asize, const int aalloc, QArrayData::Arra QT_RETHROW; } } else { - Q_ASSERT(int(d->alloc) == aalloc); // resize, without changing allocation size + Q_ASSERT(int(d->allocatedCapacity()) == aalloc); // resize, without changing allocation size Q_ASSERT(isDetached()); // can be done only on detached d Q_ASSERT(x == d); // in this case we do not need to allocate anything if (asize <= d->size) { @@ -667,9 +668,9 @@ void QVector::reallocData(const int asize, const int aalloc, QArrayData::Arra } Q_ASSERT(d->data()); - Q_ASSERT(uint(d->size) <= d->alloc); + Q_ASSERT(d->size <= int(d->allocatedCapacity())); Q_ASSERT(aalloc ? d != Data::sharedNull() : d == Data::sharedNull()); - Q_ASSERT(d->alloc >= uint(aalloc)); + Q_ASSERT(int(d->allocatedCapacity()) >= aalloc); Q_ASSERT(d->size == asize); } @@ -757,11 +758,11 @@ Q_OUTOFLINE_TEMPLATE T QVector::value(int i, const T &defaultValue) const template void QVector::append(const T &t) { - const bool isTooSmall = uint(d->size + 1) > d->alloc; + const bool isTooSmall = d->size >= int(d->allocatedCapacity()); if (!isDetached() || isTooSmall) { T copy(t); QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); - realloc(isTooSmall ? d->size + 1 : d->alloc, opt); + realloc(isTooSmall ? d->size + 1 : d->allocatedCapacity(), opt); if (QTypeInfo::isComplex) new (d->end()) T(std::move(copy)); @@ -780,10 +781,10 @@ void QVector::append(const T &t) template void QVector::append(T &&t) { - const bool isTooSmall = uint(d->size + 1) > d->alloc; + const bool isTooSmall = uint(d->size + 1) > d->allocatedCapacity(); if (!isDetached() || isTooSmall) { QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); - realloc(isTooSmall ? d->size + 1 : d->alloc, opt); + realloc(isTooSmall ? d->size + 1 : d->allocatedCapacity(), opt); } new (d->end()) T(std::move(t)); @@ -795,7 +796,7 @@ template void QVector::removeLast() { Q_ASSERT(!isEmpty()); - Q_ASSERT(d->alloc); + Q_ASSERT(d->allocatedCapacity()); if (d->ref.isShared()) detach(); @@ -812,7 +813,7 @@ typename QVector::iterator QVector::insert(iterator before, size_type n, c const auto offset = std::distance(d->begin(), before); if (n != 0) { const T copy(t); - if (!isDetached() || d->size + n > int(d->alloc)) + if (!isDetached() || d->size + n > int(d->allocatedCapacity())) realloc(d->size + n, QArrayData::GrowsForward); if (!QTypeInfoQuery::isRelocatable) { T *b = d->end(); @@ -889,7 +890,7 @@ typename QVector::iterator QVector::erase(iterator abegin, iterator aend) // FIXME we could do a proper realloc, which copy constructs only needed data. // FIXME we are about to delete data - maybe it is good time to shrink? // FIXME the shrink is also an issue in removeLast, that is just a copy + reduce of this. - if (d->alloc) { + if (d->allocatedCapacity()) { detach(); abegin = d->begin() + itemsUntouched; aend = abegin + itemsToErase; @@ -952,13 +953,13 @@ QVector &QVector::operator+=(const QVector &l) *this = l; } else { uint newSize = d->size + l.d->size; - const bool isTooSmall = newSize > d->alloc; + const bool isTooSmall = newSize > d->allocatedCapacity(); if (!isDetached() || isTooSmall) { QArrayData::ArrayOptions opt(isTooSmall ? QArrayData::GrowsForward : QArrayData::DefaultAllocationFlags); - realloc(isTooSmall ? newSize : d->alloc, opt); + realloc(isTooSmall ? newSize : d->allocatedCapacity(), opt); } - if (d->alloc) { + if (l.d->size) { T *w = d->begin() + newSize; T *i = l.d->end(); T *b = l.d->begin(); -- cgit v1.2.3