1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QSHAREDMEMORY_H
6#define QSHAREDMEMORY_H
7
8#include <QtCore/qtipccommon.h>
9#ifndef QT_NO_QOBJECT
10# include <QtCore/qobject.h>
11#else
12# include <QtCore/qobjectdefs.h>
13# include <QtCore/qscopedpointer.h>
14# include <QtCore/qstring.h>
15#endif
16
17QT_BEGIN_NAMESPACE
18
19#if QT_CONFIG(sharedmemory)
20
21class QSharedMemoryPrivate;
22
23class Q_CORE_EXPORT QSharedMemory : public QObject
24{
25 Q_OBJECT
26 Q_DECLARE_PRIVATE(QSharedMemory)
27
28public:
29 enum AccessMode
30 {
31 ReadOnly,
32 ReadWrite
33 };
34 Q_ENUM(AccessMode)
35
36 enum SharedMemoryError
37 {
38 NoError,
39 PermissionDenied,
40 InvalidSize,
41 KeyError,
42 AlreadyExists,
43 NotFound,
44 LockError,
45 OutOfResources,
46 UnknownError
47 };
48 Q_ENUM(SharedMemoryError)
49
50 QSharedMemory(QObject *parent = nullptr);
51 QSharedMemory(const QNativeIpcKey &key, QObject *parent = nullptr);
52 ~QSharedMemory();
53
54 QSharedMemory(const QString &key, QObject *parent = nullptr);
55 void setKey(const QString &key);
56 QString key() const;
57
58 void setNativeKey(const QNativeIpcKey &key);
59 void setNativeKey(const QString &key, QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs())
60 { setNativeKey({ key, type }); }
61 QString nativeKey() const;
62 QNativeIpcKey nativeIpcKey() const;
63#if QT_CORE_REMOVED_SINCE(6, 5)
64 void setNativeKey(const QString &key);
65#endif
66
67 bool create(qsizetype size, AccessMode mode = ReadWrite);
68 qsizetype size() const;
69
70 bool attach(AccessMode mode = ReadWrite);
71 bool isAttached() const;
72 bool detach();
73
74 void *data();
75 const void* constData() const;
76 const void *data() const;
77
78#if QT_CONFIG(systemsemaphore)
79 bool lock();
80 bool unlock();
81#endif
82
83 SharedMemoryError error() const;
84 QString errorString() const;
85
86 static bool isKeyTypeSupported(QNativeIpcKey::Type type) Q_DECL_CONST_FUNCTION;
87 static QNativeIpcKey platformSafeKey(const QString &key,
88 QNativeIpcKey::Type type = QNativeIpcKey::DefaultTypeForOs);
89 static QNativeIpcKey legacyNativeKey(const QString &key,
90 QNativeIpcKey::Type type = QNativeIpcKey::legacyDefaultTypeForOs());
91
92private:
93 Q_DISABLE_COPY(QSharedMemory)
94};
95
96#endif // QT_CONFIG(sharedmemory)
97
98QT_END_NAMESPACE
99
100#endif // QSHAREDMEMORY_H
101

source code of qtbase/src/corelib/ipc/qsharedmemory.h