Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qv4compilationunitmapper_unix.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <sys/mman.h>
7#include <functional>
8#include <private/qcore_unix_p.h>
9#include <QScopeGuard>
10#include <QDateTime>
11
13
15
16using namespace QV4;
17
18CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString)
19{
20 close();
21
22 int fd = qt_safe_open(QFile::encodeName(cacheFileName).constData(), O_RDONLY);
23 if (fd == -1) {
25 return nullptr;
26 }
27
28 auto cleanup = qScopeGuard([fd]{
30 });
31
33 qint64 bytesRead = qt_safe_read(fd, reinterpret_cast<char *>(&header), sizeof(header));
34
35 if (bytesRead != sizeof(header)) {
36 *errorString = QStringLiteral("File too small for the header fields");
37 return nullptr;
38 }
39
40 if (!ExecutableCompilationUnit::verifyHeader(&header, sourceTimeStamp, errorString))
41 return nullptr;
42
43 // Data structure and qt version matched, so now we can access the rest of the file safely.
44
45 length = static_cast<size_t>(lseek(fd, 0, SEEK_END));
46
47 void *ptr = mmap(nullptr, length, PROT_READ, MAP_SHARED, fd, /*offset*/0);
48 if (ptr == MAP_FAILED) {
50 return nullptr;
51 }
52 dataPtr = ptr;
53
54 return reinterpret_cast<CompiledData::Unit*>(dataPtr);
55}
56
57void CompilationUnitMapper::close()
58{
59 // Do not unmap the data here.
60 if (dataPtr != nullptr) {
61 // Do not unmap cache files that are built with the StaticData flag. That's the majority of
62 // them and it's necessary to benefit from the QString literal optimization. There might
63 // still be QString instances around that point into that memory area. The memory is backed
64 // on the disk, so the kernel is free to release the pages and all that remains is the
65 // address space allocation.
66 if (!(reinterpret_cast<CompiledData::Unit*>(dataPtr)->flags & CompiledData::Unit::StaticData))
67 munmap(dataPtr, length);
68 }
69 dataPtr = nullptr;
70}
71
\inmodule QtCore\reentrant
Definition qdatetime.h:257
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
Definition qfile.h:158
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static bool verifyHeader(const CompiledData::Unit *unit, QDateTime expectedSourceTimeStamp, QString *errorString)
Combined button and popup list for selecting options.
\qmltype Particle \inqmlmodule QtQuick.Particles
Q_MULTIMEDIA_EXPORT QString errorString(HRESULT hr)
static int qt_safe_open(const char *pathname, int flags, mode_t mode=0777)
static qint64 qt_safe_read(int fd, void *data, qint64 maxlen)
static int qt_safe_close(int fd)
static QString header(const QString &name)
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
static ControlElement< T > * ptr(QWidget *widget)
GLenum GLuint GLenum GLsizei length
GLbitfield flags
GLuint64 GLenum GLint fd
#define MAP_FAILED
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define QStringLiteral(str)
long long qint64
Definition qtypes.h:55