Qt 4.8
qsharedmemory_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
45 #include <qdebug.h>
46 
47 #ifndef QT_NO_SHAREDMEMORY
48 
49 //#define QSHAREDMEMORY_DEBUG
50 
52 
54  : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError),
56  systemSemaphore(QString()), lockedByMe(false),
57 #endif
58  hand(0)
59 {
60 }
61 
63 {
64  DWORD windowsError = GetLastError();
65  if (windowsError == 0)
66  return;
67 
68  switch (windowsError) {
69  case ERROR_ALREADY_EXISTS:
71  errorString = QSharedMemory::tr("%1: already exists").arg(function);
72  break;
73  case ERROR_FILE_NOT_FOUND:
74 #ifdef Q_OS_WINCE
75  // This happens on CE only if no file is present as CreateFileMappingW
76  // bails out with this error code
77  case ERROR_INVALID_PARAMETER:
78 #endif
80  errorString = QSharedMemory::tr("%1: doesn't exist").arg(function);
81  break;
82  case ERROR_COMMITMENT_LIMIT:
84  errorString = QSharedMemory::tr("%1: invalid size").arg(function);
85  break;
86  case ERROR_NO_SYSTEM_RESOURCES:
87  case ERROR_NOT_ENOUGH_MEMORY:
89  errorString = QSharedMemory::tr("%1: out of resources").arg(function);
90  break;
91  case ERROR_ACCESS_DENIED:
93  errorString = QSharedMemory::tr("%1: permission denied").arg(function);
94  break;
95  default:
96  errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(windowsError);
98 #ifdef QSHAREDMEMORY_DEBUG
99  qDebug() << errorString << "key" << key;
100 #endif
101  break;
102  }
103 }
104 
106 {
107  if (!hand) {
108  // don't allow making handles on empty keys
109  if (nativeKey.isEmpty()) {
111  errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle"));
112  return 0;
113  }
114 #ifndef Q_OS_WINCE
115  hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)nativeKey.utf16());
116 #else
117  // This works for opening a mapping too, but always opens it with read/write access in
118  // attach as it seems.
119  hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16());
120 #endif
121  if (!hand)
122  setErrorString(QLatin1String("QSharedMemory::handle"));
123  }
124 
125  return hand;
126 }
127 
129 {
130  if (hand != 0 && !CloseHandle(hand))
131  setErrorString(QLatin1String("QSharedMemory::cleanHandle"));
132  hand = 0;
133 }
134 
136 {
137  if (nativeKey.isEmpty()) {
139  errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::create"));
140  return false;
141  }
142 
143  // Create the file mapping.
144  hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16());
145  setErrorString(QLatin1String("QSharedMemory::create"));
146 
147  // hand is valid when it already exists unlike unix so explicitly check
148  return !(error == QSharedMemory::AlreadyExists || !hand);
149 }
150 
152 {
153  // Grab a pointer to the memory block
154  int permissions = (mode == QSharedMemory::ReadOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS);
155  memory = (void *)MapViewOfFile(handle(), permissions, 0, 0, 0);
156  if (0 == memory) {
157  setErrorString(QLatin1String("QSharedMemory::attach"));
158  cleanHandle();
159  return false;
160  }
161 
162  // Grab the size of the memory we have been given (a multiple of 4K on windows)
163  MEMORY_BASIC_INFORMATION info;
164  if (!VirtualQuery(memory, &info, sizeof(info))) {
165  // Windows doesn't set an error code on this one,
166  // it should only be a kernel memory error.
168  errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach"));
169  return false;
170  }
171  size = info.RegionSize;
172 
173  return true;
174 }
175 
177 {
178  // umap memory
179  if (!UnmapViewOfFile(memory)) {
180  setErrorString(QLatin1String("QSharedMemory::detach"));
181  return false;
182  }
183  memory = 0;
184  size = 0;
185 
186  // close handle
187  cleanHandle();
188 
189  return true;
190 }
191 
193 
194 #endif // QT_NO_SHAREDMEMORY
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QSharedMemory::SharedMemoryError error
#define error(msg)
void setErrorString(const QString &function)
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qDebug(const char *,...)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
HANDLE handle()
If not already made create the handle used for accessing the shared memory.
void * HANDLE
Definition: qnamespace.h:1671
bool attach(QSharedMemory::AccessMode mode)
#define QT_NO_SYSTEMSEMAPHORE
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
The QSharedMemory class provides access to a shared memory segment.
Definition: qsharedmemory.h:57
const ushort * utf16() const
Returns the QString as a &#39;\0\&#39;-terminated array of unsigned shorts.
Definition: qstring.cpp:5290