8f427b2 by axis at 2009-04-24 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.
da8dc76 by axis at 2009-08-06 5
** Contact: Nokia Corporation (qt-info@nokia.com)
8f427b2 by axis at 2009-04-24 6
**
bec7a9c by Jason McDonald at 2009-10-06 7
** This file is part of the QtCore module of the Qt Toolkit.
8f427b2 by axis at 2009-04-24 8
**
a014c07 by axis at 2009-06-03 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.
a014c07 by axis at 2009-06-03 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.
a014c07 by axis at 2009-06-03 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.
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.
115a995 by Jason McDonald at 2009-08-31 33
**
34
**
35
**
36
**
a014c07 by axis at 2009-06-03 37
**
38
** $QT_END_LICENSE$
8f427b2 by axis at 2009-04-24 39
**
40
****************************************************************************/
41
42
#include "qsharedmemory.h"
43
#include "qsharedmemory_p.h"
11bacd3 by Ritt Konstantin at 2011-06-06 44
8f427b2 by axis at 2009-04-24 45
#include "qcore_symbian_p.h"
46
#include <qdebug.h>
47
48
#ifndef QT_NO_SHAREDMEMORY
49
50
#define QSHAREDMEMORY_DEBUG
51
11bacd3 by Ritt Konstantin at 2011-06-06 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
8f427b2 by axis at 2009-04-24 59
{
60
}
61
62
void QSharedMemoryPrivate::setErrorString(const QString &function, TInt errorCode)
63
{
64
    if (errorCode == KErrNone)
65
        return;
11bacd3 by Ritt Konstantin at 2011-06-06 66
8f427b2 by axis at 2009-04-24 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
11bacd3 by Ritt Konstantin at 2011-06-06 94
        break;
8f427b2 by axis at 2009-04-24 95
    }
96
}
97
98
key_t QSharedMemoryPrivate::handle()
99
{
11bacd3 by Ritt Konstantin at 2011-06-06 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
8f427b2 by axis at 2009-04-24 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
8eaf39f by Ritt Konstantin at 2011-06-06 113
void QSharedMemoryPrivate::cleanHandle()
8f427b2 by axis at 2009-04-24 114
{
115
    chunk.Close();
116
}
117
118
bool QSharedMemoryPrivate::create(int size)
119
{
11bacd3 by Ritt Konstantin at 2011-06-06 120
    if (!handle())
8f427b2 by axis at 2009-04-24 121
        return false;
122
97a092b by Mirko Damiani at 2010-05-26 123
    TPtrC ptr(qt_QString2TPtrC(nativeKey));
8f427b2 by axis at 2009-04-24 124
7604f80 by Robert Griebl at 2009-06-10 125
    TInt err = chunk.CreateGlobal(ptr, size, size);
8f427b2 by axis at 2009-04-24 126
11bacd3 by Ritt Konstantin at 2011-06-06 127
    if (err != KErrNone) {
128
        setErrorString(QLatin1String("QSharedMemory::create"), err);
8f427b2 by axis at 2009-04-24 129
        return false;
11bacd3 by Ritt Konstantin at 2011-06-06 130
    }
8f427b2 by axis at 2009-04-24 131
132
    // Zero out the created chunk
133
    Mem::FillZ(chunk.Base(), chunk.Size());
134
135
    return true;
136
}
137
7f36bb0 by axis at 2009-07-09 138
bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode /* mode */)
8f427b2 by axis at 2009-04-24 139
{
140
    // Grab a pointer to the memory block
141
    if (!chunk.Handle()) {
11bacd3 by Ritt Konstantin at 2011-06-06 142
        if (!handle())
8f427b2 by axis at 2009-04-24 143
            return false;
144
97a092b by Mirko Damiani at 2010-05-26 145
        TPtrC ptr(qt_QString2TPtrC(nativeKey));
8f427b2 by axis at 2009-04-24 146
147
        TInt err = KErrNoMemory;
148
7604f80 by Robert Griebl at 2009-06-10 149
        err = chunk.OpenGlobal(ptr, false);
8f427b2 by axis at 2009-04-24 150
151
        if (err != KErrNone) {
11bacd3 by Ritt Konstantin at 2011-06-06 152
            setErrorString(QLatin1String("QSharedMemory::attach"), err);
8f427b2 by axis at 2009-04-24 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
11bacd3 by Ritt Konstantin at 2011-06-06 174
175
#endif //QT_NO_SHAREDMEMORY