From a3aa2fcfa72ab69bdbded26dcd43e37b35796a17 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 11 Jun 2012 18:45:02 +0200 Subject: Introduce the Mutable flag and move QArrayDataPointer::needsDetach The Mutable flag now contains the information on whether the data this QArrayData points to is mutable. This decouples the mutability / immutability setting from the allocation and from the type of data, opening the way for mutable raw or foreign data. There are still plenty of places in the source code that check the size of the allocation when it actually wants d->isMutable(). Fixing this will require reviewing all the code, so is left for later. The needsDetach() function is moved to QArrayData and de-constified. It returns true when a reallocation is necessary if the data is to be modified. Change-Id: I17e2bc5a3f6ef1f3eba8a205acd9852b95524f57 Reviewed-by: Simon Hausmann --- src/corelib/text/qbytearray.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/corelib/text/qbytearray.h') diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 611cacc646..597bc8bcb3 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -495,7 +495,7 @@ inline const char *QByteArray::data() const inline const char *QByteArray::constData() const { return d->data(); } inline void QByteArray::detach() -{ if (d->ref.isShared() || !d->isMutable()) reallocData(uint(d->size) + 1u, d->detachFlags()); } +{ if (d->needsDetach()) reallocData(uint(d->size) + 1u, d->detachFlags()); } inline bool QByteArray::isDetached() const { return !d->ref.isShared(); } inline QByteArray::QByteArray(const QByteArray &a) noexcept : d(a.d) @@ -506,7 +506,7 @@ inline int QByteArray::capacity() const inline void QByteArray::reserve(int asize) { - if (d->ref.isShared() || asize > capacity()) { + if (d->needsDetach() || asize > capacity()) { reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved); } else { d->flags |= Data::CapacityReserved; @@ -515,7 +515,9 @@ inline void QByteArray::reserve(int asize) inline void QByteArray::squeeze() { - if (d->ref.isShared() || d->size < capacity()) { + if ((d->flags & Data::CapacityReserved) == 0) + return; + if (d->needsDetach() || d->size < capacity()) { reallocData(uint(d->size) + 1u, d->detachFlags() & ~Data::CapacityReserved); } else { d->flags &= ~Data::CapacityReserved; -- cgit v1.2.3