Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qsharedmemory.h" | - |
43 | #include "qsharedmemory_p.h" | - |
44 | #include "qsystemsemaphore.h" | - |
45 | #include <qdir.h> | - |
46 | #include <qcryptographichash.h> | - |
47 | #include <qdebug.h> | - |
48 | #ifdef Q_OS_WIN | - |
49 | # include <qt_windows.h> | - |
50 | #endif | - |
51 | | - |
52 | QT_BEGIN_NAMESPACE | - |
53 | | - |
54 | #if !(defined(QT_NO_SHAREDMEMORY) && defined(QT_NO_SYSTEMSEMAPHORE)) | - |
55 | /*! | - |
56 | \internal | - |
57 | | - |
58 | Generate a string from the key which can be any unicode string into | - |
59 | the subset that the win/unix kernel allows. | - |
60 | | - |
61 | On Unix this will be a file name | - |
62 | */ | - |
63 | QString | - |
64 | QSharedMemoryPrivate::makePlatformSafeKey(const QString &key, | - |
65 | const QString &prefix) | - |
66 | { | - |
67 | if (key.isEmpty()) evaluated: key.isEmpty() yes Evaluation Count:1 | yes Evaluation Count:46 |
| 1-46 |
68 | return QString(); executed: return QString(); Execution Count:1 | 1 |
69 | | - |
70 | QString result = prefix; executed (the execution status of this line is deduced): QString result = prefix; | - |
71 | | - |
72 | QString part1 = key; executed (the execution status of this line is deduced): QString part1 = key; | - |
73 | part1.replace(QRegExp(QLatin1String("[^A-Za-z]")), QString()); executed (the execution status of this line is deduced): part1.replace(QRegExp(QLatin1String("[^A-Za-z]")), QString()); | - |
74 | result.append(part1); executed (the execution status of this line is deduced): result.append(part1); | - |
75 | | - |
76 | QByteArray hex = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1).toHex(); executed (the execution status of this line is deduced): QByteArray hex = QCryptographicHash::hash(key.toUtf8(), QCryptographicHash::Sha1).toHex(); | - |
77 | result.append(QLatin1String(hex)); executed (the execution status of this line is deduced): result.append(QLatin1String(hex)); | - |
78 | #ifdef Q_OS_WIN | - |
79 | return result; | - |
80 | #else | - |
81 | return QDir::tempPath() + QLatin1Char('/') + result; executed: return QDir::tempPath() + QLatin1Char('/') + result; Execution Count:46 | 46 |
82 | #endif | - |
83 | } | - |
84 | #endif // QT_NO_SHAREDMEMORY && QT_NO_SHAREDMEMORY | - |
85 | | - |
86 | #ifndef QT_NO_SHAREDMEMORY | - |
87 | | - |
88 | /*! | - |
89 | \class QSharedMemory | - |
90 | \inmodule QtCore | - |
91 | \since 4.4 | - |
92 | | - |
93 | \brief The QSharedMemory class provides access to a shared memory segment. | - |
94 | | - |
95 | QSharedMemory provides access to a shared memory segment by multiple | - |
96 | threads and processes. It also provides a way for a single thread or | - |
97 | process to lock the memory for exclusive access. | - |
98 | | - |
99 | When using this class, be aware of the following platform | - |
100 | differences: | - |
101 | | - |
102 | \list | - |
103 | | - |
104 | \li Windows: QSharedMemory does not "own" the shared memory segment. | - |
105 | When all threads or processes that have an instance of QSharedMemory | - |
106 | attached to a particular shared memory segment have either destroyed | - |
107 | their instance of QSharedMemory or exited, the Windows kernel | - |
108 | releases the shared memory segment automatically. | - |
109 | | - |
110 | \li Unix: QSharedMemory "owns" the shared memory segment. When the | - |
111 | last thread or process that has an instance of QSharedMemory | - |
112 | attached to a particular shared memory segment detaches from the | - |
113 | segment by destroying its instance of QSharedMemory, the Unix kernel | - |
114 | release the shared memory segment. But if that last thread or | - |
115 | process crashes without running the QSharedMemory destructor, the | - |
116 | shared memory segment survives the crash. | - |
117 | | - |
118 | \li HP-UX: Only one attach to a shared memory segment is allowed per | - |
119 | process. This means that QSharedMemory should not be used across | - |
120 | multiple threads in the same process in HP-UX. | - |
121 | | - |
122 | \endlist | - |
123 | | - |
124 | Remember to lock the shared memory with lock() before reading from | - |
125 | or writing to the shared memory, and remember to release the lock | - |
126 | with unlock() after you are done. | - |
127 | | - |
128 | Unlike QtSharedMemory, QSharedMemory automatically destroys the | - |
129 | shared memory segment when the last instance of QSharedMemory is | - |
130 | detached from the segment, and no references to the segment | - |
131 | remain. Do not mix using QtSharedMemory and QSharedMemory. Port | - |
132 | everything to QSharedMemory. | - |
133 | | - |
134 | \warning QSharedMemory changes the key in a Qt-specific way, unless otherwise | - |
135 | specified. Interoperation with non-Qt applications is achieved by first creating | - |
136 | a default shared memory with QSharedMemory() and then setting a native key with | - |
137 | setNativeKey(). When using native keys, shared memory is not protected against | - |
138 | multiple accesses on it (e.g. unable to lock()) and a user-defined mechanism | - |
139 | should be used to achieve a such protection. | - |
140 | */ | - |
141 | | - |
142 | /*! | - |
143 | \overload QSharedMemory() | - |
144 | | - |
145 | Constructs a shared memory object with the given \a parent. The | - |
146 | shared memory object's key is not set by the constructor, so the | - |
147 | shared memory object does not have an underlying shared memory | - |
148 | segment attached. The key must be set with setKey() or setNativeKey() | - |
149 | before create() or attach() can be used. | - |
150 | | - |
151 | \sa setKey() | - |
152 | */ | - |
153 | QSharedMemory::QSharedMemory(QObject *parent) | - |
154 | : QObject(*new QSharedMemoryPrivate, parent) | - |
155 | { | - |
156 | } executed: } Execution Count:1 | 1 |
157 | | - |
158 | /*! | - |
159 | Constructs a shared memory object with the given \a parent and with | - |
160 | its key set to \a key. Because its key is set, its create() and | - |
161 | attach() functions can be called. | - |
162 | | - |
163 | \sa setKey(), create(), attach() | - |
164 | */ | - |
165 | QSharedMemory::QSharedMemory(const QString &key, QObject *parent) | - |
166 | : QObject(*new QSharedMemoryPrivate, parent) | - |
167 | { | - |
168 | setKey(key); never executed (the execution status of this line is deduced): setKey(key); | - |
169 | } | 0 |
170 | | - |
171 | /*! | - |
172 | The destructor clears the key, which forces the shared memory object | - |
173 | to \l {detach()} {detach} from its underlying shared memory | - |
174 | segment. If this shared memory object is the last one connected to | - |
175 | the shared memory segment, the detach() operation destroys the | - |
176 | shared memory segment. | - |
177 | | - |
178 | \sa detach(), isAttached() | - |
179 | */ | - |
180 | QSharedMemory::~QSharedMemory() | - |
181 | { | - |
182 | setKey(QString()); executed (the execution status of this line is deduced): setKey(QString()); | - |
183 | } executed: } Execution Count:1 | 1 |
184 | | - |
185 | /*! | - |
186 | Sets the platform independent \a key for this shared memory object. If \a key | - |
187 | is the same as the current key, the function returns without doing anything. | - |
188 | | - |
189 | You can call key() to retrieve the platform independent key. Internally, | - |
190 | QSharedMemory converts this key into a platform specific key. If you instead | - |
191 | call nativeKey(), you will get the platform specific, converted key. | - |
192 | | - |
193 | If the shared memory object is attached to an underlying shared memory | - |
194 | segment, it will \l {detach()} {detach} from it before setting the new key. | - |
195 | This function does not do an attach(). | - |
196 | | - |
197 | \sa key(), nativeKey(), isAttached() | - |
198 | */ | - |
199 | void QSharedMemory::setKey(const QString &key) | - |
200 | { | - |
201 | Q_D(QSharedMemory); executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
202 | if (key == d->key && d->makePlatformSafeKey(key) == d->nativeKey) partially evaluated: key == d->key yes Evaluation Count:1 | no Evaluation Count:0 |
partially evaluated: d->makePlatformSafeKey(key) == d->nativeKey yes Evaluation Count:1 | no Evaluation Count:0 |
| 0-1 |
203 | return; executed: return; Execution Count:1 | 1 |
204 | | - |
205 | if (isAttached()) never evaluated: isAttached() | 0 |
206 | detach(); never executed: detach(); | 0 |
207 | d->cleanHandle(); never executed (the execution status of this line is deduced): d->cleanHandle(); | - |
208 | d->key = key; never executed (the execution status of this line is deduced): d->key = key; | - |
209 | d->nativeKey = d->makePlatformSafeKey(key); never executed (the execution status of this line is deduced): d->nativeKey = d->makePlatformSafeKey(key); | - |
210 | } | 0 |
211 | | - |
212 | /*! | - |
213 | \since 4.8 | - |
214 | | - |
215 | Sets the native, platform specific, \a key for this shared memory object. If | - |
216 | \a key is the same as the current native key, the function returns without | - |
217 | doing anything. If all you want is to assign a key to a segment, you should | - |
218 | call setKey() instead. | - |
219 | | - |
220 | You can call nativeKey() to retrieve the native key. If a native key has been | - |
221 | assigned, calling key() will return a null string. | - |
222 | | - |
223 | If the shared memory object is attached to an underlying shared memory | - |
224 | segment, it will \l {detach()} {detach} from it before setting the new key. | - |
225 | This function does not do an attach(). | - |
226 | | - |
227 | The application will not be portable if you set a native key. | - |
228 | | - |
229 | \sa nativeKey(), key(), isAttached() | - |
230 | */ | - |
231 | void QSharedMemory::setNativeKey(const QString &key) | - |
232 | { | - |
233 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
234 | if (key == d->nativeKey && d->key.isNull()) never evaluated: key == d->nativeKey never evaluated: d->key.isNull() | 0 |
235 | return; | 0 |
236 | | - |
237 | if (isAttached()) never evaluated: isAttached() | 0 |
238 | detach(); never executed: detach(); | 0 |
239 | d->cleanHandle(); never executed (the execution status of this line is deduced): d->cleanHandle(); | - |
240 | d->key = QString(); never executed (the execution status of this line is deduced): d->key = QString(); | - |
241 | d->nativeKey = key; never executed (the execution status of this line is deduced): d->nativeKey = key; | - |
242 | } | 0 |
243 | | - |
244 | bool QSharedMemoryPrivate::initKey() | - |
245 | { | - |
246 | if (!cleanHandle()) never evaluated: !cleanHandle() | 0 |
247 | return false; never executed: return false; | 0 |
248 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
249 | systemSemaphore.setKey(QString(), 1); never executed (the execution status of this line is deduced): systemSemaphore.setKey(QString(), 1); | - |
250 | systemSemaphore.setKey(key, 1); never executed (the execution status of this line is deduced): systemSemaphore.setKey(key, 1); | - |
251 | if (systemSemaphore.error() != QSystemSemaphore::NoError) { never evaluated: systemSemaphore.error() != QSystemSemaphore::NoError | 0 |
252 | QString function = QLatin1String("QSharedMemoryPrivate::initKey"); never executed (the execution status of this line is deduced): QString function = QLatin1String("QSharedMemoryPrivate::initKey"); | - |
253 | errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function); never executed (the execution status of this line is deduced): errorString = QSharedMemory::tr("%1: unable to set key on lock").arg(function); | - |
254 | switch(systemSemaphore.error()) { | - |
255 | case QSystemSemaphore::PermissionDenied: | - |
256 | error = QSharedMemory::PermissionDenied; never executed (the execution status of this line is deduced): error = QSharedMemory::PermissionDenied; | - |
257 | break; | 0 |
258 | case QSystemSemaphore::KeyError: | - |
259 | error = QSharedMemory::KeyError; never executed (the execution status of this line is deduced): error = QSharedMemory::KeyError; | - |
260 | break; | 0 |
261 | case QSystemSemaphore::AlreadyExists: | - |
262 | error = QSharedMemory::AlreadyExists; never executed (the execution status of this line is deduced): error = QSharedMemory::AlreadyExists; | - |
263 | break; | 0 |
264 | case QSystemSemaphore::NotFound: | - |
265 | error = QSharedMemory::NotFound; never executed (the execution status of this line is deduced): error = QSharedMemory::NotFound; | - |
266 | break; | 0 |
267 | case QSystemSemaphore::OutOfResources: | - |
268 | error = QSharedMemory::OutOfResources; never executed (the execution status of this line is deduced): error = QSharedMemory::OutOfResources; | - |
269 | break; | 0 |
270 | case QSystemSemaphore::UnknownError: | - |
271 | default: | - |
272 | error = QSharedMemory::UnknownError; never executed (the execution status of this line is deduced): error = QSharedMemory::UnknownError; | - |
273 | break; | 0 |
274 | } | - |
275 | return false; never executed: return false; | 0 |
276 | } | - |
277 | #endif | - |
278 | errorString = QString(); never executed (the execution status of this line is deduced): errorString = QString(); | - |
279 | error = QSharedMemory::NoError; never executed (the execution status of this line is deduced): error = QSharedMemory::NoError; | - |
280 | return true; never executed: return true; | 0 |
281 | } | - |
282 | | - |
283 | /*! | - |
284 | Returns the key assigned with setKey() to this shared memory, or a null key | - |
285 | if no key has been assigned, or if the segment is using a nativeKey(). The | - |
286 | key is the identifier used by Qt applications to identify the shared memory | - |
287 | segment. | - |
288 | | - |
289 | You can find the native, platform specific, key used by the operating system | - |
290 | by calling nativeKey(). | - |
291 | | - |
292 | \sa setKey(), setNativeKey() | - |
293 | */ | - |
294 | QString QSharedMemory::key() const | - |
295 | { | - |
296 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
297 | return d->key; never executed: return d->key; | 0 |
298 | } | - |
299 | | - |
300 | /*! | - |
301 | \since 4.8 | - |
302 | | - |
303 | Returns the native, platform specific, key for this shared memory object. The | - |
304 | native key is the identifier used by the operating system to identify the | - |
305 | shared memory segment. | - |
306 | | - |
307 | You can use the native key to access shared memory segments that have not | - |
308 | been created by Qt, or to grant shared memory access to non-Qt applications. | - |
309 | | - |
310 | \sa setKey(), setNativeKey() | - |
311 | */ | - |
312 | QString QSharedMemory::nativeKey() const | - |
313 | { | - |
314 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
315 | return d->nativeKey; never executed: return d->nativeKey; | 0 |
316 | } | - |
317 | | - |
318 | /*! | - |
319 | Creates a shared memory segment of \a size bytes with the key passed to the | - |
320 | constructor, set with setKey() or set with setNativeKey(), then attaches to | - |
321 | the new shared memory segment with the given access \a mode and returns | - |
322 | \tt true. If a shared memory segment identified by the key already exists, | - |
323 | the attach operation is not performed and \tt false is returned. When the | - |
324 | return value is \tt false, call error() to determine which error occurred. | - |
325 | | - |
326 | \sa error() | - |
327 | */ | - |
328 | bool QSharedMemory::create(int size, AccessMode mode) | - |
329 | { | - |
330 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
331 | | - |
332 | if (!d->initKey()) never evaluated: !d->initKey() | 0 |
333 | return false; never executed: return false; | 0 |
334 | | - |
335 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
336 | #ifndef Q_OS_WIN | - |
337 | // Take ownership and force set initialValue because the semaphore | - |
338 | // might have already existed from a previous crash. | - |
339 | d->systemSemaphore.setKey(d->key, 1, QSystemSemaphore::Create); never executed (the execution status of this line is deduced): d->systemSemaphore.setKey(d->key, 1, QSystemSemaphore::Create); | - |
340 | #endif | - |
341 | #endif | - |
342 | | - |
343 | QString function = QLatin1String("QSharedMemory::create"); never executed (the execution status of this line is deduced): QString function = QLatin1String("QSharedMemory::create"); | - |
344 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
345 | QSharedMemoryLocker lock(this); never executed (the execution status of this line is deduced): QSharedMemoryLocker lock(this); | - |
346 | if (!d->key.isNull() && !d->tryLocker(&lock, function)) never evaluated: !d->key.isNull() never evaluated: !d->tryLocker(&lock, function) | 0 |
347 | return false; never executed: return false; | 0 |
348 | #endif | - |
349 | | - |
350 | if (size <= 0) { never evaluated: size <= 0 | 0 |
351 | d->error = QSharedMemory::InvalidSize; never executed (the execution status of this line is deduced): d->error = QSharedMemory::InvalidSize; | - |
352 | d->errorString = never executed (the execution status of this line is deduced): d->errorString = | - |
353 | QSharedMemory::tr("%1: create size is less then 0").arg(function); never executed (the execution status of this line is deduced): QSharedMemory::tr("%1: create size is less then 0").arg(function); | - |
354 | return false; never executed: return false; | 0 |
355 | } | - |
356 | | - |
357 | if (!d->create(size)) never evaluated: !d->create(size) | 0 |
358 | return false; never executed: return false; | 0 |
359 | | - |
360 | return d->attach(mode); never executed: return d->attach(mode); | 0 |
361 | } | - |
362 | | - |
363 | /*! | - |
364 | Returns the size of the attached shared memory segment. If no shared | - |
365 | memory segment is attached, 0 is returned. | - |
366 | | - |
367 | \sa create(), attach() | - |
368 | */ | - |
369 | int QSharedMemory::size() const | - |
370 | { | - |
371 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
372 | return d->size; never executed: return d->size; | 0 |
373 | } | - |
374 | | - |
375 | /*! | - |
376 | \enum QSharedMemory::AccessMode | - |
377 | | - |
378 | \value ReadOnly The shared memory segment is read-only. Writing to | - |
379 | the shared memory segment is not allowed. An attempt to write to a | - |
380 | shared memory segment created with ReadOnly causes the program to | - |
381 | abort. | - |
382 | | - |
383 | \value ReadWrite Reading and writing the shared memory segment are | - |
384 | both allowed. | - |
385 | */ | - |
386 | | - |
387 | /*! | - |
388 | Attempts to attach the process to the shared memory segment | - |
389 | identified by the key that was passed to the constructor or to a | - |
390 | call to setKey() or setNativeKey(). The access \a mode is \l {QSharedMemory::} | - |
391 | {ReadWrite} by default. It can also be \l {QSharedMemory::} | - |
392 | {ReadOnly}. Returns true if the attach operation is successful. If | - |
393 | false is returned, call error() to determine which error occurred. | - |
394 | After attaching the shared memory segment, a pointer to the shared | - |
395 | memory can be obtained by calling data(). | - |
396 | | - |
397 | \sa isAttached(), detach(), create() | - |
398 | */ | - |
399 | bool QSharedMemory::attach(AccessMode mode) | - |
400 | { | - |
401 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
402 | | - |
403 | if (isAttached() || !d->initKey()) never evaluated: isAttached() never evaluated: !d->initKey() | 0 |
404 | return false; never executed: return false; | 0 |
405 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
406 | QSharedMemoryLocker lock(this); never executed (the execution status of this line is deduced): QSharedMemoryLocker lock(this); | - |
407 | if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::attach"))) never evaluated: !d->key.isNull() never evaluated: !d->tryLocker(&lock, QLatin1String("QSharedMemory::attach")) | 0 |
408 | return false; never executed: return false; | 0 |
409 | #endif | - |
410 | | - |
411 | if (isAttached() || !d->handle()) never evaluated: isAttached() never evaluated: !d->handle() | 0 |
412 | return false; never executed: return false; | 0 |
413 | | - |
414 | return d->attach(mode); never executed: return d->attach(mode); | 0 |
415 | } | - |
416 | | - |
417 | /*! | - |
418 | Returns true if this process is attached to the shared memory | - |
419 | segment. | - |
420 | | - |
421 | \sa attach(), detach() | - |
422 | */ | - |
423 | bool QSharedMemory::isAttached() const | - |
424 | { | - |
425 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
426 | return (0 != d->memory); never executed: return (0 != d->memory); | 0 |
427 | } | - |
428 | | - |
429 | /*! | - |
430 | Detaches the process from the shared memory segment. If this was the | - |
431 | last process attached to the shared memory segment, then the shared | - |
432 | memory segment is released by the system, i.e., the contents are | - |
433 | destroyed. The function returns true if it detaches the shared | - |
434 | memory segment. If it returns false, it usually means the segment | - |
435 | either isn't attached, or it is locked by another process. | - |
436 | | - |
437 | \sa attach(), isAttached() | - |
438 | */ | - |
439 | bool QSharedMemory::detach() | - |
440 | { | - |
441 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
442 | if (!isAttached()) never evaluated: !isAttached() | 0 |
443 | return false; never executed: return false; | 0 |
444 | | - |
445 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
446 | QSharedMemoryLocker lock(this); never executed (the execution status of this line is deduced): QSharedMemoryLocker lock(this); | - |
447 | if (!d->key.isNull() && !d->tryLocker(&lock, QLatin1String("QSharedMemory::detach"))) never evaluated: !d->key.isNull() never evaluated: !d->tryLocker(&lock, QLatin1String("QSharedMemory::detach")) | 0 |
448 | return false; never executed: return false; | 0 |
449 | #endif | - |
450 | | - |
451 | return d->detach(); never executed: return d->detach(); | 0 |
452 | } | - |
453 | | - |
454 | /*! | - |
455 | Returns a pointer to the contents of the shared memory segment, if | - |
456 | one is attached. Otherwise it returns null. Remember to lock the | - |
457 | shared memory with lock() before reading from or writing to the | - |
458 | shared memory, and remember to release the lock with unlock() after | - |
459 | you are done. | - |
460 | | - |
461 | \sa attach() | - |
462 | */ | - |
463 | void *QSharedMemory::data() | - |
464 | { | - |
465 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
466 | return d->memory; never executed: return d->memory; | 0 |
467 | } | - |
468 | | - |
469 | /*! | - |
470 | Returns a const pointer to the contents of the shared memory | - |
471 | segment, if one is attached. Otherwise it returns null. Remember to | - |
472 | lock the shared memory with lock() before reading from or writing to | - |
473 | the shared memory, and remember to release the lock with unlock() | - |
474 | after you are done. | - |
475 | | - |
476 | \sa attach(), create() | - |
477 | */ | - |
478 | const void* QSharedMemory::constData() const | - |
479 | { | - |
480 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
481 | return d->memory; never executed: return d->memory; | 0 |
482 | } | - |
483 | | - |
484 | /*! | - |
485 | \overload data() | - |
486 | */ | - |
487 | const void *QSharedMemory::data() const | - |
488 | { | - |
489 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
490 | return d->memory; never executed: return d->memory; | 0 |
491 | } | - |
492 | | - |
493 | #ifndef QT_NO_SYSTEMSEMAPHORE | - |
494 | /*! | - |
495 | This is a semaphore that locks the shared memory segment for access | - |
496 | by this process and returns true. If another process has locked the | - |
497 | segment, this function blocks until the lock is released. Then it | - |
498 | acquires the lock and returns true. If this function returns false, | - |
499 | it means that you have ignored a false return from create() or attach(), | - |
500 | that you have set the key with setNativeKey() or that | - |
501 | QSystemSemaphore::acquire() failed due to an unknown system error. | - |
502 | | - |
503 | \sa unlock(), data(), QSystemSemaphore::acquire() | - |
504 | */ | - |
505 | bool QSharedMemory::lock() | - |
506 | { | - |
507 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
508 | if (d->lockedByMe) { never evaluated: d->lockedByMe | 0 |
509 | qWarning("QSharedMemory::lock: already locked"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsharedmemory.cpp", 509, __PRETTY_FUNCTION__).warning("QSharedMemory::lock: already locked"); | - |
510 | return true; never executed: return true; | 0 |
511 | } | - |
512 | if (d->systemSemaphore.acquire()) { never evaluated: d->systemSemaphore.acquire() | 0 |
513 | d->lockedByMe = true; never executed (the execution status of this line is deduced): d->lockedByMe = true; | - |
514 | return true; never executed: return true; | 0 |
515 | } | - |
516 | QString function = QLatin1String("QSharedMemory::lock"); never executed (the execution status of this line is deduced): QString function = QLatin1String("QSharedMemory::lock"); | - |
517 | d->errorString = QSharedMemory::tr("%1: unable to lock").arg(function); never executed (the execution status of this line is deduced): d->errorString = QSharedMemory::tr("%1: unable to lock").arg(function); | - |
518 | d->error = QSharedMemory::LockError; never executed (the execution status of this line is deduced): d->error = QSharedMemory::LockError; | - |
519 | return false; never executed: return false; | 0 |
520 | } | - |
521 | | - |
522 | /*! | - |
523 | Releases the lock on the shared memory segment and returns true, if | - |
524 | the lock is currently held by this process. If the segment is not | - |
525 | locked, or if the lock is held by another process, nothing happens | - |
526 | and false is returned. | - |
527 | | - |
528 | \sa lock() | - |
529 | */ | - |
530 | bool QSharedMemory::unlock() | - |
531 | { | - |
532 | Q_D(QSharedMemory); never executed (the execution status of this line is deduced): QSharedMemoryPrivate * const d = d_func(); | - |
533 | if (!d->lockedByMe) never evaluated: !d->lockedByMe | 0 |
534 | return false; never executed: return false; | 0 |
535 | d->lockedByMe = false; never executed (the execution status of this line is deduced): d->lockedByMe = false; | - |
536 | if (d->systemSemaphore.release()) never evaluated: d->systemSemaphore.release() | 0 |
537 | return true; never executed: return true; | 0 |
538 | QString function = QLatin1String("QSharedMemory::unlock"); never executed (the execution status of this line is deduced): QString function = QLatin1String("QSharedMemory::unlock"); | - |
539 | d->errorString = QSharedMemory::tr("%1: unable to unlock").arg(function); never executed (the execution status of this line is deduced): d->errorString = QSharedMemory::tr("%1: unable to unlock").arg(function); | - |
540 | d->error = QSharedMemory::LockError; never executed (the execution status of this line is deduced): d->error = QSharedMemory::LockError; | - |
541 | return false; never executed: return false; | 0 |
542 | } | - |
543 | #endif // QT_NO_SYSTEMSEMAPHORE | - |
544 | | - |
545 | /*! | - |
546 | \enum QSharedMemory::SharedMemoryError | - |
547 | | - |
548 | \value NoError No error occurred. | - |
549 | | - |
550 | \value PermissionDenied The operation failed because the caller | - |
551 | didn't have the required permissions. | - |
552 | | - |
553 | \value InvalidSize A create operation failed because the requested | - |
554 | size was invalid. | - |
555 | | - |
556 | \value KeyError The operation failed because of an invalid key. | - |
557 | | - |
558 | \value AlreadyExists A create() operation failed because a shared | - |
559 | memory segment with the specified key already existed. | - |
560 | | - |
561 | \value NotFound An attach() failed because a shared memory segment | - |
562 | with the specified key could not be found. | - |
563 | | - |
564 | \value LockError The attempt to lock() the shared memory segment | - |
565 | failed because create() or attach() failed and returned false, or | - |
566 | because a system error occurred in QSystemSemaphore::acquire(). | - |
567 | | - |
568 | \value OutOfResources A create() operation failed because there was | - |
569 | not enough memory available to fill the request. | - |
570 | | - |
571 | \value UnknownError Something else happened and it was bad. | - |
572 | */ | - |
573 | | - |
574 | /*! | - |
575 | Returns a value indicating whether an error occurred, and, if so, | - |
576 | which error it was. | - |
577 | | - |
578 | \sa errorString() | - |
579 | */ | - |
580 | QSharedMemory::SharedMemoryError QSharedMemory::error() const | - |
581 | { | - |
582 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
583 | return d->error; never executed: return d->error; | 0 |
584 | } | - |
585 | | - |
586 | /*! | - |
587 | Returns a text description of the last error that occurred. If | - |
588 | error() returns an \l {QSharedMemory::SharedMemoryError} {error | - |
589 | value}, call this function to get a text string that describes the | - |
590 | error. | - |
591 | | - |
592 | \sa error() | - |
593 | */ | - |
594 | QString QSharedMemory::errorString() const | - |
595 | { | - |
596 | Q_D(const QSharedMemory); never executed (the execution status of this line is deduced): const QSharedMemoryPrivate * const d = d_func(); | - |
597 | return d->errorString; never executed: return d->errorString; | 0 |
598 | } | - |
599 | | - |
600 | #endif // QT_NO_SHAREDMEMORY | - |
601 | | - |
602 | QT_END_NAMESPACE | - |
603 | | - |
| | |