diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-04-01 01:18:27 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-24 13:51:37 +0200 |
commit | e92e5fda44602d7595aca329a3133ecb9991a6dd (patch) | |
tree | c580e953ccfaa0249c74d4929f940771a7dbefa4 | |
parent | f02e1d6d8e0ee560667b445e8fa43bde85e31f41 (diff) |
Migrate QString over to QArrayData
Change-Id: Ieadc60523a2bef61a088920576c65c720b11bfb9
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r-- | src/corelib/tools/qstring.cpp | 85 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 29 |
2 files changed, 32 insertions, 82 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index a536a091a1..ed0c493c7a 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -796,9 +796,6 @@ const QString::Null QString::null = { }; \sa split() */ -const QStaticStringData<1> QString::shared_null = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } }; -const QStaticStringData<1> QString::shared_empty = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } }; - /*! \typedef QString::ConstIterator Qt-style synonym for QString::const_iterator. @@ -1039,7 +1036,7 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out) QString::QString(const QChar *unicode, int size) { if (!unicode) { - d = shared_null.data_ptr(); + d = Data::sharedNull(); } else { if (size < 0) { size = 0; @@ -1047,15 +1044,11 @@ QString::QString(const QChar *unicode, int size) ++size; } if (!size) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); memcpy(d->data(), unicode, size * sizeof(QChar)); d->data()[size] = '\0'; } @@ -1071,15 +1064,11 @@ QString::QString(const QChar *unicode, int size) QString::QString(int size, QChar ch) { if (size <= 0) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; ushort *i = d->data() + size; ushort *b = d->data(); @@ -1097,13 +1086,9 @@ QString::QString(int size, QChar ch) */ QString::QString(int size, Qt::Initialization) { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; } @@ -1119,13 +1104,9 @@ QString::QString(int size, Qt::Initialization) */ QString::QString(QChar ch) { - d = (Data *) ::malloc(sizeof(Data) + 2*sizeof(QChar)); + d = Data::allocate(2); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = 1; - d->alloc = 2u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[0] = ch.unicode(); d->data()[1] = '\0'; } @@ -1182,12 +1163,6 @@ QString::QString(QChar ch) \internal */ -// ### Qt 5: rename freeData() to avoid confusion. See task 197625. -void QString::free(Data *d) -{ - ::free(d); -} - /*! Sets the size of the string to \a size characters. @@ -1229,9 +1204,9 @@ void QString::resize(int size) } if (size == 0 && !d->capacityReserved) { - Data *x = shared_empty.data_ptr(); + Data *x = Data::allocate(0); if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = x; } else { if (d->ref.isShared() || uint(size) + 1u > d->alloc @@ -1301,17 +1276,14 @@ void QString::reallocData(uint alloc, bool grow) alloc = qAllocMore(alloc * sizeof(QChar), sizeof(Data)) / sizeof(QChar); if (d->ref.isShared() || IS_RAW_DATA(d)) { - Data *x = static_cast<Data *>(::malloc(sizeof(Data) + alloc * sizeof(QChar))); + Data::AllocationOptions allocOptions(d->capacityReserved ? Data::CapacityReserved : 0); + Data *x = Data::allocate(alloc, allocOptions); Q_CHECK_PTR(x); - x->ref.initializeOwned(); x->size = qMin(int(alloc) - 1, d->size); - x->alloc = alloc; - x->capacityReserved = d->capacityReserved; - x->offset = sizeof(QStringData); ::memcpy(x->data(), d->data(), x->size * sizeof(QChar)); x->data()[x->size] = 0; if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = x; } else { Data *p = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc * sizeof(QChar))); @@ -1351,7 +1323,7 @@ QString &QString::operator=(const QString &other) { other.d->ref.ref(); if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = other.d; return *this; } @@ -1521,8 +1493,8 @@ QString& QString::insert(int i, QChar ch) */ QString &QString::append(const QString &str) { - if (str.d != &shared_null.str) { - if (d == &shared_null.str) { + if (str.d != Data::sharedNull()) { + if (d == Data::sharedNull()) { operator=(str); } else { if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc) @@ -4051,19 +4023,15 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size) { Data *d; if (!str) { - d = shared_null.data_ptr(); + d = Data::sharedNull(); } else if (size == 0 || (!*str && size < 0)) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { if (size < 0) size = qstrlen(str); - d = static_cast<Data *>(::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar))); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; ushort *dst = d->data(); /* SIMD: @@ -4129,7 +4097,7 @@ QString QString::fromLocal8Bit_helper(const char *str, int size) if (!str) return QString(); if (size == 0 || (!*str && size < 0)) { - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } #if !defined(QT_NO_TEXTCODEC) @@ -4298,7 +4266,7 @@ QString QString::simplified() const break; if (++from == fromEnd) { // All-whitespace string - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } } @@ -4393,7 +4361,7 @@ QString QString::trimmed() const } int l = end - start + 1; if (l <= 0) { - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } return QString(s + start, l); @@ -7413,17 +7381,12 @@ QString QString::fromRawData(const QChar *unicode, int size) { Data *x; if (!unicode) { - x = shared_null.data_ptr(); + x = Data::sharedNull(); } else if (!size) { - x = shared_empty.data_ptr(); + x = Data::allocate(0); } else { - x = static_cast<Data *>(::malloc(sizeof(Data))); + x = Data::fromRawData(reinterpret_cast<const ushort *>(unicode), size); Q_CHECK_PTR(x); - x->ref.initializeOwned(); - x->size = size; - x->alloc = 0; - x->capacityReserved = false; - x->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(x); } QStringDataPtr dataPtr = { x }; return QString(dataPtr); diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index a96046d837..9fcd203a3f 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -69,17 +69,7 @@ class QLatin1String; class QStringRef; template <typename T> class QVector; -struct QStringData { - QtPrivate::RefCount ref; - int size; - uint alloc : 31; - uint capacityReserved : 1; - - qptrdiff offset; - - inline ushort *data() { return reinterpret_cast<ushort *>(reinterpret_cast<char *>(this) + offset); } - inline const ushort *data() const { return reinterpret_cast<const ushort *>(reinterpret_cast<const char *>(this) + offset); } -}; +typedef QTypedArrayData<ushort> QStringData; #if defined(Q_COMPILER_UNICODE_STRINGS) @@ -162,13 +152,13 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, template <int N> struct QStaticStringData { - QStringData str; + QArrayData str; qunicodechar data[N + 1]; QStringData *data_ptr() const { Q_ASSERT(str.ref.isStatic()); - return const_cast<QStringData *>(&str); + return const_cast<QStringData *>(static_cast<const QStringData*>(&str)); } }; @@ -621,9 +611,9 @@ public: // compatibility struct Null { }; static const Null null; - inline QString(const Null &): d(shared_null.data_ptr()) {} + inline QString(const Null &): d(Data::sharedNull()) {} inline QString &operator=(const Null &) { *this = QString(); return *this; } - inline bool isNull() const { return d == &shared_null.str; } + inline bool isNull() const { return d == Data::sharedNull(); } bool isSimpleText() const; @@ -642,11 +632,8 @@ private: QString &operator=(const QByteArray &a); #endif - static const QStaticStringData<1> shared_null; - static const QStaticStringData<1> shared_empty; Data *d; - static void free(Data *); void reallocData(uint alloc, bool grow = false); void expand(int i); void updateProperties() const; @@ -903,8 +890,8 @@ inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); } inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); } -inline QString::QString() : d(shared_null.data_ptr()) {} -inline QString::~QString() { if (!d->ref.deref()) free(d); } +inline QString::QString() : d(Data::sharedNull()) {} +inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); } inline void QString::reserve(int asize) { @@ -1179,7 +1166,7 @@ public: inline const QChar *unicode() const { if (!m_string) - return reinterpret_cast<const QChar *>(QString::shared_null.str.data()); + return reinterpret_cast<const QChar *>(QString::Data::sharedNull()->data()); return m_string->unicode() + m_position; } inline const QChar *data() const { return unicode(); } |