14# include <qt_windows.h>
20# define MAX_PATH PATH_MAX
28#if QT_CONFIG(sharedmemory)
30using namespace QtIpcCommon;
31using namespace Qt::StringLiterals;
33QSharedMemoryPrivate::~QSharedMemoryPrivate()
38inline void QSharedMemoryPrivate::constructBackend()
41 visit([](
auto p) { construct_at(p); });
44inline void QSharedMemoryPrivate::destructBackend()
46 visit([](
auto p) { std::destroy_at(p); });
49#if QT_CONFIG(systemsemaphore)
50inline QNativeIpcKey QSharedMemoryPrivate::semaphoreNativeKey()
const
53 auto suffix =
"_sem"_L1;
54 QString semkey = nativeKey.nativeKey();
55 semkey.truncate(MAX_PATH - suffix.size() - 1);
57 return { semkey, nativeKey.type() };
59 if (isIpcSupported(IpcType::SharedMemory, QNativeIpcKey::Type::Windows)
60 && nativeKey.type() == QNativeIpcKey::Type::Windows) {
62 auto suffix =
"_sem"_L1;
63 QString semkey = nativeKey.nativeKey();
64 semkey.truncate(MAX_PATH - suffix.size() - 1);
66 return { semkey, QNativeIpcKey::Type::Windows };
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
112
113
114
115
116
117
118
119
120
122QSharedMemory::QSharedMemory(QObject *parent)
123 : QSharedMemory(QNativeIpcKey(), parent)
128
129
130
131
132
133
134
135
136QSharedMemory::QSharedMemory(
const QNativeIpcKey &key, QObject *parent)
137 : QObject(*
new QSharedMemoryPrivate(key.type()), parent)
143
144
145
146
147
148
149QSharedMemory::QSharedMemory(
const QString &key, QObject *parent)
150 : QSharedMemory(legacyNativeKey(key), parent)
155
156
157
158
159
160
161
162
163QSharedMemory::~QSharedMemory()
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191void QSharedMemory::setKey(
const QString &key)
193 setNativeKey(legacyNativeKey(key));
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236void QSharedMemory::setNativeKey(
const QNativeIpcKey &key)
239 if (key == d->nativeKey && key.isEmpty())
241 if (!isKeyTypeSupported(key.type())) {
242 d->setError(KeyError, tr(
"%1: unsupported key type")
243 .arg(
"QSharedMemory::setNativeKey"_L1));
250 if (key.type() == d->nativeKey.type()) {
255 d->destructBackend();
257 d->constructBackend();
261bool QSharedMemoryPrivate::initKey(SemaphoreAccessMode mode)
265#if QT_CONFIG(systemsemaphore)
266 const QString legacyKey = QNativeIpcKeyPrivate::legacyKey(nativeKey);
267 const QNativeIpcKey semKey = legacyKey.isEmpty()
268 ? semaphoreNativeKey()
269 : QSystemSemaphore::legacyNativeKey(legacyKey, nativeKey.type());
270 systemSemaphore.setNativeKey(semKey, 1, mode);
271 if (systemSemaphore.error() != QSystemSemaphore::NoError) {
272 QString function =
"QSharedMemoryPrivate::initKey"_L1;
273 errorString = QSharedMemory::tr(
"%1: unable to set key on lock (%2)")
274 .arg(function, systemSemaphore.errorString());
275 switch(systemSemaphore.error()) {
276 case QSystemSemaphore::PermissionDenied:
277 error = QSharedMemory::PermissionDenied;
279 case QSystemSemaphore::KeyError:
280 error = QSharedMemory::KeyError;
282 case QSystemSemaphore::AlreadyExists:
283 error = QSharedMemory::AlreadyExists;
285 case QSystemSemaphore::NotFound:
286 error = QSharedMemory::NotFound;
288 case QSystemSemaphore::OutOfResources:
289 error = QSharedMemory::OutOfResources;
291 case QSystemSemaphore::UnknownError:
293 error = QSharedMemory::UnknownError;
301 errorString = QString();
302 error = QSharedMemory::NoError;
307
308
309
310
311
312
313
314
315
316
317QString QSharedMemory::key()
const
319 Q_D(
const QSharedMemory);
320 return QNativeIpcKeyPrivate::legacyKey(d->nativeKey);
324
325
326
327
328
329
330
331
332
333
334
335
336QString QSharedMemory::nativeKey()
const
338 Q_D(
const QSharedMemory);
339 return d->nativeKey.nativeKey();
343
344
345
346
347
348
349
350
351
352
353
354
355QNativeIpcKey QSharedMemory::nativeIpcKey()
const
357 Q_D(
const QSharedMemory);
362
363
364
365
366
367
368
369
370
371bool QSharedMemory::create(qsizetype size, AccessMode mode)
374 QLatin1StringView function =
"QSharedMemory::create"_L1;
376#if QT_CONFIG(systemsemaphore)
377 if (!d->initKey(QSystemSemaphore::Create))
379 QSharedMemoryLocker lock(
this);
380 if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock, function))
388 d->error = QSharedMemory::InvalidSize;
390 QSharedMemory::tr(
"%1: create size is less then 0").arg(function);
394 if (!d->create(size))
397 return d->attach(mode);
401
402
403
404
405
406
407
408
409qsizetype QSharedMemory::size()
const
411 Q_D(
const QSharedMemory);
416
417
418
419
420
421
422
423
424
425
428
429
430
431
432
433
434
435
436
437
438
439bool QSharedMemory::attach(AccessMode mode)
443 if (isAttached() || !d->initKey({}))
445#if QT_CONFIG(systemsemaphore)
446 QSharedMemoryLocker lock(
this);
447 if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock,
"QSharedMemory::attach"_L1))
451 if (isAttached() || !d->handle())
454 return d->attach(mode);
458
459
460
461
462
463bool QSharedMemory::isAttached()
const
465 Q_D(
const QSharedMemory);
466 return (
nullptr != d->memory);
470
471
472
473
474
475
476
477
478
479bool QSharedMemory::detach()
485#if QT_CONFIG(systemsemaphore)
486 QSharedMemoryLocker lock(
this);
487 if (!d->nativeKey.isEmpty() && !d->tryLocker(&lock,
"QSharedMemory::detach"_L1))
495
496
497
498
499
500
501
502
503
504
505
506void *QSharedMemory::data()
513
514
515
516
517
518
519
520
521
522
523
524const void *QSharedMemory::constData()
const
526 Q_D(
const QSharedMemory);
531
532
533const void *QSharedMemory::data()
const
535 Q_D(
const QSharedMemory);
539#if QT_CONFIG(systemsemaphore)
541
542
543
544
545
546
547
548
549
550
551bool QSharedMemory::lock()
555 qWarning(
"QSharedMemory::lock: already locked");
558 if (d->systemSemaphore.acquire()) {
559 d->lockedByMe =
true;
562 const auto function =
"QSharedMemory::lock"_L1;
563 d->errorString = QSharedMemory::tr(
"%1: unable to lock").arg(function);
564 d->error = QSharedMemory::LockError;
569
570
571
572
573
574
575
576bool QSharedMemory::unlock()
581 d->lockedByMe =
false;
582 if (d->systemSemaphore.release())
584 const auto function =
"QSharedMemory::unlock"_L1;
585 d->errorString = QSharedMemory::tr(
"%1: unable to unlock").arg(function);
586 d->error = QSharedMemory::LockError;
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
621
622
623
624
625
626QSharedMemory::SharedMemoryError QSharedMemory::error()
const
628 Q_D(
const QSharedMemory);
633
634
635
636
637
638
639
640QString QSharedMemory::errorString()
const
642 Q_D(
const QSharedMemory);
643 return d->errorString;
646void QSharedMemoryPrivate::setUnixErrorString(QLatin1StringView function)
651 errorString = QSharedMemory::tr(
"%1: permission denied").arg(function);
652 error = QSharedMemory::PermissionDenied;
655 errorString = QSharedMemory::tr(
"%1: already exists").arg(function);
656 error = QSharedMemory::AlreadyExists;
659 errorString = QSharedMemory::tr(
"%1: doesn't exist").arg(function);
660 error = QSharedMemory::NotFound;
665 errorString = QSharedMemory::tr(
"%1: out of resources").arg(function);
666 error = QSharedMemory::OutOfResources;
669 errorString = QSharedMemory::tr(
"%1: unknown error: %2")
670 .arg(function, qt_error_string(errno));
671 error = QSharedMemory::UnknownError;
672#if defined QSHAREDMEMORY_DEBUG
673 qDebug() << errorString <<
"key" << key <<
"errno" << errno << EINVAL;
678bool QSharedMemory::isKeyTypeSupported(QNativeIpcKey::Type type)
680 if (!isIpcSupported(IpcType::SharedMemory, type))
682 using Variant =
decltype(QSharedMemoryPrivate::backend);
683 return Variant::staticVisit(type, [](
auto ptr) {
684 using Impl = std::decay_t<
decltype(*ptr)>;
685 return Impl::runtimeSupportCheck();
689QNativeIpcKey QSharedMemory::platformSafeKey(
const QString &key, QNativeIpcKey::Type type)
691 return QtIpcCommon::platformSafeKey(key, IpcType::SharedMemory, type);
694QNativeIpcKey QSharedMemory::legacyNativeKey(
const QString &key, QNativeIpcKey::Type type)
696 return QtIpcCommon::legacyPlatformSafeKey(key, IpcType::SharedMemory, type);
703#include "moc_qsharedmemory.cpp"
Combined button and popup list for selecting options.