QmlDesigner: Use a POSIX shared memory for unix

QSharedMemory is using SVR4 shared memory for unix which is limited to
4 MB globally which is really hurting the performance for the designer.
POSIX shared memory is lifting some of this constraints but still has
some bugs on Max OS but generally works better.

Change-Id: I74c1ffd56495f408cd9340cd159190a1175a4086
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
This commit is contained in:
Marco Bubke
2014-07-31 16:45:21 +02:00
parent edb7961fe6
commit 0da08c227f
9 changed files with 772 additions and 52 deletions
@@ -29,7 +29,7 @@
#include "valueschangedcommand.h"
#include <QSharedMemory>
#include "sharedmemory.h"
#include <QCache>
#include <QDebug>
@@ -37,7 +37,7 @@
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
static QCache<qint32, SharedMemory> globalSharedMemoryCache(10000);
ValuesChangedCommand::ValuesChangedCommand()
: m_keyNumber(0)
@@ -63,7 +63,7 @@ quint32 ValuesChangedCommand::keyNumber() const
void ValuesChangedCommand::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
SharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
@@ -75,17 +75,11 @@ void ValuesChangedCommand::sort()
static const QLatin1String valueKeyTemplateString("Values-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
static SharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = new QSharedMemory(QString(valueKeyTemplateString).arg(key));
SharedMemory *sharedMemory = new SharedMemory(QString(valueKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (!sharedMemoryIsCreated) {
if (sharedMemory->isAttached())
sharedMemory->attach();
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
@@ -109,12 +103,13 @@ QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
temporaryOutDataStream << command.valueChanges();
QSharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
SharedMemory *sharedMemory = createSharedMemory(keyCounter, outDataStreamByteArray.size());
if (sharedMemory) {
sharedMemory->lock();
std::memcpy(sharedMemory->data(), outDataStreamByteArray.constData(), sharedMemory->size());
sharedMemory->unlock();
out << command.keyNumber();
return out;
}
@@ -128,13 +123,18 @@ QDataStream &operator<<(QDataStream &out, const ValuesChangedCommand &command)
void readSharedMemory(qint32 key, QVector<PropertyValueContainer> *valueChangeVector)
{
QSharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
SharedMemory sharedMemory(QString(valueKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
if (canAttach) {
sharedMemory.lock();
QDataStream in(QByteArray::fromRawData(static_cast<const char*>(sharedMemory.constData()), sharedMemory.size()));
in.setVersion(QDataStream::Qt_4_8);
in >> *valueChangeVector;
sharedMemory.unlock();
sharedMemory.detach();
}
}
@@ -1,21 +1,24 @@
INCLUDEPATH += $$PWD/
HEADERS += $$PWD/addimportcontainer.h
HEADERS += $$PWD/sharedmemory.h
HEADERS += $$PWD/imagecontainer.h
HEADERS += $$PWD/idcontainer.h
HEADERS += $$PWD/informationcontainer.h
HEADERS += $$PWD/instancecontainer.h
HEADERS += $$PWD/reparentcontainer.h
HEADERS += $$PWD/propertyabstractcontainer.h
HEADERS += $$PWD/propertybindingcontainer.h
HEADERS += $$PWD/propertyvaluecontainer.h
HEADERS += $$PWD/propertyabstractcontainer.h
HEADERS += $$PWD/propertybindingcontainer.h
HEADERS += $$PWD/propertyvaluecontainer.h
SOURCES += $$PWD/addimportcontainer.cpp
SOURCES += $$PWD/imagecontainer.cpp
SOURCES += $$PWD/idcontainer.cpp
SOURCES += $$PWD/informationcontainer.cpp
SOURCES += $$PWD/instancecontainer.cpp
SOURCES += $$PWD/reparentcontainer.cpp
SOURCES += $$PWD/propertyabstractcontainer.cpp
SOURCES += $$PWD/propertybindingcontainer.cpp
SOURCES += $$PWD/propertyvaluecontainer.cpp
SOURCES += $$PWD/addimportcontainer.cpp
unix:SOURCES += $$PWD/sharedmemory_unix.cpp
!unix:SOURCES += $$PWD/sharedmemory_qt.cpp
SOURCES += $$PWD/imagecontainer.cpp
SOURCES += $$PWD/idcontainer.cpp
SOURCES += $$PWD/informationcontainer.cpp
SOURCES += $$PWD/instancecontainer.cpp
SOURCES += $$PWD/reparentcontainer.cpp
SOURCES += $$PWD/propertyabstractcontainer.cpp
SOURCES += $$PWD/propertybindingcontainer.cpp
SOURCES += $$PWD/propertyvaluecontainer.cpp
@@ -29,7 +29,7 @@
#include "imagecontainer.h"
#include <QSharedMemory>
#include "sharedmemory.h"
#include <QCache>
#include <QDebug>
@@ -42,7 +42,7 @@
namespace QmlDesigner {
static QCache<qint32, QSharedMemory> globalSharedMemoryCache(10000);
static QCache<qint32, SharedMemory> globalSharedMemoryCache(10000);
ImageContainer::ImageContainer()
: m_instanceId(-1),
@@ -82,41 +82,52 @@ void ImageContainer::setImage(const QImage &image)
void ImageContainer::removeSharedMemorys(const QVector<qint32> &keyNumberVector)
{
foreach (qint32 keyNumber, keyNumberVector) {
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
SharedMemory *sharedMemory = globalSharedMemoryCache.take(keyNumber);
delete sharedMemory;
}
}
static const QLatin1String imageKeyTemplateString("Image-%1");
static QSharedMemory *createSharedMemory(qint32 key, int byteCount)
static SharedMemory *createSharedMemory(qint32 key, int byteCount)
{
QSharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
SharedMemory *sharedMemory = globalSharedMemoryCache.take(key);
if (sharedMemory == 0)
sharedMemory = new QSharedMemory(QString(imageKeyTemplateString).arg(key));
if (sharedMemory == 0) {
sharedMemory = new SharedMemory(QString(imageKeyTemplateString).arg(key));
bool sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
} else {
delete sharedMemory;
sharedMemory = 0;
}
} else {
bool sharedMemoryIsAttached = sharedMemory->isAttached();
if (!sharedMemoryIsAttached)
sharedMemoryIsAttached = sharedMemory->attach();
bool sharedMemoryIsCreated = sharedMemory->isAttached();
if (!sharedMemoryIsCreated)
sharedMemoryIsCreated = sharedMemory->attach();
bool sharedMemorySizeIsSmallerThanByteCount = sharedMemory->size() < byteCount;
bool sharedMemorySizeIsDoubleBiggerThanByteCount = sharedMemory->size() > (byteCount * 2);
bool sharedMemorySizeIsSmallerThanByteCount = sharedMemory->size() < byteCount;
bool sharedMemorySizeIsDoubleBiggerThanByteCount = sharedMemory->size() * 2 > byteCount;
if (!sharedMemoryIsAttached) {
sharedMemory->create(byteCount);
} else if (sharedMemorySizeIsSmallerThanByteCount || sharedMemorySizeIsDoubleBiggerThanByteCount) {
sharedMemory->detach();
sharedMemory->create(byteCount);
}
if (!sharedMemoryIsCreated || sharedMemorySizeIsSmallerThanByteCount || sharedMemorySizeIsDoubleBiggerThanByteCount) {
sharedMemory->detach();
sharedMemoryIsCreated = sharedMemory->create(byteCount);
if (!sharedMemory->isAttached()) {
globalSharedMemoryCache.remove(key);
delete sharedMemory;
sharedMemory = 0;
}
}
if (sharedMemoryIsCreated) {
globalSharedMemoryCache.insert(key, sharedMemory);
return sharedMemory;
}
return 0;
return sharedMemory;
}
static void writeSharedMemory(QSharedMemory *sharedMemory, const QImage &image)
static void writeSharedMemory(SharedMemory *sharedMemory, const QImage &image)
{
sharedMemory->lock();
@@ -129,7 +140,6 @@ static void writeSharedMemory(QSharedMemory *sharedMemory, const QImage &image)
std::memcpy(sharedMemory->data(), headerData, 20);
std::memcpy(reinterpret_cast<char*>(sharedMemory->data()) + 20, image.constBits(), image.byteCount());
sharedMemory->unlock();
}
@@ -156,7 +166,7 @@ QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
out << qint32(0);
writeStream(out, image);
} else {
QSharedMemory *sharedMemory = createSharedMemory(container.keyNumber(), image.byteCount() + extraDataSize);
SharedMemory *sharedMemory = createSharedMemory(container.keyNumber(), image.byteCount() + extraDataSize);
out << qint32(sharedMemory != 0); // send if shared memory is used
@@ -171,7 +181,7 @@ QDataStream &operator<<(QDataStream &out, const ImageContainer &container)
static void readSharedMemory(qint32 key, ImageContainer &container)
{
QSharedMemory sharedMemory(QString(imageKeyTemplateString).arg(key));
SharedMemory sharedMemory(QString(imageKeyTemplateString).arg(key));
bool canAttach = sharedMemory.attach(QSharedMemory::ReadOnly);
@@ -194,6 +204,7 @@ static void readSharedMemory(qint32 key, ImageContainer &container)
container.setImage(image);
sharedMemory.unlock();
sharedMemory.detach();
}
}
@@ -0,0 +1,96 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef QMLDESIGNER_SHAREDMEMORY_H
#define QMLDESIGNER_SHAREDMEMORY_H
#include <QSharedMemory>
#include <QSystemSemaphore>
namespace QmlDesigner {
class SharedMemory
{
friend class SharedMemoryLocker;
public:
SharedMemory();
SharedMemory(const QString &key);
~SharedMemory();
void setKey(const QString &key);
QString key() const;
bool create(int size, QSharedMemory::AccessMode mode = QSharedMemory::ReadWrite);
int size() const;
bool attach(QSharedMemory::AccessMode mode = QSharedMemory::ReadWrite);
bool isAttached() const;
bool detach();
void *data();
const void* constData() const;
const void *data() const;
bool lock();
bool unlock();
QSharedMemory::SharedMemoryError error() const;
QString errorString() const;
protected:
#ifdef Q_OS_UNIX
bool initKeyInternal();
bool cleanHandleInternal();
bool createInternal(QSharedMemory::AccessMode mode, int size);
bool attachInternal(QSharedMemory::AccessMode mode);
bool detachInternal();
int handle();
void setErrorString(const QString &function);
#endif
private:
#ifndef Q_OS_UNIX
QSharedMemory m_sharedMemory;
#else
void *m_memory;
int m_size;
QString m_key;
QByteArray m_nativeKey;
QSharedMemory::SharedMemoryError m_error;
QString m_errorString;
QSystemSemaphore m_systemSemaphore;
bool m_lockedByMe;
int m_fileHandle;
bool m_createdByMe;
#endif
};
} // namespace QmlDesigner
#endif // QMLDESIGNER_SHAREDMEMORY_H
@@ -0,0 +1,117 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "sharedmemory.h"
namespace QmlDesigner {
SharedMemory::SharedMemory()
{
}
SharedMemory::SharedMemory(const QString &key)
: m_sharedMemory(key)
{
}
SharedMemory::~SharedMemory()
{
}
void SharedMemory::setKey(const QString &key)
{
m_sharedMemory.setKey(key);
}
QString SharedMemory::key() const
{
return m_sharedMemory.key();
}
bool SharedMemory::create(int size, QSharedMemory::AccessMode mode)
{
return m_sharedMemory.create(size, mode);
}
int SharedMemory::size() const
{
return m_sharedMemory.size();
}
bool SharedMemory::attach(QSharedMemory::AccessMode mode)
{
return m_sharedMemory.attach(mode);
}
bool SharedMemory::isAttached() const
{
return m_sharedMemory.isAttached();
}
bool SharedMemory::detach()
{
return m_sharedMemory.detach();
}
void *SharedMemory::data()
{
return m_sharedMemory.data();
}
const void *SharedMemory::constData() const
{
return m_sharedMemory.constData();
}
const void *SharedMemory::data() const
{
return m_sharedMemory.data();
}
bool SharedMemory::lock()
{
return m_sharedMemory.lock();
}
bool SharedMemory::unlock()
{
return m_sharedMemory.unlock();
}
QSharedMemory::SharedMemoryError SharedMemory::error() const
{
return m_sharedMemory.error();
}
QString SharedMemory::errorString() const
{
return m_sharedMemory.errorString();
}
} // namespace QmlDesigner
@@ -0,0 +1,488 @@
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "sharedmemory.h"
#include <qdir.h>
#include <qcryptographichash.h>
#include "qplatformdefs.h"
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */
#include <sys/types.h>
#include <sys/stat.h>
#ifdef Q_OS_OSX
#include <sys/posix_shm.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <private/qcore_unix_p.h>
#ifdef Q_OS_OSX
#define NAME_MAX PSHMNAMLEN
#endif
namespace QmlDesigner {
class SharedMemoryLocker
{
public:
SharedMemoryLocker(SharedMemory *sharedMemory) : m_sharedMemory(sharedMemory)
{
Q_ASSERT(m_sharedMemory);
}
~SharedMemoryLocker()
{
if (m_sharedMemory)
m_sharedMemory->unlock();
}
bool lock()
{
if (m_sharedMemory && m_sharedMemory->lock())
return true;
m_sharedMemory = 0;
return false;
}
bool tryLocker(const QString function) {
if (!lock()) {
m_sharedMemory->m_errorString = QStringLiteral("%1: unable to lock").arg(function);
m_sharedMemory->m_error = QSharedMemory::LockError;
return false;
}
return true;
}
private:
SharedMemory *m_sharedMemory;
};
static QByteArray makePlatformSafeKey(const QString &key)
{
if (key.isEmpty())
return QByteArray();
return QCryptographicHash::hash(key.toLatin1(), QCryptographicHash::Sha1).toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
}
SharedMemory::SharedMemory()
: m_memory(0),
m_size(0),
m_error(QSharedMemory::NoError),
m_systemSemaphore(QString()),
m_lockedByMe(false),
m_fileHandle(-1),
m_createdByMe(false)
{
}
SharedMemory::SharedMemory(const QString &key)
: m_memory(0),
m_size(0),
m_error(QSharedMemory::NoError),
m_systemSemaphore(QString()),
m_lockedByMe(false),
m_fileHandle(-1),
m_createdByMe(false)
{
setKey(key);
}
SharedMemory::~SharedMemory()
{
if (m_memory) {
munmap(m_memory, m_size);
}
if (m_fileHandle != -1) {
close(m_fileHandle);
if (m_createdByMe)
shm_unlink(m_nativeKey);
}
setKey(QString());
}
void SharedMemory::setKey(const QString &key)
{
if (key == m_key && makePlatformSafeKey(key) == m_nativeKey)
return;
if (isAttached())
detach();
cleanHandleInternal();
m_key = key;
m_nativeKey = makePlatformSafeKey(key);
}
QString SharedMemory::key() const
{
return m_key;
}
bool SharedMemory::create(int size, QSharedMemory::AccessMode mode)
{
if (!initKeyInternal())
return false;
m_systemSemaphore.setKey(m_key, 1, QSystemSemaphore::Create);
QString function = QLatin1String("SharedMemory::create");
SharedMemoryLocker lock(this);
if (!m_key.isNull() && !lock.tryLocker(function))
return false;
if (size <= 0) {
m_error = QSharedMemory::InvalidSize;
m_errorString = QStringLiteral("%1: create size is less then 0").arg(function);
return false;
}
return createInternal(mode ,size);
}
int SharedMemory::size() const
{
return m_size;
}
bool SharedMemory::attach(QSharedMemory::AccessMode mode)
{
if (isAttached() || !initKeyInternal())
return false;
SharedMemoryLocker lock(this);
if (!m_key.isNull() && !lock.tryLocker(QStringLiteral("SharedMemory::attach")))
return false;
if (isAttached() || !handle())
return false;
return attachInternal(mode);
}
bool SharedMemory::isAttached() const
{
return (0 != m_memory);
}
bool SharedMemory::detach()
{
if (!isAttached())
return false;
SharedMemoryLocker lock(this);
if (!m_key.isNull() && !lock.tryLocker(QStringLiteral("SharedMemory::detach")))
return false;
return detachInternal();
}
void *SharedMemory::data()
{
return m_memory;
}
const void* SharedMemory::constData() const
{
return m_memory;
}
const void *SharedMemory::data() const
{
return m_memory;
}
bool SharedMemory::lock()
{
if (m_lockedByMe) {
qWarning("SharedMemory::lock: already locked");
return true;
}
if (m_systemSemaphore.acquire()) {
m_lockedByMe = true;
return true;
}
QString function = QStringLiteral("SharedMemory::lock");
m_errorString = QStringLiteral("%1: unable to lock").arg(function);
m_error = QSharedMemory::LockError;
return false;
}
bool SharedMemory::unlock()
{
if (!m_lockedByMe)
return false;
m_lockedByMe = false;
if (m_systemSemaphore.release())
return true;
QString function = QStringLiteral("SharedMemory::unlock");
m_errorString = QStringLiteral("%1: unable to unlock").arg(function);
m_error = QSharedMemory::LockError;
return false;
}
QSharedMemory::SharedMemoryError SharedMemory::error() const
{
return m_error;
}
QString SharedMemory::errorString() const
{
return m_errorString;
}
void SharedMemory::setErrorString(const QString &function)
{
// EINVAL is handled in functions so they can give better error strings
switch (errno) {
case EACCES:
m_errorString = QStringLiteral("%1: permission denied").arg(function);
m_error = QSharedMemory::PermissionDenied;
break;
case EEXIST:
m_errorString = QStringLiteral("%1: already exists").arg(function);
m_error = QSharedMemory::AlreadyExists;
break;
case ENOENT:
m_errorString = QStringLiteral("%1: doesn't exist").arg(function);
m_error = QSharedMemory::NotFound;
break;
case EMFILE:
case ENOMEM:
case ENOSPC:
m_errorString = QStringLiteral("%1: out of resources").arg(function);
m_error = QSharedMemory::OutOfResources;
break;
default:
m_errorString = QStringLiteral("%1: unknown error %2").arg(function).arg(strerror(errno));
m_error = QSharedMemory::UnknownError;
}
}
bool SharedMemory::initKeyInternal()
{
if (!cleanHandleInternal())
return false;
m_systemSemaphore.setKey(QString(), 1);
m_systemSemaphore.setKey(m_key, 1);
if (m_systemSemaphore.error() != QSystemSemaphore::NoError) {
m_errorString = QStringLiteral("SharedMemoryPrivate::initKey: unable to set key on lock");
switch (m_systemSemaphore.error()) {
case QSystemSemaphore::PermissionDenied:
m_error = QSharedMemory::PermissionDenied;
break;
case QSystemSemaphore::KeyError:
m_error = QSharedMemory::KeyError;
break;
case QSystemSemaphore::AlreadyExists:
m_error = QSharedMemory::AlreadyExists;
break;
case QSystemSemaphore::NotFound:
m_error = QSharedMemory::NotFound;
break;
case QSystemSemaphore::OutOfResources:
m_error = QSharedMemory::OutOfResources;
break;
case QSystemSemaphore::UnknownError:
default:
m_error = QSharedMemory::UnknownError;
break;
}
return false;
}
m_errorString = QString();
m_error = QSharedMemory::NoError;
return true;
}
int SharedMemory::handle()
{
return m_fileHandle;
}
bool SharedMemory::cleanHandleInternal()
{
m_fileHandle = -1;
return true;
}
bool SharedMemory::createInternal(QSharedMemory::AccessMode mode, int size)
{
detachInternal();
#ifdef Q_OS_OSX
if (m_fileHandle > -1 && m_createdByMe) {
close(m_fileHandle);
shm_unlink(m_nativeKey.constData());
m_fileHandle = -1;
}
#endif
if (m_fileHandle == -1) {
int permission = mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR;
m_fileHandle = shm_open(m_nativeKey.constData(), O_CREAT | permission, 0666);
if (m_fileHandle == -1) {
switch (errno) {
case EINVAL:
m_errorString = QStringLiteral("QSharedMemory::create: key is not invalid");
m_error = QSharedMemory::KeyError;
break;
case EMFILE:
m_errorString = QStringLiteral("QSharedMemory::create: maximum file limit reached");
m_error = QSharedMemory::UnknownError;
break;
case ENAMETOOLONG:
m_errorString = QStringLiteral("QSharedMemory::create: key is to long");
m_error = QSharedMemory::KeyError;
break;
default:
setErrorString(QStringLiteral("SharedMemory::create"));
}
return false;
}
m_createdByMe = true;
}
struct stat statBuffer;
fstat(m_fileHandle, &statBuffer);
int fileSize = statBuffer.st_size;
if (fileSize < size) {
int returnValue = ftruncate(m_fileHandle, size);
if (returnValue == -1) {
switch (errno) {
case EFBIG:
m_errorString = QStringLiteral("QSharedMemory::create: size is to large");
m_error = QSharedMemory::InvalidSize;
break;
default:
setErrorString(QStringLiteral("SharedMemory::create"));
}
close(m_fileHandle);
shm_unlink(m_nativeKey.constData());
m_fileHandle = -1;
m_size = 0;
return false;
}
}
int protection = mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_WRITE;
m_memory = mmap(0, size, protection, MAP_SHARED, m_fileHandle, 0);
if (m_memory == MAP_FAILED) {
close(m_fileHandle);
shm_unlink(m_nativeKey.constData());
m_memory = 0;
m_fileHandle = -1;
m_size = 0;
return false;
}
m_size = size;
return true;
}
bool SharedMemory::attachInternal(QSharedMemory::AccessMode mode)
{
if (m_fileHandle == -1) {
int permission = mode == QSharedMemory::ReadOnly ? O_RDONLY : O_RDWR;
m_fileHandle = shm_open(m_nativeKey.constData(), permission, 0666);
if (m_fileHandle == -1) {
switch (errno) {
case EINVAL:
m_errorString = QStringLiteral("QSharedMemory::attach: key is invalid");
m_error = QSharedMemory::KeyError;
break;
case EMFILE:
m_errorString = QStringLiteral("QSharedMemory::attach: maximum file limit reached");
m_error = QSharedMemory::UnknownError;
break;
case ENAMETOOLONG:
m_errorString = QStringLiteral("QSharedMemory::attach: key is to long");
m_error = QSharedMemory::KeyError;
break;
default:
setErrorString(QStringLiteral("SharedMemory::attach"));
}
return false;
}
}
struct stat statBuffer;
fstat(m_fileHandle, &statBuffer);
int size = statBuffer.st_size;
int protection = mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_WRITE;
m_memory = mmap(0, size, protection, MAP_SHARED, m_fileHandle, 0);
if (m_memory == MAP_FAILED) {
m_memory = 0;
return false;
}
m_size = size;
return true;
}
bool SharedMemory::detachInternal()
{
if (m_memory) {
munmap(m_memory, m_size);
m_memory = 0;
m_size = 0;
}
return false;
}
} // namespace QmlDesigner
@@ -14,7 +14,7 @@ QT += core-private qml-private quick-private gui-private
QT += v8-private
}
!macx {
!osx {
CONFIG += c++11
}
@@ -41,8 +41,9 @@ DEFINES -= QT_NO_CAST_FROM_ASCII
OTHER_FILES += Info.plist
unix:!osx:LIBS += -lrt # posix shared memory
macx {
osx {
CONFIG -= app_bundle
QMAKE_LFLAGS += -sectcreate __TEXT __info_plist $$system_quote($$PWD/Info.plist)
} else {
@@ -22,6 +22,8 @@ SOURCES += $$PWD/qmlpuppetmain.cpp
RESOURCES += $$PWD/../qmlpuppet.qrc
DEFINES -= QT_NO_CAST_FROM_ASCII
unix:!osx:LIBS += -lrt # posix shared memory
OTHER_FILES += Info.plist
macx {
CONFIG -= app_bundle
@@ -3,6 +3,8 @@ CONFIG += exceptions
INCLUDEPATH += $$PWD
unix:!osx:LIBS += -lrt # posix shared memory
include(../../qtcreatorplugin.pri)
include(designercore/designercore-lib.pri)