summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRitt Konstantin <ritt.ks@gmail.com>2011-06-06 12:51:41 (GMT)
committerHarald Fernengel <harald.fernengel@nokia.com>2011-06-06 12:51:41 (GMT)
commit9d0c57b225d3a9a771edf4f4b4c557430560761d (patch)
tree18d6da2382d34d050691121366b1e65d453c8a3a /src
parent601b6d5ccbd3980eebb4e6f41ac721f01ee5ef04 (diff)
downloadQt-9d0c57b225d3a9a771edf4f4b4c557430560761d.zip
Qt-9d0c57b225d3a9a771edf4f4b4c557430560761d.tar.gz
Qt-9d0c57b225d3a9a771edf4f4b4c557430560761d.tar.bz2
refactoring of the QWSSharedMemory class
drop old/unused API and obsolete code; improve attach() for comon case; cache the shm size at the first call; fix the includes; better debug strings; minor code clean-ups Merge-request: 1247 Reviewed-by: Harald Fernengel <harald.fernengel@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/gui/embedded/qwssharedmemory.cpp120
-rw-r--r--src/gui/embedded/qwssharedmemory_p.h34
2 files changed, 47 insertions, 107 deletions
diff --git a/src/gui/embedded/qwssharedmemory.cpp b/src/gui/embedded/qwssharedmemory.cpp
index 66bedee..a677626 100644
--- a/src/gui/embedded/qwssharedmemory.cpp
+++ b/src/gui/embedded/qwssharedmemory.cpp
@@ -43,51 +43,51 @@
#if !defined(QT_NO_QWS_MULTIPROCESS)
+#include <sys/types.h>
+#include <sys/ipc.h>
#include <sys/shm.h>
+//#define QT_SHM_DEBUG
+
QT_BEGIN_NAMESPACE
QWSSharedMemory::QWSSharedMemory()
- : shmBase(0), shmSize(0), character(0), shmId(-1), key(-1)
+ : shmId(-1), shmBase(0), shmSize(0)
{
}
-
QWSSharedMemory::~QWSSharedMemory()
{
detach();
}
-/*
- man page says:
- On Linux, it is possible to attach a shared memory segment even if it
- is already marked to be deleted. However, POSIX.1-2001 does not spec-
- ify this behaviour and many other implementations do not support it.
-*/
-
bool QWSSharedMemory::create(int size)
{
if (shmId != -1)
detach();
- shmId = shmget(IPC_PRIVATE, size, IPC_CREAT|0600);
+ shmId = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
if (shmId == -1) {
#ifdef QT_SHM_DEBUG
- perror("QWSSharedMemory::create allocating shared memory");
+ perror("QWSSharedMemory::create():");
qWarning("Error allocating shared memory of size %d", size);
#endif
return false;
}
- shmBase = shmat(shmId,0,0);
+ shmBase = shmat(shmId, 0, 0);
+ // On Linux, it is possible to attach a shared memory segment even if it
+ // is already marked to be deleted. However, POSIX.1-2001 does not specify
+ // this behaviour and many other implementations do not support it.
shmctl(shmId, IPC_RMID, 0);
- if (shmBase == (void*)-1) {
+ if (shmBase == (void*)-1 || !shmBase) {
#ifdef QT_SHM_DEBUG
- perror("QWSSharedMemory::create attaching to shared memory");
+ perror("QWSSharedMemory::create():");
qWarning("Error attaching to shared memory id %d", shmId);
#endif
- shmBase = 0;
+ detach();
return false;
}
+
return true;
}
@@ -95,91 +95,49 @@ bool QWSSharedMemory::attach(int id)
{
if (shmId == id)
return id != -1;
- if (shmId != -1)
- detach();
- shmBase = shmat(id,0,0);
- if (shmBase == (void*)-1) {
+ detach();
+
+ if (id == -1)
+ return false;
+
+ shmId = id;
+ shmBase = shmat(shmId, 0, 0);
+ if (shmBase == (void*)-1 || !shmBase) {
#ifdef QT_SHM_DEBUG
- perror("QWSSharedMemory::attach attaching to shared memory");
- qWarning("Error attaching to shared memory 0x%x of size %d",
- id, size());
+ perror("QWSSharedMemory::attach():");
+ qWarning("Error attaching to shared memory id %d", shmId);
#endif
- shmBase = 0;
+ detach();
return false;
}
- shmId = id;
+
return true;
}
-
-void QWSSharedMemory::detach ()
+void QWSSharedMemory::detach()
{
- if (!shmBase)
- return;
- shmdt (shmBase);
+ if (shmBase && shmBase != (void*)-1)
+ shmdt(shmBase);
shmBase = 0;
shmSize = 0;
shmId = -1;
}
-void QWSSharedMemory::setPermissions (mode_t mode)
+int QWSSharedMemory::size() const
{
- struct shmid_ds shm;
- shmctl (shmId, IPC_STAT, &shm);
- shm.shm_perm.mode = mode;
- shmctl (shmId, IPC_SET, &shm);
-}
-
-int QWSSharedMemory::size () const
-{
- struct shmid_ds shm;
- shmctl (shmId, IPC_STAT, &shm);
- return shm.shm_segsz;
-}
+ if (shmId == -1)
+ return 0;
+ if (!shmSize) {
+ struct shmid_ds shm;
+ shmctl(shmId, IPC_STAT, &shm);
+ const_cast<QWSSharedMemory *>(this)->shmSize = shm.shm_segsz;
+ }
-// old API
-
-
-
-QWSSharedMemory::QWSSharedMemory (int size, const QString &filename, char c)
-{
- shmSize = size;
- shmFile = filename;
- shmBase = 0;
- shmId = -1;
- character = c;
- key = ftok (shmFile.toLatin1().constData(), c);
-}
-
-
-
-bool QWSSharedMemory::create ()
-{
- shmId = shmget (key, shmSize, IPC_CREAT | 0666);
- return (shmId != -1);
-}
-
-void QWSSharedMemory::destroy ()
-{
- if (shmId != -1)
- shmctl(shmId, IPC_RMID, 0);
-}
-
-bool QWSSharedMemory::attach ()
-{
- if (shmId == -1)
- shmId = shmget (key, shmSize, 0);
-
- shmBase = shmat (shmId, 0, 0);
- if ((long)shmBase == -1)
- shmBase = 0;
-
- return (long)shmBase != 0;
+ return shmSize;
}
-
QT_END_NAMESPACE
#endif // QT_NO_QWS_MULTIPROCESS
diff --git a/src/gui/embedded/qwssharedmemory_p.h b/src/gui/embedded/qwssharedmemory_p.h
index 31e69c4..f3ce241 100644
--- a/src/gui/embedded/qwssharedmemory_p.h
+++ b/src/gui/embedded/qwssharedmemory_p.h
@@ -53,49 +53,31 @@
// We mean it.
//
-#include "qplatformdefs.h"
-#include "QtCore/qstring.h"
+#include <qplatformdefs.h>
QT_BEGIN_NAMESPACE
#if !defined(QT_NO_QWS_MULTIPROCESS)
-class QWSSharedMemory {
+class QWSSharedMemory
+{
public:
-
QWSSharedMemory();
~QWSSharedMemory();
- void setPermissions(mode_t mode);
- int size() const;
- void *address() { return shmBase; }
-
- int id() const { return shmId; }
-
- void detach();
-
bool create(int size);
bool attach(int id);
+ void detach();
- //bool create(int size, const QString &filename, char c = 'Q');
- //bool attach(const QString &filename, char c = 'Q');
-// old API
-
- QWSSharedMemory(int, const QString &, char c = 'Q');
- void * base() { return address(); }
-
- bool create();
- void destroy();
+ int id() const { return shmId; }
- bool attach();
+ void *address() const { return shmBase; }
+ int size() const;
private:
+ int shmId;
void *shmBase;
int shmSize;
- QString shmFile;
- char character;
- int shmId;
- key_t key;
};
#endif // QT_NO_QWS_MULTIPROCESS