QVariant: pass the size and alignment to PrivateShared::create()

Instead of having that function load those from memory. This allows us
to pass constant expressions in QVariant::Private s constructor

Change-Id: I3859764fed084846bcb0fffd17045ddd62553710
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-07-22 22:22:48 -07:00
parent 392e445393
commit cfa955040c
2 changed files with 6 additions and 11 deletions

View File

@ -236,7 +236,7 @@ static bool isValidMetaTypeForVariant(const QtPrivate::QMetaTypeInterface *iface
}
template <typename F> static QVariant::PrivateShared *
customConstructShared(const QtPrivate::QMetaTypeInterface *iface, F &&construct)
customConstructShared(size_t size, size_t align, F &&construct)
{
struct Deleter {
void operator()(QVariant::PrivateShared *p) const
@ -245,7 +245,7 @@ customConstructShared(const QtPrivate::QMetaTypeInterface *iface, F &&construct)
// this is exception-safe
std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
ptr.reset(QVariant::PrivateShared::create(iface));
ptr.reset(QVariant::PrivateShared::create(size, align));
construct(ptr->data());
return ptr.release();
}
@ -273,7 +273,7 @@ static void customConstruct(const QtPrivate::QMetaTypeInterface *iface, QVariant
return; // trivial default constructor, we've already memset
construct(iface, d->data.data, copy);
} else {
d->data.shared = customConstructShared(iface, [=](void *where) {
d->data.shared = customConstructShared(iface->size, iface->alignment, [=](void *where) {
construct(iface, where, copy);
});
d->is_shared = true;
@ -310,13 +310,8 @@ static QVariant::Private clonePrivate(const QVariant::Private &other)
} // anonymous used to hide QVariant handlers
inline QVariant::PrivateShared *
QVariant::PrivateShared::create(const QtPrivate::QMetaTypeInterface *type)
inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
{
Q_ASSERT(type);
size_t size = type->size;
size_t align = type->alignment;
size += sizeof(PrivateShared);
if (align > sizeof(PrivateShared)) {
// The alignment is larger than the alignment we can guarantee for the pointer
@ -362,7 +357,7 @@ QVariant::Private::Private(std::piecewise_construct_t, const T &t)
new (data.data) T(t);
} else {
static_assert(!isNothrowQVariantConstructible); // we allocate memory, even if T doesn't
data.shared = customConstructShared(iface, [=](void *where) {
data.shared = customConstructShared(sizeof(T), alignof(T), [=](void *where) {
new (where) T(t);
});
}

View File

@ -61,7 +61,7 @@ public:
private:
inline PrivateShared() : ref(1) { }
public:
static PrivateShared *create(const QtPrivate::QMetaTypeInterface *type);
static PrivateShared *create(size_t size, size_t align);
static void free(PrivateShared *p);
alignas(8) QAtomicInt ref;