From d91b4f0b13926a0861d7e172f14437d20d06332e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Thu, 5 Jan 2012 16:29:42 +0100 Subject: Remove shared_empty and unsharable_empty from API They still exist and help avoid allocation of "empty" array headers, but they're no longer part of the public API, thus reducing relocatable symbols and relocations in inline code. This means an extra non-inline call on QArrayDataPointer::clear and setSharable operations, which are (expensive) detaching operations, anyway. Change-Id: Iea804e5ddc8af55ebc0951ca17a7a4e8401abc55 Reviewed-by: Bradley T. Hughes Reviewed-by: Thiago Macieira --- src/corelib/tools/qarraydata.cpp | 11 ++++++----- src/corelib/tools/qarraydata.h | 14 -------------- src/corelib/tools/qarraydatapointer.h | 9 ++------- tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 18 ++++++++++++------ 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp index c6e96c78a9..150f23cc12 100644 --- a/src/corelib/tools/qarraydata.cpp +++ b/src/corelib/tools/qarraydata.cpp @@ -44,8 +44,9 @@ QT_BEGIN_NAMESPACE const QArrayData QArrayData::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; -const QArrayData QArrayData::shared_empty = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; -const QArrayData QArrayData::unsharable_empty = { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, 0 }; + +static const QArrayData qt_array_empty = { Q_REFCOUNT_INITIALIZE_STATIC, 0, 0, 0, 0 }; +static const QArrayData qt_array_unsharable_empty = { { Q_BASIC_ATOMIC_INITIALIZER(0) }, 0, 0, 0, 0 }; QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, size_t capacity, bool reserve, bool sharable) @@ -57,8 +58,8 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment, // Don't allocate empty headers if (!capacity) return sharable - ? const_cast(&shared_empty) - : const_cast(&unsharable_empty); + ? const_cast(&qt_array_empty) + : const_cast(&qt_array_unsharable_empty); // Allocate extra (alignment - Q_ALIGNOF(QArrayData)) padding bytes so we // can properly align the data array. This assumes malloc is able to @@ -90,7 +91,7 @@ void QArrayData::deallocate(QArrayData *data, size_t objectSize, && !(alignment & (alignment - 1))); Q_UNUSED(objectSize) Q_UNUSED(alignment) - if (data == &unsharable_empty) + if (data == &qt_array_unsharable_empty) return; qFree(data); diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index 1fd60e2155..2486bebafa 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -79,8 +79,6 @@ struct Q_CORE_EXPORT QArrayData size_t alignment); static const QArrayData shared_null; - static const QArrayData shared_empty; - static const QArrayData unsharable_empty; }; template @@ -117,18 +115,6 @@ struct QTypedArrayData return static_cast( const_cast(&QArrayData::shared_null)); } - - static QTypedArrayData *sharedEmpty() - { - return static_cast( - const_cast(&QArrayData::shared_empty)); - } - - static QTypedArrayData *unsharableEmpty() - { - return static_cast( - const_cast(&QArrayData::unsharable_empty)); - } }; template diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index e42d146c58..c03e2ef849 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -115,12 +115,7 @@ public: void setSharable(bool sharable) { if (d->alloc == 0 && d->size == 0) { - Q_ASSERT(Data::sharedNull() == d - || Data::sharedEmpty() == d - || Data::unsharableEmpty() == d); - d = sharable - ? Data::sharedEmpty() - : Data::unsharableEmpty(); + d = Data::allocate(0, false, sharable); return; } @@ -136,7 +131,7 @@ public: void clear() { QArrayDataPointer tmp(d); - d = Data::sharedEmpty(); + d = Data::allocate(0); } bool detach() diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index e8edab20e4..47d5e2a32b 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -153,7 +153,7 @@ void tst_QArrayData::referenceCounting() void tst_QArrayData::sharedNullEmpty() { QArrayData *null = const_cast(&QArrayData::shared_null); - QArrayData *empty = const_cast(&QArrayData::shared_empty); + QArrayData *empty = QArrayData::allocate(1, Q_ALIGNOF(QArrayData), 0, false, true); QVERIFY(null->ref.isStatic()); QVERIFY(null->ref.isSharable()); @@ -492,16 +492,22 @@ void tst_QArrayData::allocate_data() { "void *", sizeof(void *), Q_ALIGNOF(void *) } }; + QArrayData *shared_empty = QArrayData::allocate(0, Q_ALIGNOF(QArrayData), 0, false, true); + QArrayData *unsharable_empty = QArrayData::allocate(0, Q_ALIGNOF(QArrayData), 0, false, false); + + QVERIFY(shared_empty); + QVERIFY(unsharable_empty); + struct { char const *description; bool isCapacityReserved; bool isSharable; const QArrayData *commonEmpty; } options[] = { - { "Default", false, true, &QArrayData::shared_empty }, - { "Reserved", true, true, &QArrayData::shared_empty }, - { "Reserved | Unsharable", true, false, &QArrayData::unsharable_empty }, - { "Unsharable", false, false, &QArrayData::unsharable_empty }, + { "Default", false, true, shared_empty }, + { "Reserved", true, true, shared_empty }, + { "Reserved | Unsharable", true, false, unsharable_empty }, + { "Unsharable", false, false, unsharable_empty }, }; for (size_t i = 0; i < sizeof(types)/sizeof(types[0]); ++i) @@ -635,7 +641,7 @@ void tst_QArrayData::typedData() { QTypedArrayData *null = QTypedArrayData::sharedNull(); - QTypedArrayData *empty = QTypedArrayData::sharedEmpty(); + QTypedArrayData *empty = QTypedArrayData::allocate(0); QVERIFY(null != empty); -- cgit v1.2.3