From 919b723088b8617b202b92d80b8d0983e4fd9500 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 17 Aug 2009 10:17:27 +0200 Subject: Improve memory usage of the connectionlists inside QObject ... by not allocating space for slots in the vector. Before, the vector uses the signal index as index. The problem is that the slots and signal are mixed in the same index space. We solve the problem by having a different index space for the signal in the connectionlists vector. All we need to do is to add the information about the number of signals in the moc. Also, we are not connecting to cloned signal but only to the orginial ones. For example, destroyed(QObject * = 0) would generate two signal, we now only connect to the first one. This also improve a little bit the performence while activating signals since it removed one call to indexOfMethod. Reviewed-by: Brad --- src/corelib/animation/qvariantanimation.cpp | 15 +- src/corelib/animation/qvariantanimation_p.h | 2 - src/corelib/kernel/qmetaobject.cpp | 102 ++++----- src/corelib/kernel/qmetaobject_p.h | 74 ++++++ src/corelib/kernel/qobject.cpp | 335 +++++++++++++++++----------- src/corelib/kernel/qobject_p.h | 3 + src/corelib/kernel/qobjectdefs.h | 8 +- src/gui/graphicsview/qgraphicsitem.cpp | 2 +- src/gui/graphicsview/qgraphicsscene.cpp | 17 +- src/gui/graphicsview/qgraphicsscene_p.h | 2 +- src/tools/moc/generator.cpp | 53 +---- 11 files changed, 353 insertions(+), 260 deletions(-) diff --git a/src/corelib/animation/qvariantanimation.cpp b/src/corelib/animation/qvariantanimation.cpp index fc11815..696a95a 100644 --- a/src/corelib/animation/qvariantanimation.cpp +++ b/src/corelib/animation/qvariantanimation.cpp @@ -137,7 +137,6 @@ QT_BEGIN_NAMESPACE \sa currentValue */ - static bool animationValueLessThan(const QVariantAnimation::KeyValue &p1, const QVariantAnimation::KeyValue &p2) { return p1.first < p2.first; @@ -178,11 +177,8 @@ template<> Q_INLINE_TEMPLATE QLineF _q_interpolate(const QLineF &f, const QLineF return QLineF( _q_interpolate(f.p1(), t.p1(), progress), _q_interpolate(f.p2(), t.p2(), progress)); } -QVariantAnimationPrivate::QVariantAnimationPrivate() : duration(250), interpolator(&defaultInterpolator), - changedSignalMask(1 << QVariantAnimation::staticMetaObject.indexOfSignal("valueChanged(QVariant)")) -{ - //we keep the mask so that we emit valueChanged only when needed (for performance reasons) -} +QVariantAnimationPrivate::QVariantAnimationPrivate() : duration(250), interpolator(&defaultInterpolator) +{ } void QVariantAnimationPrivate::convertValues(int t) { @@ -278,7 +274,12 @@ void QVariantAnimationPrivate::setCurrentValueForProgress(const qreal progress) localProgress); qSwap(currentValue, ret); q->updateCurrentValue(currentValue); - if ((connectedSignals[0] & changedSignalMask) && currentValue != ret) { + static QBasicAtomicInt changedSignalIndex = Q_BASIC_ATOMIC_INITIALIZER(0); + if (!changedSignalIndex) { + //we keep the mask so that we emit valueChanged only when needed (for performance reasons) + changedSignalIndex.testAndSetRelaxed(0, signalIndex("valueChanged(QVariant)")); + } + if (isSignalConnected(changedSignalIndex) && currentValue != ret) { //the value has changed emit q->valueChanged(currentValue); } diff --git a/src/corelib/animation/qvariantanimation_p.h b/src/corelib/animation/qvariantanimation_p.h index ef57a4c..ce625f1 100644 --- a/src/corelib/animation/qvariantanimation_p.h +++ b/src/corelib/animation/qvariantanimation_p.h @@ -93,8 +93,6 @@ public: QVariantAnimation::Interpolator interpolator; - const quint32 changedSignalMask; - void setCurrentValueForProgress(const qreal progress); void recalculateCurrentInterval(bool force=false); void setValueAt(qreal, const QVariant &); diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 522f0dc..847938f 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -140,63 +140,6 @@ QT_BEGIN_NAMESPACE \value Public */ -// do not touch without touching the moc as well -enum PropertyFlags { - Invalid = 0x00000000, - Readable = 0x00000001, - Writable = 0x00000002, - Resettable = 0x00000004, - EnumOrFlag = 0x00000008, - StdCppSet = 0x00000100, -// Override = 0x00000200, - Constant = 0x00000400, - Final = 0x00000800, - Designable = 0x00001000, - ResolveDesignable = 0x00002000, - Scriptable = 0x00004000, - ResolveScriptable = 0x00008000, - Stored = 0x00010000, - ResolveStored = 0x00020000, - Editable = 0x00040000, - ResolveEditable = 0x00080000, - User = 0x00100000, - ResolveUser = 0x00200000, - Notify = 0x00400000 -}; - -enum MethodFlags { - AccessPrivate = 0x00, - AccessProtected = 0x01, - AccessPublic = 0x02, - AccessMask = 0x03, //mask - - MethodMethod = 0x00, - MethodSignal = 0x04, - MethodSlot = 0x08, - MethodConstructor = 0x0c, - MethodTypeMask = 0x0c, - - MethodCompatibility = 0x10, - MethodCloned = 0x20, - MethodScriptable = 0x40 -}; - -enum MetaObjectFlags { - DynamicMetaObject = 0x01 -}; - -struct QMetaObjectPrivate -{ - int revision; - int className; - int classInfoCount, classInfoData; - int methodCount, methodData; - int propertyCount, propertyData; - int enumeratorCount, enumeratorData; - int constructorCount, constructorData; - int flags; -}; - static inline const QMetaObjectPrivate *priv(const uint* data) { return reinterpret_cast(data); } @@ -599,29 +542,46 @@ int QMetaObject::indexOfMethod(const char *method) const */ int QMetaObject::indexOfSignal(const char *signal) const { - int i = -1; const QMetaObject *m = this; - while (m && i < 0) { + int i = QMetaObjectPrivate::indexOfSignalRelative(&m, signal); + if (i >= 0) + i += m->methodOffset(); + return i; +} + +/*! \internal + Same as QMetaObject::indexOfSignal, but the result is the local offset to the base object. + + \a baseObject will be adjusted to the enclosing QMetaObject, or 0 if the signal is not found +*/ +int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject, const char *signal) +{ + int i = -1; + while (*baseObject) { + const QMetaObject *const m = *baseObject; for (i = priv(m->d.data)->methodCount-1; i >= 0; --i) if ((m->d.data[priv(m->d.data)->methodData + 5*i + 4] & MethodTypeMask) == MethodSignal && strcmp(signal, m->d.stringdata - + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) { - i += m->methodOffset(); + + m->d.data[priv(m->d.data)->methodData + 5*i]) == 0) { break; } - m = m->d.superdata; + if (i >= 0) + break; + *baseObject = m->d.superdata; } #ifndef QT_NO_DEBUG + const QMetaObject *m = *baseObject; if (i >= 0 && m && m->d.superdata) { int conflict = m->d.superdata->indexOfMethod(signal); if (conflict >= 0) qWarning("QMetaObject::indexOfSignal:%s: Conflict with %s::%s", - m->d.stringdata, m->d.superdata->d.stringdata, signal); + m->d.stringdata, m->d.superdata->d.stringdata, signal); } #endif return i; } + /*! Finds \a slot and returns its index; otherwise returns -1. @@ -2669,4 +2629,20 @@ const char* QMetaClassInfo::value() const and \a data. */ +/*! \internal + If the local_method_index is a cloned method, return the index of the original. + + Example: if the index of "destroyed()" is passed, the index of "destroyed(QObject*)" is returned + */ +int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index) +{ + int handle = get(mobj)->methodData + 5 * local_method_index; + while (mobj->d.data[handle + 4] & MethodCloned) { + Q_ASSERT(local_method_index > 0); + handle -= 5; + local_method_index--; + } + return local_method_index; +} + QT_END_NAMESPACE diff --git a/src/corelib/kernel/qmetaobject_p.h b/src/corelib/kernel/qmetaobject_p.h index 66ed55c..d843deb 100644 --- a/src/corelib/kernel/qmetaobject_p.h +++ b/src/corelib/kernel/qmetaobject_p.h @@ -54,9 +54,82 @@ // #include +#include QT_BEGIN_NAMESPACE +enum PropertyFlags { + Invalid = 0x00000000, + Readable = 0x00000001, + Writable = 0x00000002, + Resettable = 0x00000004, + EnumOrFlag = 0x00000008, + StdCppSet = 0x00000100, +// Override = 0x00000200, + Constant = 0x00000400, + Final = 0x00000800, + Designable = 0x00001000, + ResolveDesignable = 0x00002000, + Scriptable = 0x00004000, + ResolveScriptable = 0x00008000, + Stored = 0x00010000, + ResolveStored = 0x00020000, + Editable = 0x00040000, + ResolveEditable = 0x00080000, + User = 0x00100000, + ResolveUser = 0x00200000, + Notify = 0x00400000 +}; + +enum MethodFlags { + AccessPrivate = 0x00, + AccessProtected = 0x01, + AccessPublic = 0x02, + AccessMask = 0x03, //mask + + MethodMethod = 0x00, + MethodSignal = 0x04, + MethodSlot = 0x08, + MethodConstructor = 0x0c, + MethodTypeMask = 0x0c, + + MethodCompatibility = 0x10, + MethodCloned = 0x20, + MethodScriptable = 0x40 +}; + +enum MetaObjectFlags { + DynamicMetaObject = 0x01 +}; + + +struct QMetaObjectPrivate +{ + int revision; + int className; + int classInfoCount, classInfoData; + int methodCount, methodData; + int propertyCount, propertyData; + int enumeratorCount, enumeratorData; + int constructorCount, constructorData; //since revision 2 + int flags; //since revision 3 + int signalCount; //since revision 4 + + static inline const QMetaObjectPrivate *get(const QMetaObject *metaobject) + { return reinterpret_cast(metaobject->d.data); } + + static int indexOfSignalRelative(const QMetaObject **baseObject, const char* name); + static int originalClone(const QMetaObject *obj, int local_method_index); + + //defined in qobject.cpp + static bool connect(const QObject *sender, int signal_index, + const QObject *receiver, int method_index, + int type = 0, int *types = 0); + static bool disconnect(const QObject *sender, int signal_index, + const QObject *receiver, int method_index); + +}; + #ifndef UTILS_H // mirrored in moc's utils.h static inline bool is_ident_char(char s) @@ -202,6 +275,7 @@ static QByteArray normalizeTypeInternal(const char *t, const char *e, bool fixSc return result; } + QT_END_NAMESPACE #endif diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 0e75867..022ae13 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -41,6 +41,7 @@ #include "qobject.h" #include "qobject_p.h" +#include "qmetaobject_p.h" #include "qabstracteventdispatcher.h" #include "qcoreapplication.h" @@ -221,12 +222,42 @@ void QObjectPrivate::removePendingChildInsertedEvents(QObject *child) #endif +/*!\internal + For a given metaobject, compute the signal offset, and the method offset (including signals) +*/ +static void computeOffsets(const QMetaObject *metaobject, int *signalOffset, int *methodOffset) +{ + *signalOffset = *methodOffset = 0; + const QMetaObject *m = metaobject->d.superdata; + while (m) { + const QMetaObjectPrivate *d = QMetaObjectPrivate::get(m); + *methodOffset += d->methodCount; + *signalOffset += (d->revision >= 4) ? d->signalCount : d->methodCount; + /*Before Qt 4.6 (revision 4), the signalCount information was not generated by moc. + so for compatibility we consider all the method as slot for old moc output*/ + m = m->d.superdata; + } +} + +/*! \internal + This vector contains the all connections from an object. + + Each object may have one vector containing the lists of connections for a given signal. + The index in the vector correspond to the signal index. + The signal index is the one returned by QObjectPrivate::signalIndex (not QMetaObject::indexOfSignal). + Negative index means connections to all signals. + + This vector is protected by the object mutex (signalSlotMutexes()) + + Each Connection is also part of a 'senders' linked list. The mutex of the receiver must be locked when touching + the pointers of this linked list. +*/ class QObjectConnectionListVector : public QVector { public: - bool orphaned; - bool dirty; - int inUse; + bool orphaned; //the QObject owner of this vector has been destroyed while the vector was inUse + bool dirty; //some Connection have been disconnected (their receiver is 0) but not removed from the list yet + int inUse; //number of functions that are currently accessing this object or its connections QObjectPrivate::ConnectionList allsignals; QObjectConnectionListVector() @@ -251,7 +282,7 @@ public: bool QObjectPrivate::isSender(const QObject *receiver, const char *signal) const { Q_Q(const QObject); - int signal_index = q->metaObject()->indexOfSignal(signal); + int signal_index = signalIndex(signal); if (signal_index < 0) return false; QMutexLocker locker(signalSlotLock(q)); @@ -275,7 +306,7 @@ QObjectList QObjectPrivate::receiverList(const char *signal) const { Q_Q(const QObject); QObjectList returnValue; - int signal_index = q->metaObject()->indexOfSignal(signal); + int signal_index = signalIndex(signal); if (signal_index < 0) return returnValue; QMutexLocker locker(signalSlotLock(q)); @@ -2417,6 +2448,7 @@ QObject *QObject::sender() const int QObject::receivers(const char *signal) const { + Q_D(const QObject); int receivers = 0; if (signal) { QByteArray signal_name = QMetaObject::normalizedSignature(signal); @@ -2426,8 +2458,7 @@ int QObject::receivers(const char *signal) const return 0; #endif signal++; // skip code - const QMetaObject *smeta = this->metaObject(); - int signal_index = smeta->indexOfSignal(signal); + int signal_index = d->signalIndex(signal); if (signal_index < 0) { #ifndef QT_NO_DEBUG err_method_notfound(this, signal-1, "receivers"); @@ -2551,19 +2582,26 @@ bool QObject::connect(const QObject *sender, const char *signal, const QMetaObject *smeta = sender->metaObject(); const char *signal_arg = signal; ++signal; //skip code - int signal_index = smeta->indexOfSignal(signal); + int signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal); if (signal_index < 0) { // check for normalized signatures tmp_signal_name = QMetaObject::normalizedSignature(signal - 1); signal = tmp_signal_name.constData() + 1; - signal_index = smeta->indexOfSignal(signal); + smeta = sender->metaObject(); + signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal); + if (signal_index < 0) { err_method_notfound(sender, signal_arg, "connect"); err_info_about_objects("connect", sender, receiver); return false; } } + signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index); + int signalOffset, methodOffset; + computeOffsets(smeta, &signalOffset, &methodOffset); + int signal_absolute_index = signal_index + methodOffset; + signal_index += signalOffset; QByteArray tmp_method_name; int membcode = extract_code(method); @@ -2612,12 +2650,12 @@ bool QObject::connect(const QObject *sender, const char *signal, int *types = 0; if ((type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection) - && !(types = queuedConnectionTypes(smeta->method(signal_index).parameterTypes()))) + && !(types = queuedConnectionTypes(smeta->method(signal_absolute_index).parameterTypes()))) return false; #ifndef QT_NO_DEBUG { - QMetaMethod smethod = smeta->method(signal_index); + QMetaMethod smethod = smeta->method(signal_absolute_index); QMetaMethod rmethod = rmeta->method(method_index); if (warnCompat) { if(smethod.attributes() & QMetaMethod::Compatibility) { @@ -2630,7 +2668,7 @@ bool QObject::connect(const QObject *sender, const char *signal, } } #endif - if (!QMetaObject::connect(sender, signal_index, receiver, method_index, type, types)) + if (!QMetaObjectPrivate::connect(sender, signal_index, receiver, method_index, type, types)) return false; const_cast(sender)->connectNotify(signal - 1); return true; @@ -2761,14 +2799,18 @@ bool QObject::disconnect(const QObject *sender, const char *signal, do { int signal_index = -1; if (signal) { - signal_index = smeta->indexOfSignal(signal); - if (signal_index < smeta->methodOffset()) - continue; + signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signal); + if (signal_index < 0) + break; + signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index); + int signalOffset, methodOffset; + computeOffsets(smeta, &signalOffset, &methodOffset); + signal_index += signalOffset; signal_found = true; } if (!method) { - res |= QMetaObject::disconnect(sender, signal_index, receiver, -1); + res |= QMetaObjectPrivate::disconnect(sender, signal_index, receiver, -1); } else { const QMetaObject *rmeta = receiver->metaObject(); do { @@ -2778,7 +2820,7 @@ bool QObject::disconnect(const QObject *sender, const char *signal, rmeta = rmeta->superClass(); if (method_index < 0) break; - res |= QMetaObject::disconnect(sender, signal_index, receiver, method_index); + res |= QMetaObjectPrivate::disconnect(sender, signal_index, receiver, method_index); method_found = true; } while ((rmeta = rmeta->superClass())); } @@ -2869,16 +2911,31 @@ void QObject::disconnectNotify(const char *) } /*!\internal + \a types is a 0-terminated vector of meta types for queued + connections. - \a types is a 0-terminated vector of meta types for queued - connections. - - if \a signal_index is -1, then we effectively connect *all* signals - from the sender to the receiver's slot -*/ + if \a signal_index is -1, then we effectively connect *all* signals + from the sender to the receiver's slot + */ bool QMetaObject::connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type, int *types) { + const QMetaObject *mo = sender->metaObject(); + while (mo && mo->methodOffset() > signal_index) + mo = mo->superClass(); + int signalOffset, methodOffset; + computeOffsets(mo, &signalOffset, &methodOffset); + signal_index = QMetaObjectPrivate::originalClone(mo, signal_index - methodOffset) + signalOffset; + return QMetaObjectPrivate::connect(sender, signal_index, + receiver, method_index, type, types); +} + +/*! \internal + Same as the QMetaObject::connect, but \a signal_index must be the result of QObjectPrivate::signalIndex + */ +bool QMetaObjectPrivate::connect(const QObject *sender, int signal_index, + const QObject *receiver, int method_index, int type, int *types) +{ QObject *s = const_cast(sender); QObject *r = const_cast(receiver); @@ -2886,7 +2943,7 @@ bool QMetaObject::connect(const QObject *sender, int signal_index, signalSlotLock(receiver)); if (type & Qt::UniqueConnection) { - QObjectConnectionListVector *connectionLists = s->d_func()->connectionLists; + QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists; if (connectionLists && connectionLists->count() > signal_index) { const QObjectPrivate::Connection *c2 = (*connectionLists)[signal_index].first; @@ -2907,25 +2964,25 @@ bool QMetaObject::connect(const QObject *sender, int signal_index, c->connectionType = type; c->argumentTypes = types; c->nextConnectionList = 0; - c->prev = &r->d_func()->senders; + c->prev = &(QObjectPrivate::get(r)->senders); c->next = *c->prev; *c->prev = c; if (c->next) c->next->prev = &c->next; - s->d_func()->addConnection(signal_index, c); + QObjectPrivate::get(s)->addConnection(signal_index, c); + QObjectPrivate *const sender_d = QObjectPrivate::get(s); if (signal_index < 0) { - for (uint i = 0; i < (sizeof sender->d_func()->connectedSignals - / sizeof sender->d_func()->connectedSignals[0] ); ++i) - sender->d_func()->connectedSignals[i] = ~0u; - } else if (signal_index < (int)sizeof sender->d_func()->connectedSignals * 8) { - uint n = (signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - sender->d_func()->connectedSignals[n] |= (1 << (signal_index - n * 8 - * sizeof sender->d_func()->connectedSignals[0])); + for (uint i = 0; i < (sizeof sender_d->connectedSignals + / sizeof sender_d->connectedSignals[0] ); ++i) + sender_d->connectedSignals[i] = ~0u; + } else if (signal_index < (int)sizeof sender_d->connectedSignals * 8) { + uint n = (signal_index / (8 * sizeof sender_d->connectedSignals[0])); + sender_d->connectedSignals[n] |= (1 << (signal_index - n * 8 + * sizeof sender_d->connectedSignals[0])); } - return true; } @@ -2935,6 +2992,22 @@ bool QMetaObject::connect(const QObject *sender, int signal_index, bool QMetaObject::disconnect(const QObject *sender, int signal_index, const QObject *receiver, int method_index) { + const QMetaObject *mo = sender->metaObject(); + while (mo && mo->methodOffset() > signal_index) + mo = mo->superClass(); + int signalOffset, methodOffset; + computeOffsets(mo, &signalOffset, &methodOffset); + signal_index = QMetaObjectPrivate::originalClone(mo, signal_index - methodOffset) + signalOffset; + return QMetaObjectPrivate::disconnect(sender, signal_index, + receiver, method_index); +} + +/*! \internal + Same as the QMetaObject::disconnect, but \a signal_index must be the result of QObjectPrivate::signalIndex + */ +bool QMetaObjectPrivate::disconnect(const QObject *sender, int signal_index, + const QObject *receiver, int method_index) +{ if (!sender) return false; @@ -2945,7 +3018,7 @@ bool QMetaObject::disconnect(const QObject *sender, int signal_index, QMutex *receiverMutex = receiver ? signalSlotLock(receiver) : 0; QOrderedMutexLocker locker(senderMutex, receiverMutex); - QObjectConnectionListVector *connectionLists = s->d_func()->connectionLists; + QObjectConnectionListVector *connectionLists = QObjectPrivate::get(s)->connectionLists; if (!connectionLists) return false; @@ -3056,23 +3129,26 @@ void QMetaObject::connectSlotsByName(QObject *o) int len = objName.length(); if (!len || qstrncmp(slot + 3, objName.data(), len) || slot[len+3] != '_') continue; - const QMetaObject *smo = co->metaObject(); - int sigIndex = smo->indexOfMethod(slot + len + 4); + int sigIndex = co->d_func()->signalIndex(slot + len + 4); if (sigIndex < 0) { // search for compatible signals + const QMetaObject *smo = co->metaObject(); int slotlen = qstrlen(slot + len + 4) - 1; for (int k = 0; k < co->metaObject()->methodCount(); ++k) { - if (smo->method(k).methodType() != QMetaMethod::Signal) + QMetaMethod method = smo->method(k); + if (method.methodType() != QMetaMethod::Signal) continue; - if (!qstrncmp(smo->method(k).signature(), slot + len + 4, slotlen)) { - sigIndex = k; + if (!qstrncmp(method.signature(), slot + len + 4, slotlen)) { + int signalOffset, methodOffset; + computeOffsets(method.enclosingMetaObject(), &signalOffset, &methodOffset); + sigIndex = k + - methodOffset + signalOffset; break; } } } if (sigIndex < 0) continue; - if (QMetaObject::connect(co, sigIndex, o, i)) { + if (QMetaObjectPrivate::connect(co, sigIndex, o, i)) { foundIt = true; break; } @@ -3142,15 +3218,43 @@ static void blocking_activate(QObject *sender, int signal, QObjectPrivate::Conne } /*!\internal + \obsolete. + Used to be called from QMetaObject::activate(QObject *, QMetaObject *, int, int, void **) */ void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal_index, void **argv) { + int offset = sender->metaObject()->methodOffset(); + activate(sender, from_signal_index - offset, to_signal_index - offset, argv); +} + +/*!\internal + */ +void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index, + void **argv) +{ + int signalOffset; + int methodOffset; + computeOffsets(m, &signalOffset, &methodOffset); + + int signal_index = signalOffset + local_signal_index; + if (signal_index < (int)sizeof(sender->d_func()->connectedSignals) * 8 + && !qt_signal_spy_callback_set.signal_begin_callback + && !qt_signal_spy_callback_set.signal_end_callback) { + uint n = (signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); + uint m = 1 << (signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); + if ((sender->d_func()->connectedSignals[n] & m) == 0) + // nothing connected to these signals, and no spy + return; + } + if (sender->d_func()->blockSig) return; + int signal_absolute_index = methodOffset + local_signal_index; + void *empty_argv[] = { 0 }; if (qt_signal_spy_callback_set.signal_begin_callback != 0) { - qt_signal_spy_callback_set.signal_begin_callback(sender, from_signal_index, + qt_signal_spy_callback_set.signal_begin_callback(sender, signal_absolute_index, argv ? argv : empty_argv); } @@ -3160,26 +3264,20 @@ void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal QObjectConnectionListVector *connectionLists = sender->d_func()->connectionLists; if (!connectionLists) { if (qt_signal_spy_callback_set.signal_end_callback != 0) - qt_signal_spy_callback_set.signal_end_callback(sender, from_signal_index); + qt_signal_spy_callback_set.signal_end_callback(sender, signal_absolute_index); return; } ++connectionLists->inUse; + if (signal_index >= connectionLists->count()) { + signal_index = -2; //for "all signals"; + } - // emit signals in the following order: from_signal_index <= signals <= to_signal_index, signal < 0 - for (int signal = from_signal_index; - (signal >= from_signal_index && signal <= to_signal_index) || (signal == -2); - (signal == to_signal_index ? signal = -2 : ++signal)) - { - if (signal >= connectionLists->count()) { - signal = to_signal_index; - continue; - } - - QObjectPrivate::Connection *c = connectionLists->at(signal).first; + do { + QObjectPrivate::Connection *c = connectionLists->at(signal_index).first; if (!c) continue; // We need to check against last here to ensure that signals added // during the signal emission are not emitted in this emission. - QObjectPrivate::Connection *last = connectionLists->at(signal).last; + QObjectPrivate::Connection *last = connectionLists->at(signal_index).last; do { if (!c->receiver) @@ -3193,17 +3291,17 @@ void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal && (currentThreadData != sender->d_func()->threadData || receiver->d_func()->threadData != sender->d_func()->threadData)) || (c->connectionType == Qt::QueuedConnection)) { - queued_activate(sender, signal, c, argv); + queued_activate(sender, signal_absolute_index, c, argv); continue; } else if (c->connectionType == Qt::BlockingQueuedConnection) { - blocking_activate(sender, signal, c, argv); + blocking_activate(sender, signal_absolute_index, c, argv); continue; } const int method = c->method; QObjectPrivate::Sender currentSender; currentSender.sender = sender; - currentSender.signal = signal < 0 ? from_signal_index : signal; + currentSender.signal = signal_absolute_index; currentSender.ref = 1; QObjectPrivate::Sender *previousSender = 0; if (currentThreadData == receiver->d_func()->threadData) @@ -3247,7 +3345,7 @@ void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal if (connectionLists->orphaned) break; - } + } while (signal_index >= 0 && (signal_index = -1)); //start over for -1 (all signal) --connectionLists->inUse; Q_ASSERT(connectionLists->inUse >= 0); @@ -3261,82 +3359,50 @@ void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal locker.unlock(); if (qt_signal_spy_callback_set.signal_end_callback != 0) - qt_signal_spy_callback_set.signal_end_callback(sender, from_signal_index); -} + qt_signal_spy_callback_set.signal_end_callback(sender, signal_absolute_index); +} /*!\internal - */ + Obsolete. (signal_index comes from indexOfMethod()) +*/ void QMetaObject::activate(QObject *sender, int signal_index, void **argv) { - if (signal_index < (int)sizeof(sender->d_func()->connectedSignals) * 8 - && !qt_signal_spy_callback_set.signal_begin_callback - && !qt_signal_spy_callback_set.signal_end_callback) { - uint n = (signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - uint m = 1 << (signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); - if ((sender->d_func()->connectedSignals[n] & m) == 0) - // nothing connected to these signals, and no spy - return; - } - activate(sender, signal_index, signal_index, argv); -} - -/*!\internal - */ -void QMetaObject::activate(QObject *sender, const QMetaObject *m, int local_signal_index, - void **argv) -{ - int signal_index = m->methodOffset() + local_signal_index; - if (signal_index < (int)sizeof(sender->d_func()->connectedSignals) * 8 - && !qt_signal_spy_callback_set.signal_begin_callback - && !qt_signal_spy_callback_set.signal_end_callback) { - uint n = (signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - uint m = 1 << (signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); - if ((sender->d_func()->connectedSignals[n] & m) == 0) - // nothing connected to these signals, and no spy - return; - } - activate(sender, signal_index, signal_index, argv); + const QMetaObject *mo = sender->metaObject(); + while (mo && mo->methodOffset() > signal_index) + mo = mo->superClass(); + activate(sender, signal_index - mo->methodOffset(), argv); } /*!\internal + Obsolete, called by moc generated code before Qt 4.6 for cloned signals + But since Qt 4.6, all clones are connected to their original */ void QMetaObject::activate(QObject *sender, const QMetaObject *m, int from_local_signal_index, int to_local_signal_index, void **argv) { - Q_ASSERT(from_local_signal_index <= to_local_signal_index); - int offset = m->methodOffset(); - int from_signal_index = offset + from_local_signal_index; - int to_signal_index = offset + to_local_signal_index; + Q_UNUSED(to_local_signal_index); + Q_ASSERT(from_local_signal_index == QMetaObjectPrivate::originalClone(m, to_local_signal_index)); + activate(sender, m, from_local_signal_index, argv); +} - if (to_signal_index < (int)sizeof(sender->d_func()->connectedSignals) * 8 - && !qt_signal_spy_callback_set.signal_begin_callback - && !qt_signal_spy_callback_set.signal_end_callback) { +/*! \internal + Returns the signal index used in the internal connectionLists vector. - uint n = (from_signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - uint m = 1 << (from_signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); - uint nt = (to_signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - uint mt = 1 << (to_signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); - bool connected = false; - quint32 *connectedSignals = sender->d_func()->connectedSignals; - for (uint i = 0; !connected && i < (sizeof sender->d_func()->connectedSignals - / sizeof sender->d_func()->connectedSignals[0]); ++i) { - uint mask = 0; - if (i > n) - mask = ~0u; - else if (i == n) - mask = ~(m -1); - if (i > nt) - mask = 0; - else if (i == nt) - mask &= (mt << 1) - 1; - connected = connectedSignals[i] & mask; - } - if (!connected) - // nothing connected to these signals, and no spy - return; - } - activate(sender, from_signal_index, to_signal_index, argv); + It is different from QMetaObject::indexOfSignal(): indexOfSignal is the same as indexOfMethod + while QObjectPrivate::signalIndex is smaller because it doesn't give index to slots. +*/ +int QObjectPrivate::signalIndex(const char *signalName) const +{ + Q_Q(const QObject); + const QMetaObject *base = q->metaObject(); + int relative_index = QMetaObjectPrivate::indexOfSignalRelative(&base, signalName); + if (relative_index < 0) + return relative_index; + relative_index = QMetaObjectPrivate::originalClone(base, relative_index); + int signalOffset, methodOffset; + computeOffsets(base, &signalOffset, &methodOffset); + return relative_index + signalOffset; } /*! \internal @@ -3344,14 +3410,16 @@ void QMetaObject::activate(QObject *sender, const QMetaObject *m, Returns true if the signal with index \a signal_index from object \a sender is connected. Signals with indices above a certain range are always considered connected (see connectedSignals in QObjectPrivate). If a signal spy is installed, all signals are considered connected. + + \a signal_index must be the index returned by QObjectPrivate::signalIndex; */ -bool QMetaObject::isConnected(QObject *sender, int signal_index) { - if (signal_index < (int)sizeof(sender->d_func()->connectedSignals) * 8 +bool QObjectPrivate::isSignalConnected(int signal_index) const { + if (signal_index < (int)sizeof(connectedSignals) * 8 && !qt_signal_spy_callback_set.signal_begin_callback && !qt_signal_spy_callback_set.signal_end_callback) { - uint n = (signal_index / (8 * sizeof sender->d_func()->connectedSignals[0])); - uint m = 1 << (signal_index - n * 8 * sizeof sender->d_func()->connectedSignals[0]); - if ((sender->d_func()->connectedSignals[n] & m) == 0) + uint n = (signal_index / (8 * sizeof connectedSignals[0])); + uint m = 1 << (signal_index - n * 8 * sizeof connectedSignals[0]); + if ((connectedSignals[n] & m) == 0) // nothing connected to these signals, and no spy return false; } @@ -3557,8 +3625,21 @@ void QObject::dumpObjectInfo() qDebug(" SIGNALS OUT"); if (d->connectionLists) { + int offset = 0; + int offsetToNextMetaObject = 0; for (int signal_index = 0; signal_index < d->connectionLists->count(); ++signal_index) { - const QMetaMethod signal = metaObject()->method(signal_index); + if (signal_index >= offsetToNextMetaObject) { + const QMetaObject *mo = metaObject(); + int signalOffset, methodOffset; + computeOffsets(mo, &signalOffset, &methodOffset); + while (mo && signalOffset > signal_index) { + mo = mo->superClass(); + offsetToNextMetaObject = signalOffset; + computeOffsets(mo, &signalOffset, &methodOffset); + } + offset = offset - signalOffset + methodOffset; + } + const QMetaMethod signal = metaObject()->method(signal_index + offset); qDebug(" signal: %s", signal.signature()); // receivers diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index 49d8b63..9721ef8 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -169,6 +169,9 @@ public: return o->d_func(); } + int signalIndex(const char *signalName) const; + bool isSignalConnected(int signalIdx) const; + public: QString objectName; ExtraData *extraData; // extra data set by the user diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index befd596..421617a 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -330,12 +330,10 @@ struct Q_CORE_EXPORT QMetaObject static void connectSlotsByName(QObject *o); // internal index-based signal activation - static void activate(QObject *sender, int signal_index, void **argv); - static void activate(QObject *sender, int from_signal_index, int to_signal_index, void **argv); + static void activate(QObject *sender, int signal_index, void **argv); //obsolete + static void activate(QObject *sender, int from_signal_index, int to_signal_index, void **argv); //obsolete static void activate(QObject *sender, const QMetaObject *, int local_signal_index, void **argv); - static void activate(QObject *sender, const QMetaObject *, int from_local_signal_index, int to_local_signal_index, void **argv); - - static bool isConnected(QObject *sender, int signal_index); + static void activate(QObject *sender, const QMetaObject *, int from_local_signal_index, int to_local_signal_index, void **argv); //obsolete // internal guarded pointers static void addGuard(QObject **ptr); diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 675fc0d..708ba09 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -6589,7 +6589,7 @@ void QGraphicsItem::prepareGeometryChange() // if someone is connected to the changed signal or the scene has no views. // Note that this has to be done *after* markDirty to ensure that // _q_processDirtyItems is called before _q_emitUpdated. - if ((scenePrivate->connectedSignals[0] & scenePrivate->changedSignalMask) + if (scenePrivate->isSignalConnected(scenePrivate->changedSignalIndex) || scenePrivate->views.isEmpty()) { if (d_ptr->hasTranslateOnlySceneTransform()) { d_ptr->scene->update(boundingRect().translated(d_ptr->sceneTransform.dx(), diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp index 3f13a86..7fa565d 100644 --- a/src/gui/graphicsview/qgraphicsscene.cpp +++ b/src/gui/graphicsview/qgraphicsscene.cpp @@ -265,12 +265,13 @@ static void _q_hoverFromMouseEvent(QGraphicsSceneHoverEvent *hover, const QGraph hover->setAccepted(mouseEvent->isAccepted()); } +int QGraphicsScenePrivate::changedSignalIndex; + /*! \internal */ QGraphicsScenePrivate::QGraphicsScenePrivate() - : changedSignalMask(0), - indexMethod(QGraphicsScene::BspTreeIndex), + : indexMethod(QGraphicsScene::BspTreeIndex), index(0), lastItemCount(0), hasSceneRect(false), @@ -311,7 +312,9 @@ void QGraphicsScenePrivate::init() index = new QGraphicsSceneBspTreeIndex(q); // Keep this index so we can check for connected slots later on. - changedSignalMask = (1 << q->metaObject()->indexOfSignal("changed(QList)")); + if (!changedSignalIndex) { + changedSignalIndex = signalIndex("changed(QList)"); + } qApp->d_func()->scene_list.append(q); q->update(); } @@ -343,7 +346,7 @@ void QGraphicsScenePrivate::_q_emitUpdated() // the optimization that items send updates directly to the views, but it // needs to happen in order to keep compatibility with the behavior from // Qt 4.4 and backward. - if (connectedSignals[0] & changedSignalMask) { + if (isSignalConnected(changedSignalIndex)) { for (int i = 0; i < views.size(); ++i) { QGraphicsView *view = views.at(i); if (!view->d_func()->connectedToScene) { @@ -2894,7 +2897,7 @@ void QGraphicsScene::update(const QRectF &rect) // Check if anyone's connected; if not, we can send updates directly to // the views. Otherwise or if there are no views, use old behavior. - bool directUpdates = !(d->connectedSignals[0] & d->changedSignalMask) && !d->views.isEmpty(); + bool directUpdates = !(d->isSignalConnected(d->changedSignalIndex)) && !d->views.isEmpty(); if (rect.isNull()) { d->updateAll = true; d->updatedRects.clear(); @@ -4473,7 +4476,7 @@ void QGraphicsScenePrivate::markDirty(QGraphicsItem *item, const QRectF &rect, b if (removingItemFromScene) { // Note that this function can be called from the item's destructor, so // do NOT call any virtual functions on it within this block. - if ((connectedSignals[0] & changedSignalMask) || views.isEmpty()) { + if (isSignalConnected(changedSignalIndex) || views.isEmpty()) { // This block of code is kept for compatibility. Since 4.5, by default // QGraphicsView does not connect the signal and we use the below // method of delivering updates. @@ -4619,7 +4622,7 @@ void QGraphicsScenePrivate::processDirtyItemsRecursive(QGraphicsItem *item, bool // Process item. if (item->d_ptr->dirty || item->d_ptr->paintedViewBoundingRectsNeedRepaint) { - const bool useCompatUpdate = views.isEmpty() || (connectedSignals[0] & changedSignalMask); + const bool useCompatUpdate = views.isEmpty() || isSignalConnected(changedSignalIndex); const QRectF itemBoundingRect = adjustedItemBoundingRect(item); if (useCompatUpdate && !itemIsUntransformable && qFuzzyIsNull(item->boundingRegionGranularity())) { diff --git a/src/gui/graphicsview/qgraphicsscene_p.h b/src/gui/graphicsview/qgraphicsscene_p.h index 685f534..f1ddb5a 100644 --- a/src/gui/graphicsview/qgraphicsscene_p.h +++ b/src/gui/graphicsview/qgraphicsscene_p.h @@ -87,7 +87,7 @@ public: static QGraphicsScenePrivate *get(QGraphicsScene *q); - quint32 changedSignalMask; + static int changedSignalIndex; QGraphicsScene::ItemIndexMethod indexMethod; QGraphicsSceneIndex *index; diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index f19c94b..cc6fa88 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -45,44 +45,9 @@ #include #include -QT_BEGIN_NAMESPACE +#include //for the flags. -// if the flags change, you MUST to change it in qmetaobject.cpp too -enum PropertyFlags { - Invalid = 0x00000000, - Readable = 0x00000001, - Writable = 0x00000002, - Resettable = 0x00000004, - EnumOrFlag = 0x00000008, - StdCppSet = 0x00000100, -// Override = 0x00000200, - Constant = 0x00000400, - Final = 0x00000800, - Designable = 0x00001000, - ResolveDesignable = 0x00002000, - Scriptable = 0x00004000, - ResolveScriptable = 0x00008000, - Stored = 0x00010000, - ResolveStored = 0x00020000, - Editable = 0x00040000, - ResolveEditable = 0x00080000, - User = 0x00100000, - ResolveUser = 0x00200000, - Notify = 0x00400000 -}; - -enum MethodFlags { - AccessPrivate = 0x00, - AccessProtected = 0x01, - AccessPublic = 0x02, - MethodMethod = 0x00, - MethodSignal = 0x04, - MethodSlot = 0x08, - MethodConstructor = 0x0c, - MethodCompatibility = 0x10, - MethodCloned = 0x20, - MethodScriptable = 0x40 -}; +QT_BEGIN_NAMESPACE uint qvariant_nameToType(const char* name) { @@ -205,10 +170,10 @@ void Generator::generateCode() QByteArray qualifiedClassNameIdentifier = cdef->qualified; qualifiedClassNameIdentifier.replace(':', '_'); - int index = 13; + int index = 14; fprintf(out, "static const uint qt_meta_data_%s[] = {\n", qualifiedClassNameIdentifier.constData()); fprintf(out, "\n // content:\n"); - fprintf(out, " %4d, // revision\n", 3); + fprintf(out, " %4d, // revision\n", 4); fprintf(out, " %4d, // classname\n", strreg(cdef->qualified)); fprintf(out, " %4d, %4d, // classinfo\n", cdef->classInfoList.count(), cdef->classInfoList.count() ? index : 0); index += cdef->classInfoList.count() * 2; @@ -229,6 +194,7 @@ void Generator::generateCode() isConstructible ? index : 0); fprintf(out, " %4d, // flags\n", 0); + fprintf(out, " %4d, // signalCount\n", cdef->signalList.count()); // @@ -1016,14 +982,7 @@ void Generator::generateSignal(FunctionDef *def,int index) else fprintf(out, ", const_cast(reinterpret_cast(&_t%d))", i); fprintf(out, " };\n"); - int n = 0; - for (i = 0; i < def->arguments.count(); ++i) - if (def->arguments.at(i).isDefault) - ++n; - if (n) - fprintf(out, " QMetaObject::activate(%s, &staticMetaObject, %d, %d, _a);\n", thisPtr.constData(), index, index + n); - else - fprintf(out, " QMetaObject::activate(%s, &staticMetaObject, %d, _a);\n", thisPtr.constData(), index); + fprintf(out, " QMetaObject::activate(%s, &staticMetaObject, %d, _a);\n", thisPtr.constData(), index); if (def->normalizedType.size()) fprintf(out, " return _t0;\n"); fprintf(out, "}\n"); -- cgit v0.12