diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-11-12 10:16:22 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-11-29 22:59:17 +0000 |
commit | 51089a5742a79467221b5781cb35a8cea023febf (patch) | |
tree | 95f765fa452cdfaa12f986e4d228d9a958c95100 /src/corelib | |
parent | 14d189f7875b7def6f9745bfd20527a0fce19a44 (diff) |
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 <ritt.ks@gmail.com>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qprocess_unix.cpp | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_unix.cpp | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_win.cpp | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 4 | ||||
-rw-r--r-- | src/corelib/kernel/qobject.cpp | 2 | ||||
-rw-r--r-- | src/corelib/tools/qhash.cpp | 6 | ||||
-rw-r--r-- | src/corelib/tools/qsharedpointer.cpp | 8 | ||||
-rw-r--r-- | src/corelib/tools/qsimd.cpp | 2 |
9 files changed, 15 insertions, 15 deletions
diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 472d8699a2..9a25f56c5f 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -560,7 +560,7 @@ static int doSpawn(pid_t *ppid, const posix_spawn_file_actions_t *file_actions, qWarning("ThreadCtl(): failed to chdir to %s", oldWorkingDir); # ifdef Q_OS_QNX - if (ThreadCtl(_NTO_TCTL_THREADS_CONT, 0) == -1) + if (Q_UNLIKELY(ThreadCtl(_NTO_TCTL_THREADS_CONT, 0) == -1)) qFatal("ThreadCtl(): cannot resume threads: %s", qPrintable(qt_error_string(errno))); # endif } 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 diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index b334a697a9..7724516a5d 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -682,17 +682,17 @@ void QHashData::dump() void QHashData::checkSanity() { - if (fakeNext) + if (Q_UNLIKELY(fakeNext)) qFatal("Fake next isn't 0"); for (int i = 0; i < numBuckets; ++i) { Node *n = buckets[i]; Node *p = n; - if (!n) + if (Q_UNLIKELY(!n)) qFatal("%d: Bucket entry is 0", i); if (n != reinterpret_cast<Node *>(this)) { while (n != reinterpret_cast<Node *>(this)) { - if (!n->next) + if (Q_UNLIKELY(!n->next)) qFatal("%d: Next of %p is 0, should be %p", i, n, this); n = n->next; } diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp index 4d30396cb6..b5e27cc720 100644 --- a/src/corelib/tools/qsharedpointer.cpp +++ b/src/corelib/tools/qsharedpointer.cpp @@ -1508,7 +1508,7 @@ void QtSharedPointer::internalSafetyCheckAdd(const void *d_ptr, const volatile v //qDebug("Adding d=%p value=%p", d_ptr, ptr); const void *other_d_ptr = kp->dataPointers.value(ptr, 0); - if (other_d_ptr) { + if (Q_UNLIKELY(other_d_ptr)) { # ifdef BACKTRACE_SUPPORTED printBacktrace(knownPointers()->dPointers.value(other_d_ptr).backtrace); # endif @@ -1539,7 +1539,7 @@ void QtSharedPointer::internalSafetyCheckRemove(const void *d_ptr) QMutexLocker lock(&kp->mutex); QHash<const void *, Data>::iterator it = kp->dPointers.find(d_ptr); - if (it == kp->dPointers.end()) { + if (Q_UNLIKELY(it == kp->dPointers.end())) { qFatal("QSharedPointer: internal self-check inconsistency: pointer %p was not tracked. " "To use QT_SHAREDPOINTER_TRACK_POINTERS, you have to enable it throughout " "in your code.", d_ptr); @@ -1566,10 +1566,10 @@ void QtSharedPointer::internalSafetyCheckCleanCheck() KnownPointers *const kp = knownPointers(); Q_ASSERT_X(kp, "internalSafetyCheckSelfCheck()", "Called after global statics deletion!"); - if (kp->dPointers.size() != kp->dataPointers.size()) + if (Q_UNLIKELY(kp->dPointers.size() != kp->dataPointers.size())) qFatal("Internal consistency error: the number of pointers is not equal!"); - if (!kp->dPointers.isEmpty()) + if (Q_UNLIKELY(!kp->dPointers.isEmpty())) qFatal("Pointer cleaning failed: %d entries remaining", kp->dPointers.size()); # endif } diff --git a/src/corelib/tools/qsimd.cpp b/src/corelib/tools/qsimd.cpp index 3e4fdfaaf1..0b3b84a302 100644 --- a/src/corelib/tools/qsimd.cpp +++ b/src/corelib/tools/qsimd.cpp @@ -685,7 +685,7 @@ void qDetectCpuFeatures() #else bool runningOnValgrind = false; #endif - if (!runningOnValgrind && (minFeature != 0 && (f & minFeature) != minFeature)) { + if (Q_UNLIKELY(!runningOnValgrind && minFeature != 0 && (f & minFeature) != minFeature)) { quint64 missing = minFeature & ~f; fprintf(stderr, "Incompatible processor. This Qt build requires the following features:\n "); for (int i = 0; i < features_count; ++i) { |