summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2012-06-04 22:31:26 +0200
committerLars Knoll <lars.knoll@qt.io>2019-12-07 14:18:19 +0100
commitf6a151024b03158bcf46dc86e346d4227f8ca9d4 (patch)
tree898bbc0be052034801a5269bdd291de09766bd95 /src/corelib/text
parent8fb45ae5b8b8ad276aeb9bc9e40f351f47523087 (diff)
Introduce flags to indicate the QArrayData type
These flags allow us to determine what type of data QArrayData is carrying. There are currently only two supported types: - raw data type: constructed via fromRawData or static data - allocated data type: regular data done via heap allocation The QArrayData object is usually allocated on the heap, unless its own reference count is -1 (indicating static const QArrayData). Such object should have a type of RawDataType, since we can't call free(). Add GrowsBackward for completeness as well as the StaticDataFlags default for static data. Change-Id: Icc915a468a2acf2eae91a94e82451f852d382c92 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qbytearray.cpp15
-rw-r--r--src/corelib/text/qbytearray.h2
-rw-r--r--src/corelib/text/qstring.cpp17
-rw-r--r--src/corelib/text/qstring.h2
-rw-r--r--src/corelib/text/qstringliteral.h2
5 files changed, 16 insertions, 22 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index d70977b841..fa328a6d26 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)->offset != sizeof(QByteArrayData))
+#define IS_RAW_DATA(d) (!(d)->isMutable())
QT_BEGIN_NAMESPACE
@@ -4469,16 +4469,13 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
*/
QByteArray &QByteArray::setRawData(const char *data, uint size)
{
- if (d->ref.isShared() || d->allocatedCapacity()) {
+ if (!data || !size) {
+ clear();
+ } else if (d->ref.isShared() || (d->flags & Data::RawDataType) == 0) {
*this = fromRawData(data, size);
} else {
- if (data) {
- d->size = size;
- d->offset = data - reinterpret_cast<char *>(d);
- } else {
- d->offset = sizeof(QByteArrayData);
- d->size = 0;
- }
+ d->size = size;
+ d->offset = data - reinterpret_cast<char *>(d);
}
return *this;
}
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index c161e92743..611cacc646 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->offset != sizeof(QByteArrayData))) reallocData(uint(d->size) + 1u, d->detachFlags()); }
+{ if (d->ref.isShared() || !d->isMutable()) 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)
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index e00f2998b0..d0f141e079 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)->offset != sizeof(QStringData))
+#define IS_RAW_DATA(d) (!(d)->isMutable())
QT_BEGIN_NAMESPACE
@@ -2272,7 +2272,7 @@ void QString::resize(int size)
if (d->ref.isShared() || uint(size) + 1u > d->allocatedCapacity())
reallocData(uint(size) + 1u, true);
- if (d->alloc) {
+ if (d->flags & Data::AllocatedDataType) {
d->size = size;
d->data()[size] = '\0';
}
@@ -9149,16 +9149,13 @@ QString QString::fromRawData(const QChar *unicode, int size)
*/
QString &QString::setRawData(const QChar *unicode, int size)
{
- if (d->ref.isShared() || d->allocatedCapacity()) {
+ if (!unicode || !size) {
+ clear();
+ } else if (d->ref.isShared() || !IS_RAW_DATA(d)) {
*this = fromRawData(unicode, size);
} else {
- if (unicode) {
- d->size = size;
- d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
- } else {
- d->offset = sizeof(QStringData);
- d->size = 0;
- }
+ d->size = size;
+ d->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(d);
}
return *this;
}
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index 4d853bfc72..03d83eeab9 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -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->offset != sizeof(QStringData))) reallocData(uint(d->size) + 1u); }
+{ if (d->ref.isShared() || !d->isMutable()) reallocData(uint(d->size) + 1u); }
inline bool QString::isDetached() const
{ return !d->ref.isShared(); }
inline void QString::clear()
diff --git a/src/corelib/text/qstringliteral.h b/src/corelib/text/qstringliteral.h
index b407bf6bc0..796909dcab 100644
--- a/src/corelib/text/qstringliteral.h
+++ b/src/corelib/text/qstringliteral.h
@@ -75,7 +75,7 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2,
/**/
#define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \
- { Q_REFCOUNT_INITIALIZE_STATIC, QArrayData::DefaultAllocationFlags, size, 0, offset } \
+ { Q_REFCOUNT_INITIALIZE_STATIC, QArrayData::StaticDataFlags, size, 0, offset } \
/**/
#define Q_STATIC_STRING_DATA_HEADER_INITIALIZER(size) \