Qt 4.8
|
The QSharedMemory class provides access to a shared memory segment. More...
#include <qsharedmemory.h>
Public Types | |
enum | AccessMode { ReadOnly, ReadWrite } |
enum | SharedMemoryError { NoError, PermissionDenied, InvalidSize, KeyError, AlreadyExists, NotFound, LockError, OutOfResources, UnknownError } |
Public Functions | |
bool | attach (AccessMode mode=ReadWrite) |
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(). More... | |
const void * | constData () const |
Returns a const pointer to the contents of the shared memory segment, if one is attached. More... | |
bool | create (int size, AccessMode mode=ReadWrite) |
Creates a shared memory segment of 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 mode and returns true . More... | |
void * | data () |
Returns a pointer to the contents of the shared memory segment, if one is attached. More... | |
const void * | data () const |
bool | detach () |
Detaches the process from the shared memory segment. More... | |
SharedMemoryError | error () const |
Returns a value indicating whether an error occurred, and, if so, which error it was. More... | |
QString | errorString () const |
Returns a text description of the last error that occurred. More... | |
bool | isAttached () const |
Returns true if this process is attached to the shared memory segment. More... | |
QString | key () const |
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey(). More... | |
bool | lock () |
This is a semaphore that locks the shared memory segment for access by this process and returns true. More... | |
QString | nativeKey () const |
Returns the native, platform specific, key for this shared memory object. More... | |
QSharedMemory (QObject *parent=0) | |
QSharedMemory (const QString &key, QObject *parent=0) | |
Constructs a shared memory object with the given parent and with its key set to key. More... | |
void | setKey (const QString &key) |
Sets the platform independent key for this shared memory object. More... | |
void | setNativeKey (const QString &key) |
Sets the native, platform specific, key for this shared memory object. More... | |
int | size () const |
Returns the size of the attached shared memory segment. More... | |
bool | unlock () |
Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process. More... | |
~QSharedMemory () | |
The destructor clears the key, which forces the shared memory object to detach() {detach} from its underlying shared memory segment. More... | |
Public Functions inherited from QObject | |
bool | blockSignals (bool b) |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More... | |
const QObjectList & | children () const |
Returns a list of child objects. More... | |
bool | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0) |
bool | disconnect (const QObject *receiver, const char *member=0) |
void | dumpObjectInfo () |
Dumps information about signal connections, etc. More... | |
void | dumpObjectTree () |
Dumps a tree of children to the debug output. More... | |
QList< QByteArray > | dynamicPropertyNames () const |
Returns the names of all properties that were dynamically added to the object using setProperty(). More... | |
virtual bool | event (QEvent *) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. More... | |
virtual bool | eventFilter (QObject *, QEvent *) |
Filters events if this object has been installed as an event filter for the watched object. More... | |
template<typename T > | |
T | findChild (const QString &aName=QString()) const |
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More... | |
template<typename T > | |
QList< T > | findChildren (const QString &aName=QString()) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More... | |
template<typename T > | |
QList< T > | findChildren (const QRegExp &re) const |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More... | |
void | installEventFilter (QObject *) |
Installs an event filter filterObj on this object. More... | |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false. More... | |
void | killTimer (int id) |
Kills the timer with timer identifier, id. More... | |
virtual const QMetaObject * | metaObject () const |
Returns a pointer to the meta-object of this object. More... | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. More... | |
QString | objectName () const |
QObject * | parent () const |
Returns a pointer to the parent object. More... | |
QVariant | property (const char *name) const |
Returns the value of the object's name property. More... | |
Q_INVOKABLE | QObject (QObject *parent=0) |
Constructs an object with parent object parent. More... | |
void | removeEventFilter (QObject *) |
Removes an event filter object obj from this object. More... | |
void | setObjectName (const QString &name) |
void | setParent (QObject *) |
Makes the object a child of parent. More... | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. More... | |
void | setUserData (uint id, QObjectUserData *data) |
bool | signalsBlocked () const |
Returns true if signals are blocked; otherwise returns false. More... | |
int | startTimer (int interval) |
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (uint id) const |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. More... | |
Related Functions | |
(Note that these are not member functions.) | |
QSharedMemory | |
Constructs a shared memory object with the given parent. More... | |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
Additional Inherited Members | |
Public Slots inherited from QObject | |
void | deleteLater () |
Schedules this object for deletion. More... | |
Signals inherited from QObject | |
void | destroyed (QObject *=0) |
This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More... | |
Static Public Functions inherited from QObject | |
static bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More... | |
static bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
Disconnects signal in object sender from method in object receiver. More... | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static uint | registerUserData () |
static QString | tr (const char *sourceText, const char *comment=0, int n=-1) |
static QString | trUtf8 (const char *sourceText, const char *comment=0, int n=-1) |
Static Public Variables inherited from QObject | |
static const QMetaObject | staticMetaObject |
This variable stores the meta-object for the class. More... | |
Protected Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *) |
This event handler can be reimplemented in a subclass to receive child events. More... | |
virtual void | connectNotify (const char *signal) |
This virtual function is called when something has been connected to signal in this object. More... | |
virtual void | customEvent (QEvent *) |
This event handler can be reimplemented in a subclass to receive custom events. More... | |
virtual void | disconnectNotify (const char *signal) |
This virtual function is called when something has been disconnected from signal in this object. More... | |
QObject (QObjectPrivate &dd, QObject *parent=0) | |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. More... | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More... | |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *) |
This event handler can be reimplemented in a subclass to receive timer events for the object. More... | |
Protected Variables inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
The QSharedMemory class provides access to a shared memory segment.
QSharedMemory provides access to a shared memory segment by multiple threads and processes. It also provides a way for a single thread or process to lock the memory for exclusive access.
When using this class, be aware of the following platform differences:
Windows: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Windows kernel releases the shared memory segment automatically.
Unix: QSharedMemory "owns" the shared memory segment. When the last thread or process that has an instance of QSharedMemory attached to a particular shared memory segment detaches from the segment by destroying its instance of QSharedMemory, the Unix kernel release the shared memory segment. But if that last thread or process crashes without running the QSharedMemory destructor, the shared memory segment survives the crash.
QNX: Due to possible race conditions in the POSIX IPC implementation, create() should be called prior to any attach() calls (even across multiple threads).
HP-UX: Only one attach to a shared memory segment is allowed per process. This means that QSharedMemory should not be used across multiple threads in the same process in HP-UX.
Symbian: QSharedMemory does not "own" the shared memory segment. When all threads or processes that have an instance of QSharedMemory attached to a particular shared memory segment have either destroyed their instance of QSharedMemory or exited, the Symbian kernel releases the shared memory segment automatically. Also, access to a shared memory segment cannot be limited to read-only in Symbian.
Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
Unlike QtSharedMemory, QSharedMemory automatically destroys the shared memory segment when the last instance of QSharedMemory is detached from the segment, and no references to the segment remain. Do not mix using QtSharedMemory and QSharedMemory. Port everything to QSharedMemory.
Definition at line 57 of file qsharedmemory.h.
Enumerator | |
---|---|
ReadOnly | |
ReadWrite |
Definition at line 63 of file qsharedmemory.h.
Enumerator | |
---|---|
NoError | |
PermissionDenied | |
InvalidSize | |
KeyError | |
AlreadyExists | |
NotFound | |
LockError | |
OutOfResources | |
UnknownError |
Definition at line 69 of file qsharedmemory.h.
QSharedMemory::QSharedMemory | ( | QObject * | parent = 0 | ) |
Definition at line 177 of file qsharedmemory.cpp.
Constructs a shared memory object with the given parent and with its key set to key.
Because its key is set, its create() and attach() functions can be called.
Definition at line 189 of file qsharedmemory.cpp.
QSharedMemory::~QSharedMemory | ( | ) |
The destructor clears the key, which forces the shared memory object to detach() {detach} from its underlying shared memory segment.
If this shared memory object is the last one connected to the shared memory segment, the detach() operation destroys the shared memory segment.
Definition at line 204 of file qsharedmemory.cpp.
bool QSharedMemory::attach | ( | AccessMode | mode = ReadWrite | ) |
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 mode is QSharedMemory::ReadWrite by default. It can also be QSharedMemory::ReadOnly . Returns true if the attach operation is successful. If false is returned, call error() to determine which error occurred. After attaching the shared memory segment, a pointer to the shared memory can be obtained by calling data().
Definition at line 425 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure().
const void * QSharedMemory::constData | ( | ) | const |
Returns a const pointer to the contents of the shared memory segment, if one is attached.
Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
Definition at line 504 of file qsharedmemory.cpp.
bool QSharedMemory::create | ( | int | size, |
AccessMode | mode = ReadWrite |
||
) |
Creates a shared memory segment of 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 mode and returns true
.
If a shared memory segment identified by the key already exists, the attach operation is not performed and false
is returned. When the return value is false
, call error() to determine which error occurred.
Definition at line 358 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure().
void * QSharedMemory::data | ( | ) |
Returns a pointer to the contents of the shared memory segment, if one is attached.
Otherwise it returns null. Remember to lock the shared memory with lock() before reading from or writing to the shared memory, and remember to release the lock with unlock() after you are done.
Definition at line 489 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure().
const void * QSharedMemory::data | ( | ) | const |
Definition at line 513 of file qsharedmemory.cpp.
bool QSharedMemory::detach | ( | ) |
Detaches the process from the shared memory segment.
If this was the last process attached to the shared memory segment, then the shared memory segment is released by the system, i.e., the contents are destroyed. The function returns true if it detaches the shared memory segment. If it returns false, it usually means the segment either isn't attached, or it is locked by another process.
Definition at line 465 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure(), QVNCScreen::disconnect(), setKey(), setNativeKey(), and QVNCScreenPrivate::~QVNCScreenPrivate().
QSharedMemory::SharedMemoryError QSharedMemory::error | ( | ) | const |
Returns a value indicating whether an error occurred, and, if so, which error it was.
Definition at line 606 of file qsharedmemory.cpp.
Referenced by QSharedMemoryPrivate::initKey().
QString QSharedMemory::errorString | ( | ) | const |
Returns a text description of the last error that occurred.
If error() returns an QSharedMemory::SharedMemoryError {error value}, call this function to get a text string that describes the error.
Definition at line 620 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure(), and QSharedMemoryPrivate::initKey().
bool QSharedMemory::isAttached | ( | ) | const |
Returns true if this process is attached to the shared memory segment.
Definition at line 449 of file qsharedmemory.cpp.
Referenced by attach(), detach(), setKey(), and setNativeKey().
QString QSharedMemory::key | ( | ) | const |
Returns the key assigned with setKey() to this shared memory, or a null key if no key has been assigned, or if the segment is using a nativeKey().
The key is the identifier used by Qt applications to identify the shared memory segment.
You can find the native, platform specific, key used by the operating system by calling nativeKey().
Definition at line 321 of file qsharedmemory.cpp.
Referenced by QSharedMemoryPrivate::initKey(), setKey(), and setNativeKey().
bool QSharedMemory::lock | ( | ) |
This is a semaphore that locks the shared memory segment for access 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.
Definition at line 531 of file qsharedmemory.cpp.
Referenced by attach(), create(), and detach().
QString QSharedMemory::nativeKey | ( | ) | const |
Returns the native, platform specific, key for this shared memory object.
The native key is the identifier used by the operating system to identify the shared memory segment.
You can use the native key to access shared memory segments that have not been created by Qt, or to grant shared memory access to non-Qt applications.
Definition at line 342 of file qsharedmemory.cpp.
void QSharedMemory::setKey | ( | const QString & | key | ) |
Sets the platform independent key for this shared memory object.
If key is the same as the current key, the function returns without doing anything.
You can call key() to retrieve the platform independent key. Internally, QSharedMemory converts this key into a platform specific key. If you instead call nativeKey(), you will get the platform specific, converted key.
If the shared memory object is attached to an underlying shared memory segment, it will detach() {detach} from it before setting the new key. This function does not do an attach().
Definition at line 223 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure(), QSharedMemory(), and ~QSharedMemory().
void QSharedMemory::setNativeKey | ( | const QString & | key | ) |
Sets the native, platform specific, key for this shared memory object.
If key is the same as the current native key, the function returns without doing anything. If all you want is to assign a key to a segment, you should call setKey() instead.
You can call nativeKey() to retrieve the native key. If a native key has been assigned, calling key() will return a null string.
If the shared memory object is attached to an underlying shared memory segment, it will detach() {detach} from it before setting the new key. This function does not do an attach().
The application will not be portable if you set a native key.
Definition at line 258 of file qsharedmemory.cpp.
int QSharedMemory::size | ( | ) | const |
Returns the size of the attached shared memory segment.
If no shared memory segment is attached, 0 is returned.
Definition at line 395 of file qsharedmemory.cpp.
Referenced by QVNCScreenPrivate::configure().
bool QSharedMemory::unlock | ( | ) |
Releases the lock on the shared memory segment and returns true, if the lock is currently held by this process.
If the segment is not locked, or if the lock is held by another process, nothing happens and false is returned.
Definition at line 556 of file qsharedmemory.cpp.
|
related |
Constructs a shared memory object with the given parent.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
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.