diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-07-26 16:17:53 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-08-10 22:04:11 +0200 |
commit | b7d073e9905bf9812ba96cecdcf6871a95517d30 (patch) | |
tree | ab48181deddd93f40cff5e908f243c44efc359c5 /src/corelib/kernel/qmetaobject.cpp | |
parent | e75aed1a96e626f079af307c5604ddf9054ecafc (diff) |
Refactor memory allocation for arguments of QMetaCallEvents
There are two cases:
In a BlockingQueuedConnection, QMetaCallEvent doesn't allocate memory
and instead passes already existing pointers through. A QSemaphore
is used to serialize data access between threads. So the constructor
taking a QSemaphore can be simplified to only accept an existing arg
array.
In a QueuedConnection, QMetaCallEvent needs to make deep copies of
the arguments, and memory needs to be allocated based on the number
of arguments. The previous code put the burden of memory allocation
on the code generating the event, while the memory was free'd by
~QMetaCallEvent. Instead, make it QMetaCallEvent's responsibility
to allocate and free the memory as needed, and adjust the code
generating QMetaCallEvents.
We can allocate the memory for types and pointers to arguments in a
single block, starting with the space for the array of void*, followed
by the space for the array of integers to avoid byte alignment issues.
By pre-allocating the space that's needed by three arguments, we can
avoid all mallocs for the majority of QMetaCallEvents.
Until this change has propagated through qt5.git, we need to keep the
old API that is still used by QtDeclarative around. Once QtDeclarative
has migrated to the new API, it can be removed.
Change-Id: Id7359ffc14897237ea9672dabae9ef199a821907
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib/kernel/qmetaobject.cpp')
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index acb1f54bdf..f366d2fe49 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -1542,21 +1542,14 @@ bool QMetaObject::invokeMethodImpl(QObject *object, QtPrivate::QSlotObjectBase * return false; } - // args and typesCopy will be deallocated by ~QMetaCallEvent() using free() - void **args = static_cast<void **>(calloc(1, sizeof(void *))); - Q_CHECK_PTR(args); - - int *types = static_cast<int *>(calloc(1, sizeof(int))); - Q_CHECK_PTR(types); - - QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, 1, types, args)); + QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, 1)); } else if (type == Qt::BlockingQueuedConnection) { #if QT_CONFIG(thread) if (currentThread == objectThread) qWarning("QMetaObject::invokeMethod: Dead lock detected"); QSemaphore semaphore; - QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, 0, 0, argv, &semaphore)); + QCoreApplication::postEvent(object, new QMetaCallEvent(slot, 0, -1, argv, &semaphore)); semaphore.acquire(); #endif // QT_CONFIG(thread) } else { @@ -2310,42 +2303,31 @@ bool QMetaMethod::invoke(QObject *object, return false; } - int nargs = 1; // include return type - void **args = (void **) malloc(paramCount * sizeof(void *)); - Q_CHECK_PTR(args); - int *types = (int *) malloc(paramCount * sizeof(int)); - Q_CHECK_PTR(types); - types[0] = 0; // return type - args[0] = 0; + QScopedPointer<QMetaCallEvent> event(new QMetaCallEvent(idx_offset, idx_relative, callFunction, 0, -1, paramCount)); + int *types = event->types(); + void **args = event->args(); + int argIndex = 0; for (int i = 1; i < paramCount; ++i) { types[i] = QMetaType::type(typeNames[i]); if (types[i] == QMetaType::UnknownType && param[i]) { // Try to register the type and try again before reporting an error. - int index = nargs - 1; - void *argv[] = { &types[i], &index }; + void *argv[] = { &types[i], &argIndex }; QMetaObject::metacall(object, QMetaObject::RegisterMethodArgumentMetaType, idx_relative + idx_offset, argv); if (types[i] == -1) { qWarning("QMetaMethod::invoke: Unable to handle unregistered datatype '%s'", typeNames[i]); - for (int x = 1; x < i; ++x) { - if (types[x] && args[x]) - QMetaType::destroy(types[x], args[x]); - } - free(types); - free(args); return false; } } if (types[i] != QMetaType::UnknownType) { args[i] = QMetaType::create(types[i], param[i]); - ++nargs; + ++argIndex; } } - QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction, - 0, -1, nargs, types, args)); + QCoreApplication::postEvent(object, event.take()); } else { // blocking queued connection #if QT_CONFIG(thread) QThread *currentThread = QThread::currentThread(); @@ -2358,7 +2340,7 @@ bool QMetaMethod::invoke(QObject *object, QSemaphore semaphore; QCoreApplication::postEvent(object, new QMetaCallEvent(idx_offset, idx_relative, callFunction, - 0, -1, 0, 0, param, &semaphore)); + 0, -1, param, &semaphore)); semaphore.acquire(); #endif // QT_CONFIG(thread) } |