diff options
author | Lars Knoll <lars.knoll@qt.io> | 2019-11-16 23:25:26 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-06 21:29:58 +0200 |
commit | 76004502baa118016c8e0f32895af7a822f1ba37 (patch) | |
tree | 3fb524e333cb2edf6e983c4898c76f9eebb04eb4 /src/corelib/tools/qarraydata.h | |
parent | 2e51686746c45055e5bb74f58e0c159bfb6a8c13 (diff) |
Get rid of shared null for QByteArray, QString and QVector
As a side effect, data() can now return a nullptr. This
has the potential to cause crashes in existig code. To work
around this, return an empty string from QString::data()
and QByteArray::data() for now.
For Qt 6 (and once all our internal issues are fixed), data()
will by default return a nullptr for a null QString, but we'll
offer a #define to enable backwards compatible behavior.
Change-Id: I4f66d97ff1dce3eb99a239f1eab9106fa9b1741a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qarraydata.h')
-rw-r--r-- | src/corelib/tools/qarraydata.h | 68 |
1 files changed, 7 insertions, 61 deletions
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index ed2c7a41c1..d0a285a0e9 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -143,14 +143,6 @@ struct Q_CORE_EXPORT QArrayData return result; } - ArrayOptions cloneFlags() const - { - ArrayOptions result = DefaultAllocationFlags; - if (flags & CapacityReserved) - result |= CapacityReserved; - return result; - } - Q_REQUIRED_RESULT #if defined(Q_CC_GNU) __attribute__((__malloc__)) @@ -159,8 +151,6 @@ struct Q_CORE_EXPORT QArrayData size_t capacity, ArrayOptions options = DefaultAllocationFlags) noexcept; Q_REQUIRED_RESULT static QPair<QArrayData *, void *> reallocateUnaligned(QArrayData *data, void *dataPointer, size_t objectSize, size_t newCapacity, ArrayOptions newOptions = DefaultAllocationFlags) Q_DECL_NOTHROW; - Q_REQUIRED_RESULT static QArrayData *prepareRawData(ArrayOptions options = ArrayOptions(RawDataType)) - Q_DECL_NOTHROW; static void deallocate(QArrayData *data, size_t objectSize, size_t alignment) noexcept; @@ -229,36 +219,14 @@ struct QTypedArrayData QArrayData::deallocate(data, sizeof(T), alignof(AlignmentDummy)); } - static QArrayDataPointerRef<T> fromRawData(const T *data, size_t n, - ArrayOptions options = DefaultRawFlags) + static QArrayDataPointerRef<T> fromRawData(const T *data, size_t n) { static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData)); QArrayDataPointerRef<T> result = { - static_cast<QTypedArrayData *>(prepareRawData(options)), const_cast<T *>(data), uint(n) + nullptr, const_cast<T *>(data), uint(n) }; - if (result.ptr) { - Q_ASSERT(!result.ptr->isShared()); // No shared empty, please! - } return result; } - - static QTypedArrayData *sharedNull() noexcept - { - static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData)); - return static_cast<QTypedArrayData *>(QArrayData::sharedNull()); - } - - static QTypedArrayData *sharedEmpty() - { - static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData)); - return allocate(/* capacity */ 0); - } - - static T *sharedNullData() - { - static_assert(sizeof(QTypedArrayData) == sizeof(QArrayData)); - return static_cast<T *>(QArrayData::sharedNullData()); - } }; //////////////////////////////////////////////////////////////////////////////// @@ -271,33 +239,11 @@ struct QTypedArrayData // Hide array inside a lambda #define Q_ARRAY_LITERAL(Type, ...) \ - ([]() -> QArrayDataPointerRef<Type> { \ - /* MSVC 2010 Doesn't support static variables in a lambda, but */ \ - /* happily accepts them in a static function of a lambda-local */ \ - /* struct :-) */ \ - struct StaticWrapper { \ - static QArrayDataPointerRef<Type> get() \ - { \ - Q_ARRAY_LITERAL_IMPL(Type, __VA_ARGS__) \ - return ref; \ - } \ - }; \ - return StaticWrapper::get(); \ - }()) \ - /**/ - -#define Q_ARRAY_LITERAL_IMPL(Type, ...) \ - /* Portable compile-time array size computation */ \ - static constexpr Type data[] = { __VA_ARGS__ }; \ - enum { Size = sizeof(data) / sizeof(data[0]) }; \ - \ - static constexpr QArrayData literal = { Q_BASIC_ATOMIC_INITIALIZER(-1), QArrayData::StaticDataFlags, 0 };\ - \ - QArrayDataPointerRef<Type> ref = \ - { static_cast<QTypedArrayData<Type> *>( \ - const_cast<QArrayData *>(&literal)), \ - const_cast<Type *>(data), \ - Size }; \ + ([]() -> QArrayDataPointerRef<Type> { \ + static Type const data[] = { __VA_ARGS__ }; \ + enum { Size = sizeof(data) / sizeof(data[0]) }; \ + return { nullptr, const_cast<Type *>(data), Size }; \ + }()) /**/ namespace QtPrivate { |