1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
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
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.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
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.
29
**
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.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "qsharedmemory.h"
43
#include "qsharedmemory_p.h"
44
45
#include "qcore_symbian_p.h"
46
#include <qdebug.h>
47
48
#ifndef QT_NO_SHAREDMEMORY
49
50
#define QSHAREDMEMORY_DEBUG
51
52
QT_BEGIN_NAMESPACE
53
54
QSharedMemoryPrivate::QSharedMemoryPrivate()
55
    : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError),
56
#ifndef QT_NO_SYSTEMSEMAPHORE
57
      systemSemaphore(QString()), lockedByMe(false)
58
#endif
59
{
60
}
61
62
void QSharedMemoryPrivate::setErrorString(const QString &function, TInt errorCode)
63
{
64
    if (errorCode == KErrNone)
65
        return;
66
67
    switch (errorCode) {
68
    case KErrAlreadyExists:
69
        error = QSharedMemory::AlreadyExists;
70
        errorString = QSharedMemory::tr("%1: already exists").arg(function);
71
    break;
72
    case KErrNotFound:
73
        error = QSharedMemory::NotFound;
74
        errorString = QSharedMemory::tr("%1: doesn't exists").arg(function);
75
        break;
76
    case KErrArgument:
77
        error = QSharedMemory::InvalidSize;
78
        errorString = QSharedMemory::tr("%1: invalid size").arg(function);
79
        break;
80
    case KErrNoMemory:
81
        error = QSharedMemory::OutOfResources;
82
        errorString = QSharedMemory::tr("%1: out of resources").arg(function);
83
        break;
84
    case KErrPermissionDenied:
85
        error = QSharedMemory::PermissionDenied;
86
        errorString = QSharedMemory::tr("%1: permission denied").arg(function);
87
        break;
88
    default:
89
        errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(errorCode);
90
        error = QSharedMemory::UnknownError;
91
#if defined QSHAREDMEMORY_DEBUG
92
        qDebug() << errorString << "key" << key;
93
#endif
94
        break;
95
    }
96
}
97
98
key_t QSharedMemoryPrivate::handle()
99
{
100
    // don't allow making handles on empty keys
101
    if (nativeKey.isEmpty()) {
102
        error = QSharedMemory::KeyError;
103
        errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle"));
104
        return 0;
105
    }
106
107
    // Not really cost effective to check here if shared memory is attachable, as it requires
108
    // exactly the same call as attaching, so always assume handle is valid and return failure
109
    // from attach.
110
    return 1;
111
}
112
113
void QSharedMemoryPrivate::cleanHandle()
114
{
115
    chunk.Close();
116
}
117
118
bool QSharedMemoryPrivate::create(int size)
119
{
120
    if (!handle())
121
        return false;
122
123
    TPtrC ptr(qt_QString2TPtrC(nativeKey));
124
125
    TInt err = chunk.CreateGlobal(ptr, size, size);
126
127
    if (err != KErrNone) {
128
        setErrorString(QLatin1String("QSharedMemory::create"), err);
129
        return false;
130
    }
131
132
    // Zero out the created chunk
133
    Mem::FillZ(chunk.Base(), chunk.Size());
134
135
    return true;
136
}
137
138
bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode /* mode */)
139
{
140
    // Grab a pointer to the memory block
141
    if (!chunk.Handle()) {
142
        if (!handle())
143
            return false;
144
145
        TPtrC ptr(qt_QString2TPtrC(nativeKey));
146
147
        TInt err = KErrNoMemory;
148
149
        err = chunk.OpenGlobal(ptr, false);
150
151
        if (err != KErrNone) {
152
            setErrorString(QLatin1String("QSharedMemory::attach"), err);
153
            return false;
154
        }
155
    }
156
157
    size = chunk.Size();
158
    memory = chunk.Base();
159
160
    return true;
161
}
162
163
bool QSharedMemoryPrivate::detach()
164
{
165
    chunk.Close();
166
167
    memory = 0;
168
    size = 0;
169
170
    return true;
171
}
172
173
QT_END_NAMESPACE
174
175
#endif //QT_NO_SHAREDMEMORY