summaryrefslogtreecommitdiffstats
path: root/src/corelib/io
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-10-09 11:40:05 -0700
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-10-29 16:04:38 +0100
commit11e93372df2ebe8700ebb39fca3709e3a6c3d399 (patch)
tree1f01950f1cf150c25218bdde1e84b02e96f7a573 /src/corelib/io
parenta199dd133e15374552c4238b3fd1fb0e83a59988 (diff)
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 <oswald.buddenhagen@digia.com> Reviewed-by: Kevin Ottens <kevin.ottens@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io')
-rw-r--r--src/corelib/io/qlockfile_unix.cpp16
1 files changed, 8 insertions, 8 deletions
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
}