From 3026f8ce811e8cebc48e49f732344cc13e107cc6 Mon Sep 17 00:00:00 2001 From: Andreas Aardal Hanssen Date: Mon, 31 May 2010 10:11:05 +0200 Subject: Revert "Added native key support to QSharedMemory API." This reverts commit 9b4ff3deb28b3d642dc4480207f2f23841cf26e9. --- src/corelib/kernel/qsharedmemory.cpp | 120 ++++++--------------- src/corelib/kernel/qsharedmemory.h | 2 - src/corelib/kernel/qsharedmemory_p.h | 1 - src/corelib/kernel/qsharedmemory_symbian.cpp | 11 +- src/corelib/kernel/qsharedmemory_unix.cpp | 15 +-- src/corelib/kernel/qsharedmemory_win.cpp | 13 ++- .../auto/qtipc/qsharedmemory/tst_qsharedmemory.cpp | 26 ++--- 7 files changed, 65 insertions(+), 123 deletions(-) diff --git a/src/corelib/kernel/qsharedmemory.cpp b/src/corelib/kernel/qsharedmemory.cpp index d5f4052..fe93ebc 100644 --- a/src/corelib/kernel/qsharedmemory.cpp +++ b/src/corelib/kernel/qsharedmemory.cpp @@ -142,12 +142,9 @@ QSharedMemoryPrivate::makePlatformSafeKey(const QString &key, remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory. - \warning QSharedMemory changes the key in a Qt-specific way, unless otherwise - specified. Interoperation with non-Qt applications is achieved by first creating - a default shared memory with QSharedMemory() and then setting a native key with - setNativeKey(). When using native keys, shared memory is not protected against - multiple accesses on it (e.g. unable to lock()) and a user-defined mechanism - should be used to achieve a such protection. + \warning QSharedMemory changes the key in a Qt-specific way. + It is therefore currently not possible to use the shared memory of + non-Qt applications with QSharedMemory. */ /*! @@ -156,8 +153,8 @@ QSharedMemoryPrivate::makePlatformSafeKey(const QString &key, Constructs a shared memory object with the given \a parent. The shared memory object's key is not set by the constructor, so the shared memory object does not have an underlying shared memory - segment attached. The key must be set with setKey() or setNativeKey() - before create() or attach() can be used. + segment attached. The key must be set with setKey() before create() + or attach() can be used. \sa setKey() */ @@ -194,52 +191,24 @@ QSharedMemory::~QSharedMemory() } /*! - Sets a new \a key for this shared memory object. - The \a key is first converted to a unicode string accepted by all supported platforms, - (see nativeKey()). If \a key and the current key are the same, the function returns - without doing anything. If the shared memory object is attached to an underlying - shared memory segment, it will \l {detach()} {detach} from it before setting the new key. - This function does not do an attach(). - - \sa key() nativeKey() isAttached() -*/ + Sets a new \a key for this shared memory object. If \a key and the + current key are the same, the function returns without doing + anything. If the shared memory object is attached to an underlying + shared memory segment, it will \l {detach()} {detach} from it before + setting the new key. This function does not do an attach(). + + \sa key() isAttached() + */ void QSharedMemory::setKey(const QString &key) { Q_D(QSharedMemory); - if (key == d->key && d->makePlatformSafeKey(key) == d->nativeKey) + if (key == d->key) return; if (isAttached()) detach(); d->cleanHandle(); d->key = key; - d->nativeKey = d->makePlatformSafeKey(key); -} - -/*! - \since 4.7 - Sets a new native \a key for this shared memory object. - The specified \a key is used as-is, without any conversion. The \a key has to be - in a valid format for the current operating system (e.g. under Unix a valid \a key - corresponds to a filename). Be aware that the application might not be portable. - This function returns without doing anything if the \a key equals the current - native key. If the shared memory object is attached to an underlying shared memory - segment, it will \l {detach()} {detach} from it before setting the new key. - This function does not do an attach(). - - \sa nativeKey() key() isAttached() -*/ -void QSharedMemory::setNativeKey(const QString &key) -{ - Q_D(QSharedMemory); - if (key == d->nativeKey && d->key.isNull()) - return; - - if (isAttached()) - detach(); - d->cleanHandle(); - d->key = QString(); - d->nativeKey = key; } bool QSharedMemoryPrivate::initKey() @@ -282,14 +251,13 @@ bool QSharedMemoryPrivate::initKey() } /*! - Returns the key assigned with setKey() to this shared memory. - The key is the identifier used by Qt applications to identify the shared - memory segment. The actual native key used by the operating system is returned - by nativeKey(). A null string is returned if the key was specified using setNativeKey(). - When QSharedMemory is used for interprocess communication, the key is how each - process attaches to the shared memory segment through which the IPC occurs. - - \sa setKey() setNativeKey() + Returns the key assigned to this shared memory. The key is the + identifier used by the operating system to identify the shared + memory segment. When QSharedMemory is used for interprocess + communication, the key is how each process attaches to the shared + memory segment through which the IPC occurs. + + \sa setKey() */ QString QSharedMemory::key() const { @@ -298,31 +266,13 @@ QString QSharedMemory::key() const } /*! - \since 4.7 - Returns the native key assigned with setKey() or setNativeKey() to this shared memory. - The native key is the identifier used by the operating system to identify the - shared memory segment. When using setKey(), the native key is obtained by - converting the specified key into a format accepted by all supported platforms. - When using setNativeKey(), the native key actually corresponds to the specified key - without any conversion. When QSharedMemory is used for interprocess communication, - the key is how each process attaches to the shared memory segment through which - the IPC occurs. - - \sa setKey() setNativeKey() -*/ -QString QSharedMemory::nativeKey() const -{ - Q_D(const QSharedMemory); - return d->nativeKey; -} - -/*! - Creates a shared memory segment of \a size bytes with the key passed to the - constructor, set with setKey() or set with setNativeKey(), then attaches to - the new shared memory segment with the given access \a mode and returns - \tt true. If a shared memory segment identified by the key already exists, - the attach operation is not performed and \tt false is returned. When the - return value is \tt false, call error() to determine which error occurred. + Creates a shared memory segment of \a size bytes with the key passed + to the constructor or set with setKey(), attaches to the new shared + memory segment with the given access \a mode, and returns \tt true. + If a shared memory segment identified by the key already exists, the + attach operation is not performed, and \tt false is returned. When + the return value is \tt false, call error() to determine which error + occurred. \sa error() */ @@ -344,7 +294,7 @@ bool QSharedMemory::create(int size, AccessMode mode) QString function = QLatin1String("QSharedMemory::create"); #ifndef QT_NO_SYSTEMSEMAPHORE QSharedMemoryLocker lock(this); - if (!d->key.isNull() && !d->tryLocker(&lock, function)) + if (!d->tryLocker(&lock, function)) return false; #endif @@ -388,7 +338,7 @@ int QSharedMemory::size() const /*! Attempts to attach the process to the shared memory segment identified by the key that was passed to the constructor or to a - call to setKey() or setNativeKey(). The access \a mode is \l {QSharedMemory::} + call to setKey(). The access \a mode is \l {QSharedMemory::} {ReadWrite} by default. It can also be \l {QSharedMemory::} {ReadOnly}. Returns true if the attach operation is successful. If false is returned, call error() to determine which error occurred. @@ -405,7 +355,7 @@ bool QSharedMemory::attach(AccessMode mode) return false; #ifndef QT_NO_SYSTEMSEMAPHORE QSharedMemoryLocker lock(this); - if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::attach"))) + if (!d->tryLocker(&lock, QLatin1String("QSharedMemory::attach"))) return false; #endif @@ -445,7 +395,7 @@ bool QSharedMemory::detach() #ifndef QT_NO_SYSTEMSEMAPHORE QSharedMemoryLocker lock(this); - if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::detach"))) + if (!d->tryLocker(&lock, QLatin1String("QSharedMemory::detach"))) return false; #endif @@ -501,9 +451,9 @@ const void *QSharedMemory::data() const by this process and returns true. If another process has locked the segment, this function blocks until the lock is released. Then it acquires the lock and returns true. If this function returns false, - it means that you have ignored a false return from create() or attach(), - that you have set the key with setNativeKey() or that - QSystemSemaphore::acquire() failed due to an unknown system error. + it means either that you have ignored a false return from create() + or attach(), or that QSystemSemaphore::acquire() failed due to an + unknown system error. \sa unlock(), data(), QSystemSemaphore::acquire() */ diff --git a/src/corelib/kernel/qsharedmemory.h b/src/corelib/kernel/qsharedmemory.h index 5673f43..fba939c 100644 --- a/src/corelib/kernel/qsharedmemory.h +++ b/src/corelib/kernel/qsharedmemory.h @@ -85,8 +85,6 @@ public: void setKey(const QString &key); QString key() const; - void setNativeKey(const QString &key); - QString nativeKey() const; bool create(int size, AccessMode mode = ReadWrite); int size() const; diff --git a/src/corelib/kernel/qsharedmemory_p.h b/src/corelib/kernel/qsharedmemory_p.h index 632a6e9..a52f8b3 100644 --- a/src/corelib/kernel/qsharedmemory_p.h +++ b/src/corelib/kernel/qsharedmemory_p.h @@ -122,7 +122,6 @@ public: void *memory; int size; QString key; - QString nativeKey; QSharedMemory::SharedMemoryError error; QString errorString; #ifndef QT_NO_SYSTEMSEMAPHORE diff --git a/src/corelib/kernel/qsharedmemory_symbian.cpp b/src/corelib/kernel/qsharedmemory_symbian.cpp index 091c2b5..9b84eb56 100644 --- a/src/corelib/kernel/qsharedmemory_symbian.cpp +++ b/src/corelib/kernel/qsharedmemory_symbian.cpp @@ -107,14 +107,16 @@ bool QSharedMemoryPrivate::cleanHandle() bool QSharedMemoryPrivate::create(int size) { + // Get a windows acceptable key + QString safeKey = makePlatformSafeKey(key); QString function = QLatin1String("QSharedMemory::create"); - if (nativeKey.isEmpty()) { + if (safeKey.isEmpty()) { error = QSharedMemory::KeyError; errorString = QSharedMemory::tr("%1: key error").arg(function); return false; } - TPtrC ptr(qt_QString2TPtrC(nativeKey)); + TPtrC ptr(qt_QString2TPtrC(safeKey)); TInt err = chunk.CreateGlobal(ptr, size, size); @@ -134,13 +136,14 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode /* mode */) // Grab a pointer to the memory block if (!chunk.Handle()) { QString function = QLatin1String("QSharedMemory::handle"); - if (nativeKey.isEmpty()) { + QString safeKey = makePlatformSafeKey(key); + if (safeKey.isEmpty()) { error = QSharedMemory::KeyError; errorString = QSharedMemory::tr("%1: unable to make key").arg(function); return false; } - TPtrC ptr(qt_QString2TPtrC(nativeKey)); + TPtrC ptr(qt_QString2TPtrC(safeKey)); TInt err = KErrNoMemory; diff --git a/src/corelib/kernel/qsharedmemory_unix.cpp b/src/corelib/kernel/qsharedmemory_unix.cpp index 064979b..a5f79c2 100644 --- a/src/corelib/kernel/qsharedmemory_unix.cpp +++ b/src/corelib/kernel/qsharedmemory_unix.cpp @@ -116,20 +116,21 @@ key_t QSharedMemoryPrivate::handle() return unix_key; // don't allow making handles on empty keys - if (nativeKey.isEmpty()) { + if (key.isEmpty()) { errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle:")); error = QSharedMemory::KeyError; return 0; } // ftok requires that an actual file exists somewhere - if (!QFile::exists(nativeKey)) { + QString fileName = makePlatformSafeKey(key); + if (!QFile::exists(fileName)) { errorString = QSharedMemory::tr("%1: UNIX key file doesn't exist").arg(QLatin1String("QSharedMemory::handle:")); error = QSharedMemory::NotFound; return 0; } - unix_key = ftok(QFile::encodeName(nativeKey).constData(), 'Q'); + unix_key = ftok(QFile::encodeName(fileName).constData(), 'Q'); if (-1 == unix_key) { errorString = QSharedMemory::tr("%1: ftok failed").arg(QLatin1String("QSharedMemory::handle:")); error = QSharedMemory::KeyError; @@ -180,7 +181,7 @@ bool QSharedMemoryPrivate::create(int size) { // build file if needed bool createdFile = false; - int built = createUnixKeyFile(nativeKey); + int built = createUnixKeyFile(makePlatformSafeKey(key)); if (built == -1) { errorString = QSharedMemory::tr("%1: unable to make key").arg(QLatin1String("QSharedMemory::handle:")); error = QSharedMemory::KeyError; @@ -193,7 +194,7 @@ bool QSharedMemoryPrivate::create(int size) // get handle if (!handle()) { if (createdFile) - QFile::remove(nativeKey); + QFile::remove(makePlatformSafeKey(key)); return false; } @@ -209,7 +210,7 @@ bool QSharedMemoryPrivate::create(int size) setErrorString(function); } if (createdFile && error != QSharedMemory::AlreadyExists) - QFile::remove(nativeKey); + QFile::remove(makePlatformSafeKey(key)); return false; } @@ -294,7 +295,7 @@ bool QSharedMemoryPrivate::detach() } // remove file - if (!QFile::remove(nativeKey)) + if (!QFile::remove(makePlatformSafeKey(key))) return false; } return true; diff --git a/src/corelib/kernel/qsharedmemory_win.cpp b/src/corelib/kernel/qsharedmemory_win.cpp index 0cdb123..0f5fdc7 100644 --- a/src/corelib/kernel/qsharedmemory_win.cpp +++ b/src/corelib/kernel/qsharedmemory_win.cpp @@ -99,17 +99,18 @@ HANDLE QSharedMemoryPrivate::handle() { if (!hand) { QString function = QLatin1String("QSharedMemory::handle"); - if (nativeKey.isEmpty()) { + QString safeKey = makePlatformSafeKey(key); + if (safeKey.isEmpty()) { error = QSharedMemory::KeyError; errorString = QSharedMemory::tr("%1: unable to make key").arg(function); return false; } #ifndef Q_OS_WINCE - hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)nativeKey.utf16()); + hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)safeKey.utf16()); #else // This works for opening a mapping too, but always opens it with read/write access in // attach as it seems. - hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16()); + hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)safeKey.utf16()); #endif if (!hand) { setErrorString(function); @@ -132,15 +133,17 @@ bool QSharedMemoryPrivate::cleanHandle() bool QSharedMemoryPrivate::create(int size) { + // Get a windows acceptable key + QString safeKey = makePlatformSafeKey(key); QString function = QLatin1String("QSharedMemory::create"); - if (nativeKey.isEmpty()) { + if (safeKey.isEmpty()) { error = QSharedMemory::KeyError; errorString = QSharedMemory::tr("%1: key error").arg(function); return false; } // Create the file mapping. - hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16()); + hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)safeKey.utf16()); setErrorString(function); // hand is valid when it already exists unlike unix so explicitly check diff --git a/tests/auto/qtipc/qsharedmemory/tst_qsharedmemory.cpp b/tests/auto/qtipc/qsharedmemory/tst_qsharedmemory.cpp index dc071ab..1f65ae7 100644 --- a/tests/auto/qtipc/qsharedmemory/tst_qsharedmemory.cpp +++ b/tests/auto/qtipc/qsharedmemory/tst_qsharedmemory.cpp @@ -225,17 +225,11 @@ void tst_QSharedMemory::key_data() { QTest::addColumn("constructorKey"); QTest::addColumn("setKey"); - QTest::addColumn("setNativeKey"); - - QTest::newRow("null, null, null") << QString() << QString() << QString(); - QTest::newRow("one, null, null") << QString("one") << QString() << QString(); - QTest::newRow("null, one, null") << QString() << QString("one") << QString(); - QTest::newRow("null, null, one") << QString() << QString() << QString("one"); - QTest::newRow("one, two, null") << QString("one") << QString("two") << QString(); - QTest::newRow("one, null, two") << QString("one") << QString() << QString("two"); - QTest::newRow("null, one, two") << QString() << QString("one") << QString("two"); - QTest::newRow("one, two, three") << QString("one") << QString("two") << QString("three"); - QTest::newRow("invalid") << QString("o/e") << QString("t/o") << QString("|x"); + + QTest::newRow("null, null") << QString() << QString(); + QTest::newRow("null, one") << QString() << QString("one"); + QTest::newRow("one, two") << QString("one") << QString("two"); + QTest::newRow("invalid") << QString("o/e") << QString("t/o"); } /*! @@ -245,17 +239,11 @@ void tst_QSharedMemory::key() { QFETCH(QString, constructorKey); QFETCH(QString, setKey); - QFETCH(QString, setNativeKey); QSharedMemory sm(constructorKey); QCOMPARE(sm.key(), constructorKey); - QCOMPARE(sm.nativeKey().isEmpty(), constructorKey.isEmpty()); sm.setKey(setKey); QCOMPARE(sm.key(), setKey); - QCOMPARE(sm.nativeKey().isEmpty(), setKey.isEmpty()); - sm.setNativeKey(setNativeKey); - QVERIFY(sm.key().isNull()); - QCOMPARE(sm.nativeKey(), setNativeKey); QCOMPARE(sm.isAttached(), false); QCOMPARE(sm.error(), QSharedMemory::NoError); @@ -274,7 +262,7 @@ void tst_QSharedMemory::create_data() QTest::addColumn("error"); QTest::newRow("null key") << QString() << 1024 - << false << QSharedMemory::KeyError; + << false << QSharedMemory::LockError; QTest::newRow("-1 size") << QString("negsize") << -1 << false << QSharedMemory::InvalidSize; QTest::newRow("nor size") << QString("norsize") << 1024 @@ -314,7 +302,7 @@ void tst_QSharedMemory::attach_data() QTest::addColumn("exists"); QTest::addColumn("error"); - QTest::newRow("null key") << QString() << false << QSharedMemory::KeyError; + QTest::newRow("null key") << QString() << false << QSharedMemory::LockError; QTest::newRow("doesn't exists") << QString("doesntexists") << false << QSharedMemory::NotFound; QTest::newRow("already exists") << QString(EXISTING_SHARE) << true << QSharedMemory::NoError; } -- cgit v0.12