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>
This commit is contained in:
Thiago Macieira 2013-10-09 11:40:05 -07:00 committed by The Qt Project
parent a199dd133e
commit 11e93372df

View File

@ -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
}