summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-06-11 18:45:02 +0200
committerLars Knoll <lars.knoll@qt.io>2019-12-08 10:29:01 +0100
commita3aa2fcfa72ab69bdbded26dcd43e37b35796a17 (patch)
tree3c8bdce81299261ffc381bebcae2e7c1312971c9 /src/corelib/text
parentb6aa133cf556c962e666b8facc9ade9fab0dedbd (diff)
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 <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qbytearray.cpp24
-rw-r--r--src/corelib/text/qbytearray.h8
-rw-r--r--src/corelib/text/qstring.cpp18
-rw-r--r--src/corelib/text/qstring.h10
4 files changed, 32 insertions, 28 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index d7fcfce90c..da91a93700 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -63,7 +63,7 @@
#include <string.h>
#include <stdlib.h>
-#define IS_RAW_DATA(d) (!(d)->isMutable())
+#define IS_RAW_DATA(d) ((d)->flags & QArrayData::RawDataType)
QT_BEGIN_NAMESPACE
@@ -1208,7 +1208,7 @@ QByteArray &QByteArray::operator=(const char *str)
} else {
const int len = int(strlen(str));
const int fullLen = len + 1;
- if (d->ref.isShared() || fullLen > int(d->allocatedCapacity())
+ if (d->needsDetach() || fullLen > int(d->allocatedCapacity())
|| (len < d->size && fullLen < int(d->allocatedCapacity() >> 1)))
reallocData(fullLen, d->detachFlags());
x = d;
@@ -1755,7 +1755,7 @@ void QByteArray::resize(int size)
if (size < 0)
size = 0;
- if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
+ if (!d->ref.isShared() && !d->isMutable() && size < d->size) {
d->size = size;
return;
}
@@ -1775,9 +1775,9 @@ void QByteArray::resize(int size)
x->data()[size] = '\0';
d = x;
} else {
- if (d->ref.isShared() || size > capacity())
+ if (d->needsDetach() || size > capacity())
reallocData(uint(size) + 1u, d->detachFlags() | Data::GrowsForward);
- if (d->allocatedCapacity()) {
+ if (d->isMutable()) {
d->size = size;
d->data()[size] = '\0';
}
@@ -1805,7 +1805,7 @@ QByteArray &QByteArray::fill(char ch, int size)
void QByteArray::reallocData(uint alloc, Data::ArrayOptions options)
{
- if (d->ref.isShared() || IS_RAW_DATA(d)) {
+ if (d->needsDetach()) {
Data *x = Data::allocate(alloc, options);
Q_CHECK_PTR(x);
x->size = qMin(int(alloc) - 1, d->size);
@@ -1900,7 +1900,7 @@ QByteArray &QByteArray::prepend(const char *str)
QByteArray &QByteArray::prepend(const char *str, int len)
{
if (str) {
- if (d->ref.isShared() || d->size + len > capacity())
+ if (d->needsDetach() || 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() || d->size + 1 > capacity())
+ if (d->needsDetach() || 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() || d->size + ba.d->size > capacity())
+ if (d->needsDetach() || 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() || d->size + len > capacity())
+ if (d->needsDetach() || 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() || d->size + len > capacity())
+ if (d->needsDetach() || 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() || d->size + 1 > capacity())
+ if (d->needsDetach() || d->size + 1 > capacity())
reallocData(uint(d->size) + 2u, d->detachFlags() | Data::GrowsForward);
d->data()[d->size++] = ch;
d->data()[d->size] = '\0';
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;
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index d0f141e079..ab08d497bd 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -100,7 +100,7 @@
#define ULLONG_MAX quint64_C(18446744073709551615)
#endif
-#define IS_RAW_DATA(d) (!(d)->isMutable())
+#define IS_RAW_DATA(d) ((d)->flags & QArrayData::RawDataType)
QT_BEGIN_NAMESPACE
@@ -2265,14 +2265,14 @@ void QString::resize(int size)
if (size < 0)
size = 0;
- if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
+ if (!d->ref.isShared() && !d->isMutable() && size < d->size) {
d->size = size;
return;
}
- if (d->ref.isShared() || uint(size) + 1u > d->allocatedCapacity())
+ if (d->needsDetach() || size > capacity())
reallocData(uint(size) + 1u, true);
- if (d->flags & Data::AllocatedDataType) {
+ if (d->isMutable()) {
d->size = size;
d->data()[size] = '\0';
}
@@ -2353,7 +2353,7 @@ void QString::reallocData(uint alloc, bool grow)
if (grow)
allocOptions |= QArrayData::GrowsForward;
- if (d->ref.isShared() || IS_RAW_DATA(d)) {
+ if (d->needsDetach()) {
Data *x = Data::allocate(alloc, allocOptions);
Q_CHECK_PTR(x);
x->size = qMin(int(alloc) - 1, d->size);
@@ -2658,7 +2658,7 @@ QString &QString::append(const QString &str)
if (d == Data::sharedNull()) {
operator=(str);
} else {
- if (d->ref.isShared() || d->size + str.d->size > capacity())
+ if (d->needsDetach() || 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;
@@ -2696,7 +2696,7 @@ QString &QString::append(QLatin1String str)
const char *s = str.latin1();
if (s) {
int len = str.size();
- if (d->ref.isShared() || d->size + len > capacity())
+ if (d->needsDetach() || 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() || d->size + 1 > capacity())
+ if (d->needsDetach() || d->size + 1 > capacity())
reallocData(uint(d->size) + 2u, true);
d->data()[d->size++] = ch.unicode();
d->data()[d->size] = '\0';
@@ -6457,7 +6457,7 @@ int QString::localeAwareCompare_helper(const QChar *data1, int length1,
const ushort *QString::utf16() const
{
- if (IS_RAW_DATA(d)) {
+ if (!d->isMutable()) {
// ensure '\0'-termination for ::fromRawData strings
const_cast<QString*>(this)->reallocData(uint(d->size) + 1u);
}
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index 03d83eeab9..fd674284fe 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -542,7 +542,7 @@ public:
inline QString &prepend(QLatin1String s) { return insert(0, s); }
inline QString &operator+=(QChar c) {
- if (d->ref.isShared() || d->size + 1 > capacity())
+ if (d->needsDetach() || d->size + 1 > capacity())
reallocData(uint(d->size) + 2u, true);
d->data()[d->size++] = c.unicode();
d->data()[d->size] = '\0';
@@ -1041,7 +1041,7 @@ inline QChar *QString::data()
inline const QChar *QString::constData() const
{ return reinterpret_cast<const QChar*>(d->data()); }
inline void QString::detach()
-{ if (d->ref.isShared() || !d->isMutable()) reallocData(uint(d->size) + 1u); }
+{ if (d->needsDetach()) reallocData(uint(d->size) + 1u); }
inline bool QString::isDetached() const
{ return !d->ref.isShared(); }
inline void QString::clear()
@@ -1263,7 +1263,7 @@ inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); }
inline void QString::reserve(int asize)
{
- if (d->ref.isShared() || asize >= capacity())
+ if (d->needsDetach() || asize >= capacity())
reallocData(qMax(asize, size()) + 1u);
// we're not shared anymore, for sure
@@ -1272,7 +1272,9 @@ inline void QString::reserve(int asize)
inline void QString::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);
// we're not shared anymore, for sure