From 51089a5742a79467221b5781cb35a8cea023febf Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 12 Nov 2015 10:16:22 +0100 Subject: Use Q_UNLIKELY for every qFatal()/qCritical() If, after checking a condition, we issue a qFatal() or a qCritical(), by definition that check is unlikely to be true. Tell the compiler so it can move the error handling code out of the normal code path to increase the effective icache size. Moved conditional code around where possible so that we could always use Q_UNLIKELY, instead of having to revert to Q_LIKELY here and there. In some cases, simplified the expressions newly wrapped in Q_UNLIKELY as a drive-by. Change-Id: I67537d62b04bc6977d69254690c5ebbdf98bfd6d Reviewed-by: Konstantin Ritt Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/kernel/qcoreapplication.cpp | 2 +- src/corelib/kernel/qeventdispatcher_unix.cpp | 2 +- src/corelib/kernel/qeventdispatcher_win.cpp | 2 +- src/corelib/kernel/qmetatype.cpp | 4 ++-- src/corelib/kernel/qobject.cpp | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 5fe0ea3559..8d9b923e61 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -417,7 +417,7 @@ QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv, uint QCoreApplicationPrivate::is_app_closing = false; # if defined(Q_OS_UNIX) - if (!setuidAllowed && (geteuid() != getuid())) + if (Q_UNLIKELY(!setuidAllowed && (geteuid() != getuid()))) qFatal("FATAL: The application binary appears to be running setuid, this is a security hole."); # endif // Q_OS_UNIX diff --git a/src/corelib/kernel/qeventdispatcher_unix.cpp b/src/corelib/kernel/qeventdispatcher_unix.cpp index 30122e809b..d45ea30a45 100644 --- a/src/corelib/kernel/qeventdispatcher_unix.cpp +++ b/src/corelib/kernel/qeventdispatcher_unix.cpp @@ -135,7 +135,7 @@ QEventDispatcherUNIXPrivate::QEventDispatcherUNIXPrivate() } #endif - if (pipefail) + if (Q_UNLIKELY(pipefail)) qFatal("QEventDispatcherUNIXPrivate(): Can not continue without a thread pipe"); sn_highest = -1; diff --git a/src/corelib/kernel/qeventdispatcher_win.cpp b/src/corelib/kernel/qeventdispatcher_win.cpp index ecaa78cbbc..47b869f62a 100644 --- a/src/corelib/kernel/qeventdispatcher_win.cpp +++ b/src/corelib/kernel/qeventdispatcher_win.cpp @@ -667,7 +667,7 @@ void QEventDispatcherWin32::installMessageHook() #ifndef Q_OS_WINCE // setup GetMessage hook needed to drive our posted events d->getMessageHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC) qt_GetMessageHook, NULL, GetCurrentThreadId()); - if (!d->getMessageHook) { + if (Q_UNLIKELY(!d->getMessageHook)) { int errorCode = GetLastError(); qFatal("Qt: INTERNAL ERROR: failed to install GetMessage hook: %d, %s", errorCode, qPrintable(qt_error_string(errorCode))); diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index e6d745bb74..c0caa3cca5 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -1074,7 +1074,7 @@ int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, previousFlags = QMetaType::typeFlags(idx); } - if (previousSize != size) { + if (Q_UNLIKELY(previousSize != size)) { qFatal("QMetaType::registerType: Binary compatibility break " "-- Size mismatch for type '%s' [%i]. Previously registered " "size %i, now registering size %i.", @@ -1084,7 +1084,7 @@ int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, // these flags cannot change in a binary compatible way: const int binaryCompatibilityFlag = PointerToQObject | IsEnumeration | SharedPointerToQObject | WeakPointerToQObject | TrackingPointerToQObject; - if ((previousFlags ^ flags) & binaryCompatibilityFlag) { + if (Q_UNLIKELY((previousFlags ^ flags) & binaryCompatibilityFlag)) { const char *msg = "QMetaType::registerType: Binary compatibility break. " "\nType flags for type '%s' [%i] don't match. Previously " diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index c9884cd76c..4717cfbf3c 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -203,7 +203,7 @@ QObjectPrivate::QObjectPrivate(int version) // This allows incompatible versions to be loaded, possibly for testing. Q_UNUSED(version); #else - if (version != QObjectPrivateVersion) + if (Q_UNLIKELY(version != QObjectPrivateVersion)) qFatal("Cannot mix incompatible Qt library (version 0x%x) with this library (version 0x%x)", version, QObjectPrivateVersion); #endif -- cgit v1.2.3