From 2b98dd7645544c27ed0a363810eb784aa53d293f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Mar 2023 20:53:37 -0700 Subject: [PATCH] QProcessPrivate: repack and reorganize the members Reduce the number of #ifdef blocks and use quint8 for the enums that don't need more than 8 bits anyway (none of them do). Plus move the std::function callback to an indirect block, as most users of QProcess won't set them and this type is 4 pointers with libstdc++ and libc++. After this, QProcessPrivate on 64-bit Unix is 688 bytes, of which: - 392 bytes from QIODevicePrivate - 295 bytes of own data - 3x56 bytes per Channel (which have 5 bytes of tail padding each) - 1 byte of tail padding and no middle padding Pick-to: 6.5 Change-Id: Icfe44ecf285a480fafe4fffd174d188a0821d060 Reviewed-by: Volker Hilsheimer --- src/corelib/io/qprocess.cpp | 20 +++++++------ src/corelib/io/qprocess_p.h | 48 ++++++++++++++------------------ src/corelib/io/qprocess_unix.cpp | 6 ++-- 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 116bb78b8a..41a7fffd8b 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -922,7 +922,7 @@ void QProcessPrivate::setErrorAndEmit(QProcess::ProcessError error, const QStrin Q_Q(QProcess); Q_ASSERT(error != QProcess::UnknownError); setError(error, description); - emit q->errorOccurred(processError); + emit q->errorOccurred(QProcess::ProcessError(processError)); } /*! @@ -1154,7 +1154,7 @@ void QProcessPrivate::processFinished() //emit q->standardOutputClosed(); //emit q->standardErrorClosed(); - emit q->finished(exitCode, exitStatus); + emit q->finished(exitCode, QProcess::ExitStatus(exitStatus)); #if defined QPROCESS_DEBUG qDebug("QProcessPrivate::processFinished(): process is dead"); @@ -1239,7 +1239,7 @@ QProcess::~QProcess() QProcess::ProcessChannelMode QProcess::processChannelMode() const { Q_D(const QProcess); - return d->processChannelMode; + return ProcessChannelMode(d->processChannelMode); } /*! @@ -1269,7 +1269,7 @@ void QProcess::setProcessChannelMode(ProcessChannelMode mode) QProcess::InputChannelMode QProcess::inputChannelMode() const { Q_D(const QProcess); - return d->inputChannelMode; + return InputChannelMode(d->inputChannelMode); } /*! @@ -1558,7 +1558,7 @@ void QProcess::setCreateProcessArgumentsModifier(CreateProcessArgumentModifier m std::function QProcess::childProcessModifier() const { Q_D(const QProcess); - return d->childProcessModifier; + return d->unixExtras ? d->unixExtras->childProcessModifier : std::function(); } /*! @@ -1602,7 +1602,9 @@ std::function QProcess::childProcessModifier() const void QProcess::setChildProcessModifier(const std::function &modifier) { Q_D(QProcess); - d->childProcessModifier = modifier; + if (!d->unixExtras) + d->unixExtras.reset(new QProcessPrivate::UnixExtras); + d->unixExtras->childProcessModifier = modifier; } #endif @@ -1693,7 +1695,7 @@ qint64 QProcess::bytesToWrite() const QProcess::ProcessError QProcess::error() const { Q_D(const QProcess); - return d->processError; + return ProcessError(d->processError); } /*! @@ -1704,7 +1706,7 @@ QProcess::ProcessError QProcess::error() const QProcess::ProcessState QProcess::state() const { Q_D(const QProcess); - return d->processState; + return ProcessState(d->processState); } /*! @@ -2373,7 +2375,7 @@ int QProcess::exitCode() const QProcess::ExitStatus QProcess::exitStatus() const { Q_D(const QProcess); - return d->exitStatus; + return ExitStatus(d->exitStatus); } /*! diff --git a/src/corelib/io/qprocess_p.h b/src/corelib/io/qprocess_p.h index e6f279d5f3..6dfc4565fb 100644 --- a/src/corelib/io/qprocess_p.h +++ b/src/corelib/io/qprocess_p.h @@ -259,20 +259,6 @@ public: bool _q_startupNotification(); void _q_processDied(); - QProcess::ProcessChannelMode processChannelMode = QProcess::SeparateChannels; - QProcess::InputChannelMode inputChannelMode = QProcess::ManagedInputChannel; - QProcess::ProcessError processError = QProcess::UnknownError; - QProcess::ProcessState processState = QProcess::NotRunning; - QString workingDirectory; -#ifdef Q_OS_WIN - Q_PROCESS_INFORMATION *pid = nullptr; -#else - qint64 pid = 0; -#endif - - bool emittedReadyRead = false; - bool emittedBytesWritten = false; - Channel stdinChannel; Channel stdoutChannel; Channel stderrChannel; @@ -286,22 +272,33 @@ public: QString program; QStringList arguments; + QString workingDirectory; + QProcessEnvironment environment = QProcessEnvironment::InheritFromParent; #if defined(Q_OS_WIN) QString nativeArguments; QProcess::CreateProcessArgumentModifier modifyCreateProcessArgs; -#else - std::function childProcessModifier; -#endif - QProcessEnvironment environment = QProcessEnvironment::InheritFromParent; - -#ifdef Q_OS_UNIX - Q_PIPE childStartedPipe[2] = {INVALID_Q_PIPE, INVALID_Q_PIPE}; - QSocketNotifier *stateNotifier = nullptr; - int forkfd = -1; -#else QWinEventNotifier *processFinishedNotifier = nullptr; + Q_PROCESS_INFORMATION *pid = nullptr; +#else + struct UnixExtras { + std::function childProcessModifier; + }; + std::unique_ptr unixExtras; + qint64 pid = 0; + QSocketNotifier *stateNotifier = nullptr; + Q_PIPE childStartedPipe[2] = {INVALID_Q_PIPE, INVALID_Q_PIPE}; + int forkfd = -1; #endif + int exitCode = 0; + quint8 processState = QProcess::NotRunning; + quint8 exitStatus = QProcess::NormalExit; + quint8 processError = QProcess::UnknownError; + quint8 processChannelMode = QProcess::SeparateChannels; + quint8 inputChannelMode = QProcess::ManagedInputChannel; + bool emittedReadyRead = false; + bool emittedBytesWritten = false; + void start(QIODevice::OpenMode mode); void startProcess(); #if defined(Q_OS_UNIX) @@ -325,9 +322,6 @@ public: bool startDetached(qint64 *pPid); - int exitCode = 0; - QProcess::ExitStatus exitStatus = QProcess::NormalExit; - bool waitForStarted(const QDeadlineTimer &deadline); bool waitForReadyRead(const QDeadlineTimer &deadline); bool waitForBytesWritten(const QDeadlineTimer &deadline); diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index 3ffe8b28e2..741cabe6fe 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -544,8 +544,10 @@ void QProcessPrivate::execChild(const char *workingDir, char **argv, char **envp goto report_errno; } - if (childProcessModifier) - childProcessModifier(); + if (unixExtras) { + if (unixExtras->childProcessModifier) + unixExtras->childProcessModifier(); + } // execute the process if (!envp) {