From 11e93372df2ebe8700ebb39fca3709e3a6c3d399 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 9 Oct 2013 11:40:05 -0700 Subject: Make the localHostName() copy function return QByteArray This avoids one extra memory allocation when creating the lock file. The number of memory allocations when checking the file are still the same. Change-Id: I16a2fdb7a5458bdc66f8ad1c602582b5698a5b5c Reviewed-by: Oswald Buddenhagen Reviewed-by: Kevin Ottens Reviewed-by: Thiago Macieira --- src/corelib/io/qlockfile_unix.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qlockfile_unix.cpp b/src/corelib/io/qlockfile_unix.cpp index dc8817706c..d1dbb51e76 100644 --- a/src/corelib/io/qlockfile_unix.cpp +++ b/src/corelib/io/qlockfile_unix.cpp @@ -57,13 +57,13 @@ QT_BEGIN_NAMESPACE -static QString localHostName() // from QHostInfo::localHostName() +static QByteArray localHostName() // from QHostInfo::localHostName(), modified to return a QByteArray { - char hostName[512]; - if (gethostname(hostName, sizeof(hostName)) == -1) - return QString(); - hostName[sizeof(hostName) - 1] = '\0'; - return QString::fromLocal8Bit(hostName); + QByteArray hostName(512, Qt::Uninitialized); + if (gethostname(hostName.data(), hostName.size()) == -1) + return QByteArray(); + hostName.truncate(strlen(hostName.data())); + return hostName; } // ### merge into qt_safe_write? @@ -145,7 +145,7 @@ QLockFile::LockError QLockFilePrivate::tryLock_sys() // Use operator% from the fast builder to avoid multiple memory allocations. QByteArray fileData = QByteArray::number(QCoreApplication::applicationPid()) % '\n' % qAppName().toUtf8() % '\n' - % localHostName().toUtf8() % '\n'; + % localHostName() % '\n'; const QByteArray lockFileName = QFile::encodeName(fileName); const int fd = qt_safe_open(lockFileName.constData(), O_WRONLY | O_CREAT | O_EXCL, 0644); @@ -190,7 +190,7 @@ bool QLockFilePrivate::isApparentlyStale() const QString hostname, appname; if (!getLockInfo(&pid, &hostname, &appname)) return false; - if (hostname == localHostName()) { + if (hostname == QString::fromLocal8Bit(localHostName())) { if (::kill(pid, 0) == -1 && errno == ESRCH) return true; // PID doesn't exist anymore } -- cgit v1.2.3