Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qv4compilationunitmapper_win.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
7#include <QScopeGuard>
8#include <QFileInfo>
9#include <QDateTime>
10#include <qt_windows.h>
11
13
14using namespace QV4;
15
16CompiledData::Unit *CompilationUnitMapper::open(const QString &cacheFileName, const QDateTime &sourceTimeStamp, QString *errorString)
17{
18 close();
19
20 // ### TODO: fix up file encoding/normalization/unc handling once QFileSystemEntry
21 // is exported from QtCore.
23 CreateFile(reinterpret_cast<const wchar_t*>(cacheFileName.constData()),
24 GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ,
25 nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
26 nullptr);
27 if (handle == INVALID_HANDLE_VALUE) {
28 *errorString = qt_error_string(GetLastError());
29 return nullptr;
30 }
31
32 auto fileHandleCleanup = qScopeGuard([handle]{
33 CloseHandle(handle);
34 });
35
37 DWORD bytesRead;
38 if (!ReadFile(handle, reinterpret_cast<char *>(&header), sizeof(header), &bytesRead, nullptr)) {
39 *errorString = qt_error_string(GetLastError());
40 return nullptr;
41 }
42
43 if (bytesRead != sizeof(header)) {
44 *errorString = QStringLiteral("File too small for the header fields");
45 return nullptr;
46 }
47
48 if (!ExecutableCompilationUnit::verifyHeader(&header, sourceTimeStamp, errorString))
49 return nullptr;
50
51 // Data structure and qt version matched, so now we can access the rest of the file safely.
52
53 HANDLE fileMappingHandle = CreateFileMapping(handle, 0, PAGE_READONLY, 0, 0, 0);
54 if (!fileMappingHandle) {
55 *errorString = qt_error_string(GetLastError());
56 return nullptr;
57 }
58
59 auto mappingCleanup = qScopeGuard([fileMappingHandle]{
60 CloseHandle(fileMappingHandle);
61 });
62
63 dataPtr = MapViewOfFile(fileMappingHandle, FILE_MAP_READ, 0, 0, 0);
64 if (!dataPtr) {
65 *errorString = qt_error_string(GetLastError());
66 return nullptr;
67 }
68
69 return reinterpret_cast<CompiledData::Unit*>(dataPtr);
70}
71
72void CompilationUnitMapper::close()
73{
74 if (dataPtr != nullptr) {
75 // Do not unmap cache files that are built with the StaticData flag. That's the majority of
76 // them and it's necessary to benefit from the QString literal optimization. There might
77 // still be QString instances around that point into that memory area. The memory is backed
78 // on the disk, so the kernel is free to release the pages and all that remains is the
79 // address space allocation.
80 if (!(reinterpret_cast<CompiledData::Unit*>(dataPtr)->flags & CompiledData::Unit::StaticData))
81 UnmapViewOfFile(dataPtr);
82 }
83 dataPtr = nullptr;
84}
85
\inmodule QtCore\reentrant
Definition qdatetime.h:257
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
const QChar * constData() const
Returns a pointer to the data stored in the QString.
Definition qstring.h:1101
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)
void * HANDLE
static QString header(const QString &name)
Q_DECL_COLD_FUNCTION Q_CORE_EXPORT QString qt_error_string(int errorCode=-1)
GLuint64 GLenum void * handle
GLbitfield flags
QScopeGuard< typename std::decay< F >::type > qScopeGuard(F &&f)
[qScopeGuard]
Definition qscopeguard.h:60
#define QStringLiteral(str)