qsharedmemory.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9