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 <simon.hausmann@qt.io>bb10
parent
64db4861bf
commit
8fb45ae5b8
|
|
@ -159,7 +159,7 @@ static inline const QByteArray stringData(const QMetaObject *mo, int index)
|
|||
Q_ASSERT(priv(mo->d.data)->revision >= 7);
|
||||
const QByteArrayDataPtr data = { const_cast<QByteArrayData*>(&mo->d.stringdata[index]) };
|
||||
Q_ASSERT(data.ptr->ref.isStatic());
|
||||
Q_ASSERT(data.ptr->alloc == 0);
|
||||
Q_ASSERT(data.ptr->allocatedCapacity() == 0);
|
||||
Q_ASSERT(data.ptr->size >= 0);
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1207,9 +1207,9 @@ QByteArray &QByteArray::operator=(const char *str)
|
|||
x = Data::allocate(0);
|
||||
} else {
|
||||
const int len = int(strlen(str));
|
||||
const uint fullLen = len + 1;
|
||||
if (d->ref.isShared() || fullLen > d->alloc
|
||||
|| (len < d->size && fullLen < uint(d->alloc >> 1)))
|
||||
const int fullLen = len + 1;
|
||||
if (d->ref.isShared() || fullLen > int(d->allocatedCapacity())
|
||||
|| (len < d->size && fullLen < int(d->allocatedCapacity() >> 1)))
|
||||
reallocData(fullLen, d->detachFlags());
|
||||
x = d;
|
||||
memcpy(x->data(), str, fullLen); // include null terminator
|
||||
|
|
@ -1775,9 +1775,9 @@ void QByteArray::resize(int size)
|
|||
x->data()[size] = '\0';
|
||||
d = x;
|
||||
} else {
|
||||
if (d->ref.isShared() || uint(size) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || size > capacity())
|
||||
reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward);
|
||||
if (d->alloc) {
|
||||
if (d->allocatedCapacity()) {
|
||||
d->size = size;
|
||||
d->data()[size] = '\0';
|
||||
}
|
||||
|
|
@ -1900,7 +1900,7 @@ QByteArray &QByteArray::prepend(const char *str)
|
|||
QByteArray &QByteArray::prepend(const char *str, int len)
|
||||
{
|
||||
if (str) {
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + len > capacity())
|
||||
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward);
|
||||
memmove(d->data()+len, d->data(), d->size);
|
||||
memcpy(d->data(), str, len);
|
||||
|
|
@ -1926,7 +1926,7 @@ QByteArray &QByteArray::prepend(const char *str, int len)
|
|||
|
||||
QByteArray &QByteArray::prepend(char ch)
|
||||
{
|
||||
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + 1 > capacity())
|
||||
reallocData(uint(d->size) + 2u, d->detachFlags() | Data::GrowsForward);
|
||||
memmove(d->data()+1, d->data(), d->size);
|
||||
d->data()[0] = ch;
|
||||
|
|
@ -1964,7 +1964,7 @@ QByteArray &QByteArray::append(const QByteArray &ba)
|
|||
if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
|
||||
*this = ba;
|
||||
} else if (ba.d->size != 0) {
|
||||
if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + ba.d->size > capacity())
|
||||
reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::GrowsForward);
|
||||
memcpy(d->data() + d->size, ba.d->data(), ba.d->size);
|
||||
d->size += ba.d->size;
|
||||
|
|
@ -1996,7 +1996,7 @@ QByteArray& QByteArray::append(const char *str)
|
|||
{
|
||||
if (str) {
|
||||
const int len = int(strlen(str));
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + len > capacity())
|
||||
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward);
|
||||
memcpy(d->data() + d->size, str, len + 1); // include null terminator
|
||||
d->size += len;
|
||||
|
|
@ -2021,7 +2021,7 @@ QByteArray &QByteArray::append(const char *str, int len)
|
|||
if (len < 0)
|
||||
len = qstrlen(str);
|
||||
if (str && len) {
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + len > capacity())
|
||||
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::GrowsForward);
|
||||
memcpy(d->data() + d->size, str, len); // include null terminator
|
||||
d->size += len;
|
||||
|
|
@ -2049,7 +2049,7 @@ QByteArray &QByteArray::append(const char *str, int len)
|
|||
|
||||
QByteArray& QByteArray::append(char ch)
|
||||
{
|
||||
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + 1 > capacity())
|
||||
reallocData(uint(d->size) + 2u, d->detachFlags() | Data::GrowsForward);
|
||||
d->data()[d->size++] = ch;
|
||||
d->data()[d->size] = '\0';
|
||||
|
|
@ -2576,7 +2576,7 @@ QByteArray QByteArray::repeated(int times) const
|
|||
|
||||
QByteArray result;
|
||||
result.reserve(resultSize);
|
||||
if (result.d->alloc != uint(resultSize) + 1u)
|
||||
if (result.capacity() != resultSize)
|
||||
return QByteArray(); // not enough memory
|
||||
|
||||
memcpy(result.d->data(), d->data(), d->size);
|
||||
|
|
@ -4469,7 +4469,7 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
|
|||
*/
|
||||
QByteArray &QByteArray::setRawData(const char *data, uint size)
|
||||
{
|
||||
if (d->ref.isShared() || d->alloc) {
|
||||
if (d->ref.isShared() || d->allocatedCapacity()) {
|
||||
*this = fromRawData(data, size);
|
||||
} else {
|
||||
if (data) {
|
||||
|
|
|
|||
|
|
@ -502,11 +502,11 @@ inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d)
|
|||
{ d->ref.ref(); }
|
||||
|
||||
inline int QByteArray::capacity() const
|
||||
{ return d->alloc ? d->alloc - 1 : 0; }
|
||||
{ int realCapacity = d->constAllocatedCapacity(); return realCapacity ? realCapacity - 1 : 0; }
|
||||
|
||||
inline void QByteArray::reserve(int asize)
|
||||
{
|
||||
if (d->ref.isShared() || uint(asize) + 1u > d->alloc) {
|
||||
if (d->ref.isShared() || asize > capacity()) {
|
||||
reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved);
|
||||
} else {
|
||||
d->flags |= Data::CapacityReserved;
|
||||
|
|
@ -515,7 +515,7 @@ inline void QByteArray::reserve(int asize)
|
|||
|
||||
inline void QByteArray::squeeze()
|
||||
{
|
||||
if (d->ref.isShared() || uint(d->size) + 1u < d->alloc) {
|
||||
if (d->ref.isShared() || d->size < capacity()) {
|
||||
reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved);
|
||||
} else {
|
||||
d->flags &= ~Data::CapacityReserved;
|
||||
|
|
|
|||
|
|
@ -2270,7 +2270,7 @@ void QString::resize(int size)
|
|||
return;
|
||||
}
|
||||
|
||||
if (d->ref.isShared() || uint(size) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || uint(size) + 1u > d->allocatedCapacity())
|
||||
reallocData(uint(size) + 1u, true);
|
||||
if (d->alloc) {
|
||||
d->size = size;
|
||||
|
|
@ -2592,7 +2592,7 @@ QString& QString::insert(int i, const QChar *unicode, int size)
|
|||
return *this;
|
||||
|
||||
const ushort *s = (const ushort *)unicode;
|
||||
if (s >= d->data() && s < d->data() + d->alloc) {
|
||||
if (s >= d->data() && s < d->data() + d->size) {
|
||||
// Part of me - take a copy
|
||||
ushort *tmp = static_cast<ushort *>(::malloc(size * sizeof(QChar)));
|
||||
Q_CHECK_PTR(tmp);
|
||||
|
|
@ -2658,7 +2658,7 @@ QString &QString::append(const QString &str)
|
|||
if (d == Data::sharedNull()) {
|
||||
operator=(str);
|
||||
} else {
|
||||
if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + str.d->size > capacity())
|
||||
reallocData(uint(d->size + str.d->size) + 1u, true);
|
||||
memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
|
||||
d->size += str.d->size;
|
||||
|
|
@ -2677,7 +2677,7 @@ QString &QString::append(const QString &str)
|
|||
QString &QString::append(const QChar *str, int len)
|
||||
{
|
||||
if (str && len > 0) {
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->allocatedCapacity())
|
||||
reallocData(uint(d->size + len) + 1u, true);
|
||||
memcpy(d->data() + d->size, str, len * sizeof(QChar));
|
||||
d->size += len;
|
||||
|
|
@ -2696,7 +2696,7 @@ QString &QString::append(QLatin1String str)
|
|||
const char *s = str.latin1();
|
||||
if (s) {
|
||||
int len = str.size();
|
||||
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + len > capacity())
|
||||
reallocData(uint(d->size + len) + 1u, true);
|
||||
ushort *i = d->data() + d->size;
|
||||
qt_from_latin1(i, s, uint(len));
|
||||
|
|
@ -2743,7 +2743,7 @@ QString &QString::append(QLatin1String str)
|
|||
*/
|
||||
QString &QString::append(QChar ch)
|
||||
{
|
||||
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + 1 > capacity())
|
||||
reallocData(uint(d->size) + 2u, true);
|
||||
d->data()[d->size++] = ch.unicode();
|
||||
d->data()[d->size] = '\0';
|
||||
|
|
@ -7954,7 +7954,7 @@ QString QString::repeated(int times) const
|
|||
|
||||
QString result;
|
||||
result.reserve(resultSize);
|
||||
if (result.d->alloc != uint(resultSize) + 1u)
|
||||
if (result.capacity() != resultSize)
|
||||
return QString(); // not enough memory
|
||||
|
||||
memcpy(result.d->data(), d->data(), d->size * sizeof(ushort));
|
||||
|
|
@ -9149,7 +9149,7 @@ QString QString::fromRawData(const QChar *unicode, int size)
|
|||
*/
|
||||
QString &QString::setRawData(const QChar *unicode, int size)
|
||||
{
|
||||
if (d->ref.isShared() || d->alloc) {
|
||||
if (d->ref.isShared() || d->allocatedCapacity()) {
|
||||
*this = fromRawData(unicode, size);
|
||||
} else {
|
||||
if (unicode) {
|
||||
|
|
|
|||
|
|
@ -274,7 +274,7 @@ public:
|
|||
void truncate(int pos);
|
||||
void chop(int n);
|
||||
|
||||
int capacity() const;
|
||||
inline int capacity() const;
|
||||
inline void reserve(int size);
|
||||
inline void squeeze();
|
||||
|
||||
|
|
@ -542,7 +542,7 @@ public:
|
|||
inline QString &prepend(QLatin1String s) { return insert(0, s); }
|
||||
|
||||
inline QString &operator+=(QChar c) {
|
||||
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
|
||||
if (d->ref.isShared() || d->size + 1 > capacity())
|
||||
reallocData(uint(d->size) + 2u, true);
|
||||
d->data()[d->size++] = c.unicode();
|
||||
d->data()[d->size] = '\0';
|
||||
|
|
@ -1049,7 +1049,7 @@ inline void QString::clear()
|
|||
inline QString::QString(const QString &other) noexcept : d(other.d)
|
||||
{ Q_ASSERT(&other != this); d->ref.ref(); }
|
||||
inline int QString::capacity() const
|
||||
{ return d->alloc ? d->alloc - 1 : 0; }
|
||||
{ int realCapacity = d->constAllocatedCapacity(); return realCapacity ? realCapacity - 1 : 0; }
|
||||
inline QString &QString::setNum(short n, int base)
|
||||
{ return setNum(qlonglong(n), base); }
|
||||
inline QString &QString::setNum(ushort n, int base)
|
||||
|
|
@ -1263,8 +1263,8 @@ inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
|
|||
|
||||
inline void QString::reserve(int asize)
|
||||
{
|
||||
if (d->ref.isShared() || uint(asize) >= d->alloc)
|
||||
reallocData(qMax(asize, d->size) + 1u);
|
||||
if (d->ref.isShared() || asize >= capacity())
|
||||
reallocData(qMax(asize, size()) + 1u);
|
||||
|
||||
// we're not shared anymore, for sure
|
||||
d->flags |= Data::CapacityReserved;
|
||||
|
|
@ -1272,7 +1272,7 @@ inline void QString::reserve(int asize)
|
|||
|
||||
inline void QString::squeeze()
|
||||
{
|
||||
if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
|
||||
if (d->ref.isShared() || d->size < capacity())
|
||||
reallocData(uint(d->size) + 1u);
|
||||
|
||||
// we're not shared anymore, for sure
|
||||
|
|
|
|||
|
|
@ -55,6 +55,16 @@ struct Q_CORE_EXPORT QArrayData
|
|||
|
||||
qptrdiff offset; // in bytes from beginning of header
|
||||
|
||||
inline size_t allocatedCapacity()
|
||||
{
|
||||
return alloc;
|
||||
}
|
||||
|
||||
inline size_t constAllocatedCapacity() const
|
||||
{
|
||||
return alloc;
|
||||
}
|
||||
|
||||
void *data()
|
||||
{
|
||||
Q_ASSERT(size == 0
|
||||
|
|
@ -90,8 +100,8 @@ struct Q_CORE_EXPORT QArrayData
|
|||
|
||||
size_t detachCapacity(size_t newSize) const
|
||||
{
|
||||
if (flags & CapacityReserved && newSize < alloc)
|
||||
return alloc;
|
||||
if (flags & CapacityReserved && newSize < constAllocatedCapacity())
|
||||
return constAllocatedCapacity();
|
||||
return newSize;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ struct QPodArrayOps
|
|||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(newSize > uint(this->size));
|
||||
Q_ASSERT(newSize <= this->alloc);
|
||||
Q_ASSERT(newSize <= this->allocatedCapacity());
|
||||
|
||||
::memset(static_cast<void *>(this->end()), 0, (newSize - this->size) * sizeof(T));
|
||||
this->size = int(newSize);
|
||||
|
|
@ -74,7 +74,7 @@ struct QPodArrayOps
|
|||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(b < e);
|
||||
Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
|
||||
|
||||
::memcpy(static_cast<void *>(this->end()), static_cast<const void *>(b),
|
||||
(e - b) * sizeof(T));
|
||||
|
|
@ -85,7 +85,7 @@ struct QPodArrayOps
|
|||
{
|
||||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(n <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(n <= uint(this->allocatedCapacity() - this->size));
|
||||
|
||||
T *iter = this->end();
|
||||
const T *const end = iter + n;
|
||||
|
|
@ -119,7 +119,7 @@ struct QPodArrayOps
|
|||
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
|
||||
Q_ASSERT(b < e);
|
||||
Q_ASSERT(e <= where || b > this->end()); // No overlap
|
||||
Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
|
||||
|
||||
::memmove(static_cast<void *>(where + (e - b)), static_cast<void *>(where),
|
||||
(static_cast<const T*>(this->end()) - where) * sizeof(T));
|
||||
|
|
@ -150,7 +150,7 @@ struct QGenericArrayOps
|
|||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(newSize > uint(this->size));
|
||||
Q_ASSERT(newSize <= this->alloc);
|
||||
Q_ASSERT(newSize <= this->allocatedCapacity());
|
||||
|
||||
T *const begin = this->begin();
|
||||
do {
|
||||
|
|
@ -163,7 +163,7 @@ struct QGenericArrayOps
|
|||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(b < e);
|
||||
Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
|
||||
|
||||
T *iter = this->end();
|
||||
for (; b != e; ++iter, ++b) {
|
||||
|
|
@ -176,7 +176,7 @@ struct QGenericArrayOps
|
|||
{
|
||||
Q_ASSERT(this->isMutable());
|
||||
Q_ASSERT(!this->ref.isShared());
|
||||
Q_ASSERT(n <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(n <= size_t(this->allocatedCapacity() - this->size));
|
||||
|
||||
T *iter = this->end();
|
||||
const T *const end = iter + n;
|
||||
|
|
@ -220,7 +220,7 @@ struct QGenericArrayOps
|
|||
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
|
||||
Q_ASSERT(b < e);
|
||||
Q_ASSERT(e <= where || b > this->end()); // No overlap
|
||||
Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
|
||||
|
||||
// Array may be truncated at where in case of exceptions
|
||||
|
||||
|
|
@ -316,7 +316,7 @@ struct QMovableArrayOps
|
|||
Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
|
||||
Q_ASSERT(b < e);
|
||||
Q_ASSERT(e <= where || b > this->end()); // No overlap
|
||||
Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
|
||||
Q_ASSERT(e - b <= this->allocatedCapacity() - this->size);
|
||||
|
||||
// Provides strong exception safety guarantee,
|
||||
// provided T::~T() nothrow
|
||||
|
|
|
|||
|
|
@ -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<T>::QVector(const QVector<T> &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<T>::detach()
|
|||
return;
|
||||
|
||||
if (!isDetached())
|
||||
realloc(int(d->alloc));
|
||||
realloc(d->allocatedCapacity());
|
||||
Q_ASSERT(isDetached());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void QVector<T>::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 <typename T>
|
||||
|
|
@ -416,9 +416,10 @@ void QVector<T>::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<T>::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<T>::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<T>::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<T>::value(int i, const T &defaultValue) const
|
|||
template <typename T>
|
||||
void QVector<T>::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<T>::isComplex)
|
||||
new (d->end()) T(std::move(copy));
|
||||
|
|
@ -780,10 +781,10 @@ void QVector<T>::append(const T &t)
|
|||
template <typename T>
|
||||
void QVector<T>::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 <typename T>
|
|||
void QVector<T>::removeLast()
|
||||
{
|
||||
Q_ASSERT(!isEmpty());
|
||||
Q_ASSERT(d->alloc);
|
||||
Q_ASSERT(d->allocatedCapacity());
|
||||
|
||||
if (d->ref.isShared())
|
||||
detach();
|
||||
|
|
@ -812,7 +813,7 @@ typename QVector<T>::iterator QVector<T>::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<T>::isRelocatable) {
|
||||
T *b = d->end();
|
||||
|
|
@ -889,7 +890,7 @@ typename QVector<T>::iterator QVector<T>::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<T> &QVector<T>::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();
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ public:
|
|||
bool isSharedWith(const SimpleVector &other) const { return d == other.d; }
|
||||
|
||||
size_t size() const { return d->size; }
|
||||
size_t capacity() const { return d->alloc; }
|
||||
size_t capacity() const { return d->allocatedCapacity(); }
|
||||
|
||||
iterator begin() { detach(); return d->begin(); }
|
||||
iterator end() { detach(); return d->end(); }
|
||||
|
|
|
|||
|
|
@ -154,10 +154,10 @@ void tst_QArrayData::sharedNullEmpty()
|
|||
QVERIFY(null != empty);
|
||||
|
||||
QCOMPARE(null->size, 0);
|
||||
QCOMPARE(null->alloc, 0u);
|
||||
QCOMPARE(null->allocatedCapacity(), size_t(0));
|
||||
|
||||
QCOMPARE(empty->size, 0);
|
||||
QCOMPARE(empty->alloc, 0u);
|
||||
QCOMPARE(empty->allocatedCapacity(), size_t(0));
|
||||
}
|
||||
|
||||
void tst_QArrayData::staticData()
|
||||
|
|
@ -597,9 +597,9 @@ void tst_QArrayData::allocate()
|
|||
|
||||
QCOMPARE(data->size, 0);
|
||||
if (allocateOptions & QArrayData::GrowsForward)
|
||||
QVERIFY(data->alloc > uint(capacity));
|
||||
QVERIFY(data->allocatedCapacity() > uint(capacity));
|
||||
else
|
||||
QCOMPARE(data->alloc, uint(capacity));
|
||||
QCOMPARE(data->allocatedCapacity(), size_t(capacity));
|
||||
QCOMPARE(bool(data->flags & QArrayData::CapacityReserved), isCapacityReserved);
|
||||
|
||||
// Check that the allocated array can be used. Best tested with a
|
||||
|
|
@ -642,9 +642,9 @@ void tst_QArrayData::reallocate()
|
|||
|
||||
QCOMPARE(data->size, capacity);
|
||||
if (allocateOptions & QArrayData::GrowsForward)
|
||||
QVERIFY(data->alloc > uint(newCapacity));
|
||||
QVERIFY(data->allocatedCapacity() > size_t(newCapacity));
|
||||
else
|
||||
QCOMPARE(data->alloc, uint(newCapacity));
|
||||
QCOMPARE(data->allocatedCapacity(), size_t(newCapacity));
|
||||
QCOMPARE(!(data->flags & QArrayData::CapacityReserved), !isCapacityReserved);
|
||||
|
||||
for (int i = 0; i < capacity; ++i)
|
||||
|
|
@ -684,7 +684,7 @@ void tst_QArrayData::alignment()
|
|||
|
||||
QVERIFY(data);
|
||||
QCOMPARE(data->size, 0);
|
||||
QVERIFY(data->alloc >= uint(8));
|
||||
QVERIFY(data->allocatedCapacity() >= uint(8));
|
||||
|
||||
// These conditions should hold as long as header and array are
|
||||
// allocated together
|
||||
|
|
@ -756,7 +756,7 @@ void tst_QArrayData::typedData()
|
|||
|
||||
QVERIFY(array);
|
||||
QCOMPARE(array->size, 0);
|
||||
QCOMPARE(array->alloc, 10u);
|
||||
QCOMPARE(array->allocatedCapacity(), size_t(10));
|
||||
|
||||
// Check that the allocated array can be used. Best tested with a
|
||||
// memory checker, such as valgrind, running.
|
||||
|
|
@ -776,7 +776,7 @@ void tst_QArrayData::typedData()
|
|||
|
||||
QVERIFY(array);
|
||||
QCOMPARE(array->size, 0);
|
||||
QCOMPARE(array->alloc, 10u);
|
||||
QCOMPARE(array->allocatedCapacity(), size_t(10));
|
||||
|
||||
// Check that the allocated array can be used. Best tested with a
|
||||
// memory checker, such as valgrind, running.
|
||||
|
|
@ -796,7 +796,7 @@ void tst_QArrayData::typedData()
|
|||
|
||||
QVERIFY(array);
|
||||
QCOMPARE(array->size, 0);
|
||||
QCOMPARE(array->alloc, 10u);
|
||||
QCOMPARE(array->allocatedCapacity(), size_t(10));
|
||||
|
||||
// Check that the allocated array can be used. Best tested with a
|
||||
// memory checker, such as valgrind, running.
|
||||
|
|
|
|||
Loading…
Reference in New Issue