diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-07-05 13:52:38 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-07-27 12:35:50 -0700 |
commit | 2e6398bd4626c054f18ca9762523dfcb49e31ab8 (patch) | |
tree | 1df9696b2cbb51947ca0a5e77b443b48562a53d8 /src/corelib | |
parent | cfdaf9226d5dd15a50cffa38ac39a1e29cb5e2fd (diff) |
QMetaType: don't record trivial construction function pointers
We can implement the trivial {default,copy,move} construction outselves
inside qmetatype.cpp and qvariant.cpp, simplifying the QMetaType
interface object, removing up to three relocations per QMTI.
This adds the testing for QMetaType::isXxxConstructible and
isDestructible that couldn't be added before.
Change-Id: Ic44396b31ba04712aab3fffd16ff0a28f541d507
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 51 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.h | 18 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype_p.h | 39 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 3 |
4 files changed, 75 insertions, 36 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index f2676fa48f..16d51ae7a1 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -414,7 +414,9 @@ const char *QtMetaTypePrivate::typedefNameForType(const QtPrivate::QMetaTypeInte The enum describes attributes of a type supported by QMetaType. - \value NeedsConstruction This type has non-trivial constructors. If the flag is not set instances can be safely initialized with memset to 0. + \value NeedsConstruction This type has a non-trivial default constructor. If the flag is not set, instances can be safely initialized with memset to 0. + \value NeedsCopyConstruction (since 6.5) This type has a non-trivial copy construtcor. If the flag is not set, instances can be copied with memcpy. + \value NeedsMoveConstruction (since 6.5) This type has a non-trivial move constructor. If the flag is not set, instances can be moved with memcpy. \value NeedsDestruction This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects. \value RelocatableType An instance of a type having this attribute can be safely moved to a different memory location using memcpy. \omitvalue MovableType @@ -429,6 +431,14 @@ const char *QtMetaTypePrivate::typedefNameForType(const QtPrivate::QMetaTypeInte \omitvalue PointerToGadget \omitvalue IsQmlList \value IsConst Indicates that values of this types are immutable; for instance because they are pointers to const objects. + + \note Before Qt 6.5, both the NeedsConstruction and NeedsDestruction flags + were incorrectly set if the either copy construtor or destructor were + non-trivial (that is, if the type was not trivial). + + Note that the Needs flags may be set but the meta type may not have a + publicly-accessible constructor of the relevant type or a + publicly-accessible destructor. */ /*! @@ -598,16 +608,17 @@ int QMetaType::registerHelper(const QtPrivate::QMetaTypeInterface *iface) */ void *QMetaType::create(const void *copy) const { - if (d_ptr && (copy ? !!d_ptr->copyCtr : !!d_ptr->defaultCtr)) { - void *where = + if (copy ? !isCopyConstructible() : !isDefaultConstructible()) + return nullptr; + + void *where = #ifdef __STDCPP_DEFAULT_NEW_ALIGNMENT__ d_ptr->alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__ ? operator new(d_ptr->size, std::align_val_t(d_ptr->alignment)) : #endif operator new(d_ptr->size); - return construct(where, copy); - } - return nullptr; + QtMetaTypePrivate::construct(d_ptr, where, copy); + return where; } /*! @@ -621,9 +632,8 @@ void *QMetaType::create(const void *copy) const */ void QMetaType::destroy(void *data) const { - if (d_ptr) { - if (d_ptr->dtor) - d_ptr->dtor(d_ptr, data); + if (data && isDestructible()) { + QtMetaTypePrivate::destruct(d_ptr, data); if (d_ptr->alignment > __STDCPP_DEFAULT_NEW_ALIGNMENT__) { operator delete(data, std::align_val_t(d_ptr->alignment)); } else { @@ -662,16 +672,11 @@ void *QMetaType::construct(void *where, const void *copy) const { if (!where) return nullptr; - if (d_ptr) { - if (copy && d_ptr->copyCtr) { - d_ptr->copyCtr(d_ptr, where, copy); - return where; - } else if (!copy && d_ptr->defaultCtr) { - d_ptr->defaultCtr(d_ptr, where); - return where; - } - } - return nullptr; + if (copy ? !isCopyConstructible() : !isDefaultConstructible()) + return nullptr; + + QtMetaTypePrivate::construct(d_ptr, where, copy); + return where; } /*! @@ -687,12 +692,8 @@ void *QMetaType::construct(void *where, const void *copy) const */ void QMetaType::destruct(void *data) const { - if (!data) - return; - if (d_ptr && d_ptr->dtor) { - d_ptr->dtor(d_ptr, data); - return; - } + if (data && isDestructible()) + QtMetaTypePrivate::destruct(d_ptr, data); } static QPartialOrdering threeWayCompare(const void *ptr1, const void *ptr2) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 28d246d2cf..7a66767258 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -381,6 +381,9 @@ public: IsPointer = 0x800, IsQmlList =0x1000, // used in the QML engine to recognize QQmlListProperty<T> and list<T> IsConst = 0x2000, + // since 6.5: + NeedsCopyConstruction = 0x4000, + NeedsMoveConstruction = 0x8000, }; Q_DECLARE_FLAGS(TypeFlags, TypeFlag) @@ -1187,8 +1190,10 @@ namespace QtPrivate { struct QMetaTypeTypeFlags { enum { Flags = (QTypeInfo<T>::isRelocatable ? QMetaType::RelocatableType : 0) - | (QTypeInfo<T>::isComplex ? QMetaType::NeedsConstruction : 0) - | (QTypeInfo<T>::isComplex ? QMetaType::NeedsDestruction : 0) + | (!std::is_trivially_default_constructible_v<T> ? QMetaType::NeedsConstruction : 0) + | (!std::is_trivially_destructible_v<T> ? QMetaType::NeedsDestruction : 0) + | (!std::is_trivially_copy_constructible_v<T> ? QMetaType::NeedsCopyConstruction : 0) + | (!std::is_trivially_move_constructible_v<T> ? QMetaType::NeedsMoveConstruction : 0) | (IsPointerToTypeDerivedFromQObject<T>::Value ? QMetaType::PointerToQObject : 0) | (IsSharedPointerToTypeDerivedFromQObject<T>::Value ? QMetaType::SharedPointerToQObject : 0) | (IsWeakPointerToTypeDerivedFromQObject<T>::Value ? QMetaType::WeakPointerToQObject : 0) @@ -2312,10 +2317,11 @@ class QMetaTypeForType { public: static constexpr decltype(typenameHelper<S>()) name = typenameHelper<S>(); + static constexpr unsigned Flags = QMetaTypeTypeFlags<S>::Flags; static constexpr QMetaTypeInterface::DefaultCtrFn getDefaultCtr() { - if constexpr (std::is_default_constructible_v<S>) { + if constexpr (std::is_default_constructible_v<S> && !std::is_trivially_default_constructible_v<S>) { return [](const QMetaTypeInterface *, void *addr) { new (addr) S(); }; } else { return nullptr; @@ -2324,7 +2330,7 @@ public: static constexpr QMetaTypeInterface::CopyCtrFn getCopyCtr() { - if constexpr (std::is_copy_constructible_v<S>) { + if constexpr (std::is_copy_constructible_v<S> && !std::is_trivially_copy_constructible_v<S>) { return [](const QMetaTypeInterface *, void *addr, const void *other) { new (addr) S(*reinterpret_cast<const S *>(other)); }; @@ -2335,7 +2341,7 @@ public: static constexpr QMetaTypeInterface::MoveCtrFn getMoveCtr() { - if constexpr (std::is_move_constructible_v<S>) { + if constexpr (std::is_move_constructible_v<S> && !std::is_trivially_move_constructible_v<S>) { return [](const QMetaTypeInterface *, void *addr, void *other) { new (addr) S(std::move(*reinterpret_cast<S *>(other))); }; @@ -2386,7 +2392,7 @@ struct QMetaTypeInterfaceWrapper /*.revision=*/ 0, /*.alignment=*/ alignof(T), /*.size=*/ sizeof(T), - /*.flags=*/ QMetaTypeTypeFlags<T>::Flags, + /*.flags=*/ QMetaTypeForType<T>::Flags, /*.typeId=*/ BuiltinMetaType<T>::value, /*.metaObjectFn=*/ MetaObjectForType<T>::metaObjectFunction, /*.name=*/ QMetaTypeForType<T>::getName(), diff --git a/src/corelib/kernel/qmetatype_p.h b/src/corelib/kernel/qmetatype_p.h index 89f1854bf6..08c1aeaa1d 100644 --- a/src/corelib/kernel/qmetatype_p.h +++ b/src/corelib/kernel/qmetatype_p.h @@ -141,21 +141,52 @@ inline bool isDefaultConstructible(const QtPrivate::QMetaTypeInterface *iface) n inline bool isCopyConstructible(const QtPrivate::QMetaTypeInterface *iface) noexcept { - // ### broken - return checkMetaTypeFlagOrPointer(iface, iface->copyCtr, QMetaType::NeedsConstruction); + return checkMetaTypeFlagOrPointer(iface, iface->copyCtr, QMetaType::NeedsCopyConstruction); } inline bool isMoveConstructible(const QtPrivate::QMetaTypeInterface *iface) noexcept { - return iface->moveCtr; + return checkMetaTypeFlagOrPointer(iface, iface->moveCtr, QMetaType::NeedsMoveConstruction); } inline bool isDestructible(const QtPrivate::QMetaTypeInterface *iface) noexcept { - // ### broken return checkMetaTypeFlagOrPointer(iface, iface->dtor, QMetaType::NeedsDestruction); } +inline void defaultConstruct(const QtPrivate::QMetaTypeInterface *iface, void *where) +{ + Q_ASSERT(isDefaultConstructible(iface)); + if (iface->defaultCtr) + iface->defaultCtr(iface, where); + else + memset(where, 0, iface->size); +} + +inline void copyConstruct(const QtPrivate::QMetaTypeInterface *iface, void *where, const void *copy) +{ + Q_ASSERT(isCopyConstructible(iface)); + if (iface->copyCtr) + iface->copyCtr(iface, where, copy); + else + memcpy(where, copy, iface->size); +} + +inline void construct(const QtPrivate::QMetaTypeInterface *iface, void *where, const void *copy) +{ + if (copy) + copyConstruct(iface, where, copy); + else + defaultConstruct(iface, where); +} + +inline void destruct(const QtPrivate::QMetaTypeInterface *iface, void *where) +{ + Q_ASSERT(isDestructible(iface)); + if (iface->dtor) + iface->dtor(iface, where); +} + const char *typedefNameForType(const QtPrivate::QMetaTypeInterface *type_d); template<typename T> diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 59148e1579..0fd14296e9 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -222,7 +222,8 @@ static void customConstruct(QVariant::Private *d, const void *copy) return; } - if (!isCopyConstructible(iface) || (!copy && !isDefaultConstructible(iface))) { + if (!isCopyConstructible(iface) || (!copy && !isDefaultConstructible(iface)) + || !isDestructible(iface)) { *d = QVariant::Private(); qWarning("QVariant: Provided metatype does not support " "destruction, copy and default construction"); |