QVariant: Extract Method PrivateShared::computeAllocationSize()

Will be re-used in the upcoming emplace() function.

Task-number: QTBUG-112187
Change-Id: Ie2b4570b7ba6c9d0ce938203933758a0d0d59f5a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Fabian Kosmale 2023-05-31 11:05:40 +02:00 committed by Marc Mutz
parent 3a7ddd7274
commit 4e860a66d6
2 changed files with 8 additions and 1 deletions

View File

@ -78,6 +78,7 @@ public:
private:
inline PrivateShared() : ref(1) { }
public:
static size_t computeAllocationSize(size_t size, size_t align);
static PrivateShared *create(size_t size, size_t align);
static void free(PrivateShared *p);

View File

@ -34,7 +34,7 @@ customConstructShared(size_t size, size_t align, F &&construct)
return ptr.release();
}
inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
inline size_t QVariant::PrivateShared::computeAllocationSize(size_t size, size_t align)
{
size += sizeof(PrivateShared);
if (align > sizeof(PrivateShared)) {
@ -44,6 +44,12 @@ inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, siz
// alignment.
size += align - sizeof(PrivateShared);
}
return size;
}
inline QVariant::PrivateShared *QVariant::PrivateShared::create(size_t size, size_t align)
{
size = computeAllocationSize(size, align);
void *data = operator new(size);
auto *ps = new (data) QVariant::PrivateShared();
ps->offset = int(((quintptr(ps) + sizeof(PrivateShared) + align - 1) & ~(align - 1)) - quintptr(ps));