1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2008 Christian Ehrlicher <ch.ehrlicher@gmx.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KMEMFILE_H
9#define KMEMFILE_H
10
11#include <QIODevice>
12#include <kservice_export.h>
13#include <memory>
14
15#ifndef QT_NO_SHAREDMEMORY
16
17/**
18 * @internal
19 * Simple QIODevice for QSharedMemory to keep ksycoca cache in memory only once
20 * The first call to open() loads the file into a shm segment. Every
21 * subsequent call only attaches to this segment. When the file content changed,
22 * you have to execute KMemFile::fileContentsChanged() to update the internal
23 * structures. The next call to open() creates a new shm segment. The old one
24 * is automatically destroyed when the last process closed KMemFile.
25 */
26
27class KMemFile : public QIODevice
28{
29 Q_OBJECT
30public:
31 /**
32 * ctor
33 *
34 * @param filename the file to load into memory
35 * @param parent our parent
36 */
37 explicit KMemFile(const QString &filename, QObject *parent = nullptr);
38 /**
39 * dtor
40 */
41 ~KMemFile() override;
42 /**
43 * closes the KMemFile
44 *
45 * @reimp
46 */
47 void close() override;
48 /**
49 * As KMemFile is a random access device, it returns false
50 *
51 * @reimp
52 */
53 bool isSequential() const override;
54 /**
55 * @reimp
56 * @param mode only QIODevice::ReadOnly is accepted
57 */
58 bool open(OpenMode mode) override;
59 /**
60 * Sets the current read/write position to pos
61 * @reimp
62 * @param pos the new read/write position
63 */
64 bool seek(qint64 pos) override;
65 /**
66 * Returns the size of the file
67 * @reimp
68 */
69 qint64 size() const override;
70 /**
71 * This static function updates the internal information about the file
72 * loaded into shared memory. The next time the file is opened, the file is
73 * reread from the file system.
74 */
75 static void fileContentsChanged(const QString &filename);
76
77protected:
78 /** @reimp */
79 qint64 readData(char *data, qint64 maxSize) override;
80 /** @reimp */
81 qint64 writeData(const char *data, qint64 maxSize) override;
82
83private:
84 class Private;
85 friend class Private;
86 std::unique_ptr<Private> const d;
87};
88
89#endif // QT_NO_SHAREDMEMORY
90
91#endif // KMEMFILE_H
92

source code of kservice/src/sycoca/kmemfile_p.h