| e5fcad3 by Lars Knoll at 2009-03-23 |
1 |
/**************************************************************************** |
|
2 |
** |
| 89c08c0 by Jason McDonald at 2012-01-11 |
3 |
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). |
| 04e3b30 by Jason McDonald at 2009-09-09 |
4 |
** All rights reserved. |
| 858c70f by Jason McDonald at 2009-06-16 |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
| e5fcad3 by Lars Knoll at 2009-03-23 |
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** GNU Lesser General Public License Usage |
| 1eea52e by Jyri Tahtela at 2011-05-13 |
11 |
** This file may be used under the terms of the GNU Lesser General Public |
|
12 |
** License version 2.1 as published by the Free Software Foundation and |
|
13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
|
14 |
** file. Please review the following information to ensure the GNU Lesser |
|
15 |
** General Public License version 2.1 requirements will be met: |
|
16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| e5fcad3 by Lars Knoll at 2009-03-23 |
17 |
** |
| 04e3b30 by Jason McDonald at 2009-09-09 |
18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 1eea52e by Jyri Tahtela at 2011-05-13 |
19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 04e3b30 by Jason McDonald at 2009-09-09 |
20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| e5fcad3 by Lars Knoll at 2009-03-23 |
21 |
** |
| 1eea52e by Jyri Tahtela at 2011-05-13 |
22 |
** GNU General Public License Usage |
|
23 |
** Alternatively, this file may be used under the terms of the GNU General |
|
24 |
** Public License version 3.0 as published by the Free Software Foundation |
|
25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
|
26 |
** file. Please review the following information to ensure the GNU General |
|
27 |
** Public License version 3.0 requirements will be met: |
|
28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 309db73 by Jason McDonald at 2009-08-31 |
29 |
** |
| 1eea52e by Jyri Tahtela at 2011-05-13 |
30 |
** Other Usage |
|
31 |
** Alternatively, this file may be used in accordance with the terms and |
|
32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 309db73 by Jason McDonald at 2009-08-31 |
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
| e5fcad3 by Lars Knoll at 2009-03-23 |
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
|
|
42 |
#include "qsharedmemory.h" |
|
43 |
#include "qsharedmemory_p.h" |
|
44 |
|
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
45 |
#include <qdebug.h> |
| e5fcad3 by Lars Knoll at 2009-03-23 |
46 |
|
|
47 |
#ifndef QT_NO_SHAREDMEMORY |
|
48 |
|
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
49 |
//#define QSHAREDMEMORY_DEBUG |
|
50 |
|
|
51 |
QT_BEGIN_NAMESPACE |
|
52 |
|
|
53 |
QSharedMemoryPrivate::QSharedMemoryPrivate() |
|
54 |
: QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError), |
|
55 |
#ifndef QT_NO_SYSTEMSEMAPHORE |
|
56 |
systemSemaphore(QString()), lockedByMe(false), |
|
57 |
#endif |
|
58 |
hand(0) |
| e5fcad3 by Lars Knoll at 2009-03-23 |
59 |
{ |
|
60 |
} |
|
61 |
|
|
62 |
void QSharedMemoryPrivate::setErrorString(const QString &function) |
|
63 |
{ |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
64 |
DWORD windowsError = GetLastError(); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
65 |
if (windowsError == 0) |
|
66 |
return; |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
67 |
|
| e5fcad3 by Lars Knoll at 2009-03-23 |
68 |
switch (windowsError) { |
|
69 |
case ERROR_ALREADY_EXISTS: |
|
70 |
error = QSharedMemory::AlreadyExists; |
|
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 |
|
79 |
error = QSharedMemory::NotFound; |
| c9e6533 by Volker Hilsheimer at 2009-07-16 |
80 |
errorString = QSharedMemory::tr("%1: doesn't exist").arg(function); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
81 |
break; |
|
82 |
case ERROR_COMMITMENT_LIMIT: |
|
83 |
error = QSharedMemory::InvalidSize; |
|
84 |
errorString = QSharedMemory::tr("%1: invalid size").arg(function); |
|
85 |
break; |
|
86 |
case ERROR_NO_SYSTEM_RESOURCES: |
|
87 |
case ERROR_NOT_ENOUGH_MEMORY: |
|
88 |
error = QSharedMemory::OutOfResources; |
|
89 |
errorString = QSharedMemory::tr("%1: out of resources").arg(function); |
|
90 |
break; |
|
91 |
case ERROR_ACCESS_DENIED: |
|
92 |
error = QSharedMemory::PermissionDenied; |
|
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); |
|
97 |
error = QSharedMemory::UnknownError; |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
98 |
#ifdef QSHAREDMEMORY_DEBUG |
| e5fcad3 by Lars Knoll at 2009-03-23 |
99 |
qDebug() << errorString << "key" << key; |
|
100 |
#endif |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
101 |
break; |
| e5fcad3 by Lars Knoll at 2009-03-23 |
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
HANDLE QSharedMemoryPrivate::handle() |
|
106 |
{ |
|
107 |
if (!hand) { |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
108 |
// don't allow making handles on empty keys |
| 97a092b by Mirko Damiani at 2010-05-26 |
109 |
if (nativeKey.isEmpty()) { |
| e5fcad3 by Lars Knoll at 2009-03-23 |
110 |
error = QSharedMemory::KeyError; |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
111 |
errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle")); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
112 |
return false; |
|
113 |
} |
|
114 |
#ifndef Q_OS_WINCE |
| 97a092b by Mirko Damiani at 2010-05-26 |
115 |
hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)nativeKey.utf16()); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
116 |
#else |
|
117 |
// This works for opening a mapping too, but always opens it with read/write access in |
|
118 |
// attach as it seems. |
| 97a092b by Mirko Damiani at 2010-05-26 |
119 |
hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16()); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
120 |
#endif |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
121 |
if (!hand) |
|
122 |
setErrorString(QLatin1String("QSharedMemory::handle")); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
123 |
} |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
124 |
|
| e5fcad3 by Lars Knoll at 2009-03-23 |
125 |
return hand; |
|
126 |
} |
|
127 |
|
| 8eaf39f by Ritt Konstantin at 2011-06-06 |
128 |
void QSharedMemoryPrivate::cleanHandle() |
| e5fcad3 by Lars Knoll at 2009-03-23 |
129 |
{ |
| 8eaf39f by Ritt Konstantin at 2011-06-06 |
130 |
if (hand != 0 && !CloseHandle(hand)) |
| e5fcad3 by Lars Knoll at 2009-03-23 |
131 |
setErrorString(QLatin1String("QSharedMemory::cleanHandle")); |
|
132 |
hand = 0; |
|
133 |
} |
|
134 |
|
|
135 |
bool QSharedMemoryPrivate::create(int size) |
|
136 |
{ |
| 97a092b by Mirko Damiani at 2010-05-26 |
137 |
if (nativeKey.isEmpty()) { |
| e5fcad3 by Lars Knoll at 2009-03-23 |
138 |
error = QSharedMemory::KeyError; |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
139 |
errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::create")); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
140 |
return false; |
|
141 |
} |
|
142 |
|
|
143 |
// Create the file mapping. |
| 97a092b by Mirko Damiani at 2010-05-26 |
144 |
hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16()); |
| e2773c3 by Harald Fernengel at 2011-06-08 |
145 |
setErrorString(QLatin1String("QSharedMemory::create")); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
146 |
|
|
147 |
// hand is valid when it already exists unlike unix so explicitly check |
| e2773c3 by Harald Fernengel at 2011-06-08 |
148 |
return !(error == QSharedMemory::AlreadyExists || !hand); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
149 |
} |
|
150 |
|
|
151 |
bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode) |
|
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. |
|
167 |
error = QSharedMemory::UnknownError; |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
168 |
errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach")); |
| e5fcad3 by Lars Knoll at 2009-03-23 |
169 |
return false; |
|
170 |
} |
|
171 |
size = info.RegionSize; |
|
172 |
|
|
173 |
return true; |
|
174 |
} |
|
175 |
|
|
176 |
bool QSharedMemoryPrivate::detach() |
|
177 |
{ |
|
178 |
// umap memory |
|
179 |
if (!UnmapViewOfFile(memory)) { |
|
180 |
setErrorString(QLatin1String("QSharedMemory::detach")); |
|
181 |
return false; |
|
182 |
} |
|
183 |
memory = 0; |
| c17e49e by Ritt Konstantin at 2011-01-21 |
184 |
size = 0; |
| e5fcad3 by Lars Knoll at 2009-03-23 |
185 |
|
|
186 |
// close handle |
| 8eaf39f by Ritt Konstantin at 2011-06-06 |
187 |
cleanHandle(); |
|
188 |
|
|
189 |
return true; |
| e5fcad3 by Lars Knoll at 2009-03-23 |
190 |
} |
|
191 |
|
|
192 |
QT_END_NAMESPACE |
| 11bacd3 by Ritt Konstantin at 2011-06-06 |
193 |
|
|
194 |
#endif // QT_NO_SHAREDMEMORY |