aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/style/muniquestringcache.cpp
blob: b70d2f7741c87ee528c57d3b2669c0c7ad6a96cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "muniquestringcache.h"

#include "mdebug.h"

#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QSystemSemaphore>
#include <QHash>
#include <QByteArray>
#include <QElapsedTimer>

const int MAX_CACHE_SIZE = 1024*1024;
#define CACHE_NAME "MTF_UNIQUE_STRING_CACHE"

class UniqueStringCacheMappedMemory
{
public:
    UniqueStringCacheMappedMemory()
        : cacheFile(createCacheFileName()),
        accessSemaphore(QLatin1String(CACHE_NAME "_SEMAPHORE"), 1),
        attached(false)
    {
        if (!accessSemaphore.acquire()) {
            qWarning() << "Unable to aquire semaphore:" << accessSemaphore.errorString();
            return;
        }

        attachToCache();

        accessSemaphore.release();
    }

    bool isAttached() const { return attached; }

    uchar* data() { return rawMappedMemory; }
    QString errorString() { return accessSemaphore.errorString(); }

    ~UniqueStringCacheMappedMemory()
    {
    }

private:
    friend class UniqueStringCacheMappedMemoryLocker;
    bool lock() { return accessSemaphore.acquire(); }
    bool unlock() { return accessSemaphore.release(); }

    void attachToCache()
    {
        if (!cacheFile.exists()) {
            if (!initializeCache()) {
                attached = false;
                return;
            }
        }

        if (!cacheFile.open(QIODevice::ReadWrite)) {
            mWarning("UniqueStringCacheMappedMemory") << "Could not open" <<
                cacheFile.fileName();
            return;
        }

        if (cacheFile.size() != MAX_CACHE_SIZE) {
            mWarning("UniqueStringCacheMappedMemory") << "Wrong cache file size" <<
                cacheFile.size() << "Expected:" << MAX_CACHE_SIZE;
            return;
        }

        rawMappedMemory = cacheFile.map(0, MAX_CACHE_SIZE);

        attached = true;

        cacheFile.close();
    }

    bool initializeCache() {
        if (!cacheFile.open(QFile::WriteOnly)) {
            //Maybe it failed because the directory doesn't exist
            QDir().mkpath(QFileInfo(cacheFile.fileName()).absolutePath());
            if (!cacheFile.open(QFile::WriteOnly)) {
                mWarning("UniqueStringCacheMappedMemory") <<
                         "Wrong permissions for cache directory. Cannot create" <<
                          cacheFile.fileName();
                return false;
            }
        }
        cacheFile.resize(MAX_CACHE_SIZE);
        cacheFile.close();
        return true;
    }

    QString createCacheFileName() {
        return QString(CACHEDIR) + "/css/" + CACHE_NAME;
    }

    QFile cacheFile;
    QSystemSemaphore accessSemaphore;
    uchar* rawMappedMemory;
    bool attached;
};

Q_GLOBAL_STATIC(UniqueStringCacheMappedMemory, uniqueStringCacheMappedMemory)


class UniqueStringCacheMappedMemoryLocker
{
public:
    UniqueStringCacheMappedMemoryLocker(UniqueStringCacheMappedMemory *cache)
        : cache(cache->lock() ? cache : (UniqueStringCacheMappedMemory *)0)
    {
        if (!cache) {
            mWarning("UniqueStringCacheMappedMemoryLocker") << "Unable to lock unique string cache" << cache->errorString();
        }
    }

    bool isLocked() const
    {
        return cache;
    }

    ~UniqueStringCacheMappedMemoryLocker()
    {
        if (!cache) {
            return;
        }
        if (!cache->unlock()) {
            mWarning("UniqueStringCacheMappedMemoryLocker") << "Unable to unlock unique string cache" << cache->errorString();
        }
    }

private:
    UniqueStringCacheMappedMemory *cache;
};

// mapping id => string
typedef QVector<QByteArray> IdToStringCache;
Q_GLOBAL_STATIC(IdToStringCache, idToStringCache)
// mapping string => id
typedef QHash<QByteArray, int> StringToIdCache;
Q_GLOBAL_STATIC(StringToIdCache, stringToIdCache)
static int offset = sizeof(int);

int insert_string_to_cache(const QByteArray &string);

// fills our datastructures from memory
// this method can be called several times to update the internal representation
// if the cache has been changed by another application
void fill_unique_string_cache() {
    uchar *rawCache = uniqueStringCacheMappedMemory()->data();
    int elementsInCache = *reinterpret_cast<int*>(rawCache);
    int oldSize = idToStringCache()->count();
    idToStringCache()->resize(elementsInCache);

    for (int i = oldSize; i < elementsInCache; ++i) {
        uchar *stringAdress = rawCache + offset;
        int stringLength = *reinterpret_cast<int*>(stringAdress);
        // TODO: figure out if we can delay creating the actual byte arrays. as long as stringForId()
        // is not called we do not need to actually create them.
        // first: check if this makes a difference at all
        QByteArray string = QByteArray::fromRawData(reinterpret_cast<const char*>(stringAdress + sizeof(int)), stringLength);
        offset += sizeof(int) + stringLength;

        (*idToStringCache())[i] = string;
        stringToIdCache()->insert(string, i);
    }
    if (elementsInCache == 0) {
        // we want the empty string as first element
        insert_string_to_cache(QByteArray());
    }

    qWarning() << "elements in cache:" << elementsInCache << ", " << (float)offset/MAX_CACHE_SIZE*100 << "% filled cache";
}

// insert one string into the cache, makes sure to handle it gracefully when the cache has
// changed before it has been locked
int insert_string_to_cache(const QByteArray &string) {
    uchar *rawCache = uniqueStringCacheMappedMemory()->data();
    int elementsInCache = *reinterpret_cast<int*>(rawCache);
    if (elementsInCache != idToStringCache()->count()) {
        // cache has changed, check if the string has been added
        // in the meanwhile
        fill_unique_string_cache();
        StringToIdCache::const_iterator it = stringToIdCache()->constFind(string);
        if (it != stringToIdCache()->constEnd()) {
            return *it;
        }
    }

    ++*reinterpret_cast<int*>(rawCache);

    uchar *stringAdress = rawCache + offset;
    *reinterpret_cast<int*>(stringAdress) = string.length();
    memcpy(stringAdress + sizeof(int), string.constData(), string.length());

    offset += sizeof(int) + string.length();
    if (offset >= MAX_CACHE_SIZE) {
        qFatal("unique string cache is full");
    }

    idToStringCache()->append(string);
    stringToIdCache()->insert(string, idToStringCache()->count() - 1);

    return idToStringCache()->count() - 1;
}

// triggers initialization of our data structures from the cache if this
// did not happen yet
void initially_fill_cache() {
    static bool cacheFilled = false;
    if (!cacheFilled) {
        QElapsedTimer timer;
        timer.start();
        UniqueStringCacheMappedMemoryLocker locker(uniqueStringCacheMappedMemory());
        if (!locker.isLocked()) {
            return;
        }
        fill_unique_string_cache();

        cacheFilled = true;
    }
}

bool MUniqueStringCache::isAttached()
{
    UniqueStringCacheMappedMemoryLocker locker(uniqueStringCacheMappedMemory());
    if (!locker.isLocked()) {
        return false;
    }
    return uniqueStringCacheMappedMemory()->isAttached();
}

MUniqueStringCache::Index MUniqueStringCache::stringToIndex(const QByteArray& string)
{
    initially_fill_cache();
    StringToIdCache::const_iterator it = stringToIdCache()->constFind(string);
    if (it != stringToIdCache()->constEnd()) {
        return *it;
    }

    // string is unknown, we need to add it to the cache
    UniqueStringCacheMappedMemoryLocker locker(uniqueStringCacheMappedMemory());
    if (!locker.isLocked()) {
        return UndefinedIndex;
    }

    return insert_string_to_cache(string);
}

QByteArray MUniqueStringCache::indexToString(Index id)
{
    initially_fill_cache();
    if (id < 0) {
        return QByteArray();
    } else if (id >= idToStringCache()->count())  {
        mWarning("MUniqueStringCache::indexToString") << "Id" << id << "is unknown. Has the string cache been deleted?";
        return QByteArray();
    }

    return idToStringCache()->at(id);
}