summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2017-02-13 16:36:49 -0800
committerThiago Macieira <thiago.macieira@intel.com>2017-02-17 02:16:02 +0000
commitad1b3a49807cdb456698991142da0244c0612a1c (patch)
tree2f2ecbc52f79ea61b708ad11bbc5574d5d64767a
parent1088539c1d1ff592050ee732efd53da0bc33d4d0 (diff)
Remove the name cache from QMetaType::typeName
It was unnecessary, since we only cached the static types, which are all generated by the macro anyway. The way it was implemented, this produced data races that are strictly-speaking UB, even if all the threads were writing the same values to the same data locations. This commit changes a little the code to simplify, since we're changing those lines anyway. Task-number: QTBUG-58851 Change-Id: Idc5061f7145940f987dffffd14a30047846e3113 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> (cherry picked from commit fb376e0fcc8d2d0a1731a588bfc6497d05e090e6)
-rw-r--r--src/corelib/kernel/qmetatype.cpp39
1 files changed, 14 insertions, 25 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp
index dc6e3737b6..59d75c182f 100644
--- a/src/corelib/kernel/qmetatype.cpp
+++ b/src/corelib/kernel/qmetatype.cpp
@@ -830,38 +830,27 @@ void QMetaType::registerStreamOperators(int idx, SaveOperator saveOp,
const char *QMetaType::typeName(int typeId)
{
const uint type = typeId;
- // In theory it can be filled during compilation time, but for some reason template code
- // that is able to do it causes GCC 4.6 to generate additional 3K of executable code. Probably
- // it is not worth of it.
- static const char *namesCache[QMetaType::HighestInternalId + 1];
-
- const char *result;
- if (type <= QMetaType::HighestInternalId && ((result = namesCache[type])))
- return result;
-
#define QT_METATYPE_TYPEID_TYPENAME_CONVERTER(MetaTypeName, TypeId, RealName) \
- case QMetaType::MetaTypeName: result = #RealName; break;
+ case QMetaType::MetaTypeName: return #RealName; break;
switch (QMetaType::Type(type)) {
QT_FOR_EACH_STATIC_TYPE(QT_METATYPE_TYPEID_TYPENAME_CONVERTER)
-
- default: {
- if (Q_UNLIKELY(type < QMetaType::User)) {
- return 0; // It can happen when someone cast int to QVariant::Type, we should not crash...
- } else {
- const QVector<QCustomTypeInfo> * const ct = customTypes();
- QReadLocker locker(customTypesLock());
- return ct && uint(ct->count()) > type - QMetaType::User && !ct->at(type - QMetaType::User).typeName.isEmpty()
- ? ct->at(type - QMetaType::User).typeName.constData()
- : 0;
- }
+ case QMetaType::UnknownType:
+ case QMetaType::User:
+ break;
}
+
+ if (Q_UNLIKELY(type < QMetaType::User)) {
+ return Q_NULLPTR; // It can happen when someone cast int to QVariant::Type, we should not crash...
}
-#undef QT_METATYPE_TYPEID_TYPENAME_CONVERTER
- Q_ASSERT(type <= QMetaType::HighestInternalId);
- namesCache[type] = result;
- return result;
+ const QVector<QCustomTypeInfo> * const ct = customTypes();
+ QReadLocker locker(customTypesLock());
+ return ct && uint(ct->count()) > type - QMetaType::User && !ct->at(type - QMetaType::User).typeName.isEmpty()
+ ? ct->at(type - QMetaType::User).typeName.constData()
+ : Q_NULLPTR;
+
+#undef QT_METATYPE_TYPEID_TYPENAME_CONVERTER
}
/*!