17# define MAX_PATH PATH_MAX
22#if QT_CONFIG(sharedmemory)
24using namespace QtIpcCommon;
27QSharedMemoryPrivate::~QSharedMemoryPrivate()
32inline void QSharedMemoryPrivate::constructBackend()
38inline void QSharedMemoryPrivate::destructBackend()
40 visit([](
auto p) { std::destroy_at(
p); });
43#if QT_CONFIG(systemsemaphore)
44inline QNativeIpcKey QSharedMemoryPrivate::semaphoreNativeKey()
const
46 if (isIpcSupported(IpcType::SharedMemory, QNativeIpcKey::Type::Windows)
47 && nativeKey.type() == QNativeIpcKey::Type::Windows) {
49 auto suffix =
"_sem"_L1;
50 QString semkey = nativeKey.nativeKey();
53 return { semkey, QNativeIpcKey::Type::Windows };
108QSharedMemory::QSharedMemory(
QObject *parent)
109 : QSharedMemory(QNativeIpcKey(), parent)
122QSharedMemory::QSharedMemory(
const QNativeIpcKey &
key,
QObject *parent)
136 : QSharedMemory(legacyNativeKey(
key), parent)
149QSharedMemory::~QSharedMemory()
179 setNativeKey(legacyNativeKey(
key));
222void QSharedMemory::setNativeKey(
const QNativeIpcKey &
key)
225 if (
key ==
d->nativeKey &&
key.isEmpty())
227 if (!isKeyTypeSupported(
key.type())) {
228 d->setError(KeyError,
tr(
"%1: unsupported key type")
229 .
arg(
"QSharedMemory::setNativeKey"_L1));
236 if (
key.type() ==
d->nativeKey.type()) {
241 d->destructBackend();
243 d->constructBackend();
247bool QSharedMemoryPrivate::initKey(SemaphoreAccessMode
mode)
251#if QT_CONFIG(systemsemaphore)
252 const QString legacyKey = QNativeIpcKeyPrivate::legacyKey(nativeKey);
253 const QNativeIpcKey semKey = legacyKey.isEmpty()
254 ? semaphoreNativeKey()
255 : QSystemSemaphore::legacyNativeKey(legacyKey, nativeKey.
type());
256 systemSemaphore.setNativeKey(semKey, 1,
mode);
257 if (systemSemaphore.error() != QSystemSemaphore::NoError) {
258 QString function =
"QSharedMemoryPrivate::initKey"_L1;
259 errorString = QSharedMemory::tr(
"%1: unable to set key on lock (%2)")
260 .arg(function, systemSemaphore.errorString());
261 switch(systemSemaphore.error()) {
262 case QSystemSemaphore::PermissionDenied:
263 error = QSharedMemory::PermissionDenied;
265 case QSystemSemaphore::KeyError:
266 error = QSharedMemory::KeyError;
268 case QSystemSemaphore::AlreadyExists:
269 error = QSharedMemory::AlreadyExists;
271 case QSystemSemaphore::NotFound:
272 error = QSharedMemory::NotFound;
274 case QSystemSemaphore::OutOfResources:
275 error = QSharedMemory::OutOfResources;
277 case QSystemSemaphore::UnknownError:
279 error = QSharedMemory::UnknownError;
288 error = QSharedMemory::NoError;
303QString QSharedMemory::key()
const
305 Q_D(
const QSharedMemory);
306 return QNativeIpcKeyPrivate::legacyKey(
d->nativeKey);
322QString QSharedMemory::nativeKey()
const
324 Q_D(
const QSharedMemory);
325 return d->nativeKey.nativeKey();
341QNativeIpcKey QSharedMemory::nativeIpcKey()
const
343 Q_D(
const QSharedMemory);
362#if QT_CONFIG(systemsemaphore)
363 if (!
d->initKey(QSystemSemaphore::Create))
365 QSharedMemoryLocker
lock(
this);
366 if (!
d->nativeKey.isEmpty() && !
d->tryLocker(&
lock, function))
374 d->error = QSharedMemory::InvalidSize;
376 QSharedMemory::tr(
"%1: create size is less then 0").arg(function);
380 if (!
d->create(
size))
383 return d->attach(
mode);
397 Q_D(
const QSharedMemory);
425bool QSharedMemory::attach(AccessMode
mode)
429 if (isAttached() || !
d->initKey({}))
431#if QT_CONFIG(systemsemaphore)
432 QSharedMemoryLocker
lock(
this);
433 if (!
d->nativeKey.isEmpty() && !
d->tryLocker(&
lock,
"QSharedMemory::attach"_L1))
437 if (isAttached() || !
d->handle())
440 return d->attach(
mode);
449bool QSharedMemory::isAttached()
const
451 Q_D(
const QSharedMemory);
452 return (
nullptr !=
d->memory);
465bool QSharedMemory::detach()
471#if QT_CONFIG(systemsemaphore)
472 QSharedMemoryLocker
lock(
this);
473 if (!
d->nativeKey.isEmpty() && !
d->tryLocker(&
lock,
"QSharedMemory::detach"_L1))
492void *QSharedMemory::data()
510const void *QSharedMemory::constData()
const
512 Q_D(
const QSharedMemory);
519const void *QSharedMemory::data()
const
521 Q_D(
const QSharedMemory);
525#if QT_CONFIG(systemsemaphore)
537bool QSharedMemory::lock()
541 qWarning(
"QSharedMemory::lock: already locked");
544 if (
d->systemSemaphore.acquire()) {
545 d->lockedByMe =
true;
548 const auto function =
"QSharedMemory::lock"_L1;
549 d->errorString = QSharedMemory::tr(
"%1: unable to lock").arg(function);
550 d->error = QSharedMemory::LockError;
562bool QSharedMemory::unlock()
567 d->lockedByMe =
false;
568 if (
d->systemSemaphore.release())
570 const auto function =
"QSharedMemory::unlock"_L1;
571 d->errorString = QSharedMemory::tr(
"%1: unable to unlock").arg(function);
572 d->error = QSharedMemory::LockError;
612QSharedMemory::SharedMemoryError QSharedMemory::error()
const
614 Q_D(
const QSharedMemory);
626QString QSharedMemory::errorString()
const
628 Q_D(
const QSharedMemory);
629 return d->errorString;
637 errorString = QSharedMemory::tr(
"%1: permission denied").arg(function);
638 error = QSharedMemory::PermissionDenied;
641 errorString = QSharedMemory::tr(
"%1: already exists").arg(function);
642 error = QSharedMemory::AlreadyExists;
645 errorString = QSharedMemory::tr(
"%1: doesn't exist").arg(function);
646 error = QSharedMemory::NotFound;
651 errorString = QSharedMemory::tr(
"%1: out of resources").arg(function);
652 error = QSharedMemory::OutOfResources;
655 errorString = QSharedMemory::tr(
"%1: unknown error: %2")
657 error = QSharedMemory::UnknownError;
658#if defined QSHAREDMEMORY_DEBUG
664bool QSharedMemory::isKeyTypeSupported(QNativeIpcKey::Type
type)
666 if (!isIpcSupported(IpcType::SharedMemory,
type))
668 using Variant =
decltype(QSharedMemoryPrivate::backend);
669 return Variant::staticVisit(
type, [](
auto ptr) {
670 using Impl = std::decay_t<
decltype(*ptr)>;
671 return Impl::runtimeSupportCheck();
675QNativeIpcKey QSharedMemory::platformSafeKey(
const QString &
key, QNativeIpcKey::Type
type)
677 return QtIpcCommon::platformSafeKey(
key, IpcType::SharedMemory,
type);
680QNativeIpcKey QSharedMemory::legacyNativeKey(
const QString &
key, QNativeIpcKey::Type
type)
682 return QtIpcCommon::legacyPlatformSafeKey(
key, IpcType::SharedMemory,
type);
689#include "moc_qsharedmemory.cpp"
\macro QT_RESTRICTED_CAST_FROM_ASCII
void truncate(qsizetype pos)
Truncates the string at the given position index.
auto visit(Fn &&fn, QIODevice *socket, Args &&...args)
Combined button and popup list for selecting options.
T * construct_at(T *ptr, Args &&... args)
DBusConnection const char DBusError * error
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
static ControlElement< T > * ptr(QWidget *widget)
GLenum GLuint GLintptr GLsizeiptr size
[1]