From 6c6b96eb698e1314e7f63a3d6340b3d446116672 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 15 Apr 2020 12:27:42 +0200 Subject: Re-arrange qv4identifier{_p.h|.cpp} QV4::IdentifierHashData doesn't need to be visible to all the clients. Furthermore, it can be completely inline. The public functions of QV4::IdentifierHash need to be out of line, but the private ones can be all inline. This shouldn't make much of a difference as most of the public functions call at least one private function. Finally, the files should be named by the class names. Change-Id: I931f2aa71103e41fdd3dae9cc47d204edece0e12 Reviewed-by: Fabian Kosmale --- .../qmldbg_debugger/qv4datacollector.cpp | 2 +- .../qqmlnativedebugservice.cpp | 2 +- .../qmldbg_nativedebugger/qqmlnativedebugservice.h | 2 +- src/qml/.prev_CMakeLists.txt | 5 +- src/qml/CMakeLists.txt | 3 +- src/qml/jsruntime/jsruntime.pri | 5 +- src/qml/jsruntime/qv4executablecompilationunit_p.h | 2 +- src/qml/jsruntime/qv4identifier.cpp | 207 ------------------- src/qml/jsruntime/qv4identifier_p.h | 173 ---------------- src/qml/jsruntime/qv4identifierhash.cpp | 225 +++++++++++++++++++++ src/qml/jsruntime/qv4identifierhash_p.h | 99 +++++++++ src/qml/jsruntime/qv4identifierhashdata_p.h | 122 +++++++++++ src/qml/jsruntime/qv4identifiertable.cpp | 1 + src/qml/jsruntime/qv4identifiertable_p.h | 2 +- src/qml/jsruntime/qv4internalclass.cpp | 2 +- src/qml/jsruntime/qv4object.cpp | 2 +- src/qml/jsruntime/qv4objectiterator.cpp | 2 +- src/qml/qml/qqmlcontextdata_p.h | 2 +- 18 files changed, 465 insertions(+), 393 deletions(-) delete mode 100644 src/qml/jsruntime/qv4identifier.cpp delete mode 100644 src/qml/jsruntime/qv4identifier_p.h create mode 100644 src/qml/jsruntime/qv4identifierhash.cpp create mode 100644 src/qml/jsruntime/qv4identifierhash_p.h create mode 100644 src/qml/jsruntime/qv4identifierhashdata_p.h diff --git a/src/plugins/qmltooling/qmldbg_debugger/qv4datacollector.cpp b/src/plugins/qmltooling/qmldbg_debugger/qv4datacollector.cpp index 506ecb64bb..d3134e0727 100644 --- a/src/plugins/qmltooling/qmldbg_debugger/qv4datacollector.cpp +++ b/src/plugins/qmltooling/qmldbg_debugger/qv4datacollector.cpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.cpp b/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.cpp index dceaab9f6d..b4eb442aca 100644 --- a/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.cpp +++ b/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.cpp @@ -46,7 +46,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.h b/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.h index 86f2e31d60..00b9749472 100644 --- a/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.h +++ b/src/plugins/qmltooling/qmldbg_nativedebugger/qqmlnativedebugservice.h @@ -47,7 +47,7 @@ #include #include #include -#include +#include #include #include diff --git a/src/qml/.prev_CMakeLists.txt b/src/qml/.prev_CMakeLists.txt index eea934fc64..90cb65a95c 100644 --- a/src/qml/.prev_CMakeLists.txt +++ b/src/qml/.prev_CMakeLists.txt @@ -105,7 +105,8 @@ qt_add_module(Qml jsruntime/qv4generatorobject.cpp jsruntime/qv4generatorobject_p.h jsruntime/qv4global_p.h jsruntime/qv4globalobject.cpp jsruntime/qv4globalobject_p.h - jsruntime/qv4identifier.cpp jsruntime/qv4identifier_p.h + jsruntime/qv4identifierhash.cpp jsruntime/qv4identifierhash_p.h + jsruntime/qv4identifierhashdata_p.h jsruntime/qv4identifiertable.cpp jsruntime/qv4identifiertable_p.h jsruntime/qv4include.cpp jsruntime/qv4include_p.h jsruntime/qv4internalclass.cpp jsruntime/qv4internalclass_p.h @@ -193,6 +194,7 @@ qt_add_module(Qml qml/qqmlcomponent.cpp qml/qqmlcomponent.h qml/qqmlcomponent_p.h qml/qqmlcomponentattached_p.h qml/qqmlcontext.cpp qml/qqmlcontext.h qml/qqmlcontext_p.h + qml/qqmlcontextdata.cpp qml/qqmlcontextdata_p.h qml/qqmlcustomparser.cpp qml/qqmlcustomparser_p.h qml/qqmldata_p.h qml/qqmldatablob.cpp qml/qqmldatablob_p.h @@ -209,6 +211,7 @@ qt_add_module(Qml qml/qqmlfileselector.cpp qml/qqmlfileselector.h qml/qqmlfileselector_p.h qml/qqmlglobal.cpp qml/qqmlglobal_p.h qml/qqmlguard_p.h + qml/qqmlguardedcontextdata_p.h qml/qqmlimport.cpp qml/qqmlimport_p.h qml/qqmlincubator.cpp qml/qqmlincubator.h qml/qqmlincubator_p.h qml/qqmlinfo.cpp qml/qqmlinfo.h diff --git a/src/qml/CMakeLists.txt b/src/qml/CMakeLists.txt index df85a87c69..381a76cb12 100644 --- a/src/qml/CMakeLists.txt +++ b/src/qml/CMakeLists.txt @@ -105,7 +105,8 @@ qt_add_module(Qml jsruntime/qv4generatorobject.cpp jsruntime/qv4generatorobject_p.h jsruntime/qv4global_p.h jsruntime/qv4globalobject.cpp jsruntime/qv4globalobject_p.h - jsruntime/qv4identifier.cpp jsruntime/qv4identifier_p.h + jsruntime/qv4identifierhash.cpp jsruntime/qv4identifierhash_p.h + jsruntime/qv4identifierhashdata_p.h jsruntime/qv4identifiertable.cpp jsruntime/qv4identifiertable_p.h jsruntime/qv4include.cpp jsruntime/qv4include_p.h jsruntime/qv4internalclass.cpp jsruntime/qv4internalclass_p.h diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index adea21969d..00127e1ca8 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -6,7 +6,7 @@ SOURCES += \ $$PWD/qv4context.cpp \ $$PWD/qv4persistent.cpp \ $$PWD/qv4lookup.cpp \ - $$PWD/qv4identifier.cpp \ + $$PWD/qv4identifierhash.cpp \ $$PWD/qv4identifiertable.cpp \ $$PWD/qv4managed.cpp \ $$PWD/qv4internalclass.cpp \ @@ -77,7 +77,8 @@ HEADERS += \ $$PWD/qv4persistent_p.h \ $$PWD/qv4debugging_p.h \ $$PWD/qv4lookup_p.h \ - $$PWD/qv4identifier_p.h \ + $$PWD/qv4identifierhashdata_p.h \ + $$PWD/qv4identifierhash_p.h \ $$PWD/qv4identifiertable_p.h \ $$PWD/qv4managed_p.h \ $$PWD/qv4internalclass_p.h \ diff --git a/src/qml/jsruntime/qv4executablecompilationunit_p.h b/src/qml/jsruntime/qv4executablecompilationunit_p.h index 410316e160..1af5824074 100644 --- a/src/qml/jsruntime/qv4executablecompilationunit_p.h +++ b/src/qml/jsruntime/qv4executablecompilationunit_p.h @@ -52,7 +52,7 @@ // #include -#include +#include #include #include #include diff --git a/src/qml/jsruntime/qv4identifier.cpp b/src/qml/jsruntime/qv4identifier.cpp deleted file mode 100644 index c3d7165f71..0000000000 --- a/src/qml/jsruntime/qv4identifier.cpp +++ /dev/null @@ -1,207 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "qv4identifier_p.h" -#include "qv4identifiertable_p.h" -#include "qv4string_p.h" -#include - -QT_BEGIN_NAMESPACE - -namespace QV4 { - -IdentifierHashData::IdentifierHashData(IdentifierTable *table, int numBits) - : size(0) - , numBits(numBits) - , identifierTable(table) -{ - refCount.storeRelaxed(1); - alloc = qPrimeForNumBits(numBits); - entries = (IdentifierHashEntry *)malloc(alloc*sizeof(IdentifierHashEntry)); - memset(entries, 0, alloc*sizeof(IdentifierHashEntry)); - identifierTable->addIdentifierHash(this); -} - -IdentifierHashData::IdentifierHashData(IdentifierHashData *other) - : size(other->size) - , numBits(other->numBits) - , identifierTable(other->identifierTable) -{ - refCount.storeRelaxed(1); - alloc = other->alloc; - entries = (IdentifierHashEntry *)malloc(alloc*sizeof(IdentifierHashEntry)); - memcpy(entries, other->entries, alloc*sizeof(IdentifierHashEntry)); - identifierTable->addIdentifierHash(this); -} - -IdentifierHashData::~IdentifierHashData() { - free(entries); - if (identifierTable) - identifierTable->removeIdentifierHash(this); -} - -IdentifierHash::IdentifierHash(ExecutionEngine *engine) -{ - d = new IdentifierHashData(engine->identifierTable, 3); -} - -void IdentifierHash::detach() -{ - if (!d || d->refCount.loadAcquire() == 1) - return; - IdentifierHashData *newData = new IdentifierHashData(d); - if (d && !d->refCount.deref()) - delete d; - d = newData; -} - - -IdentifierHashEntry *IdentifierHash::addEntry(PropertyKey identifier) -{ - Q_ASSERT(identifier.isStringOrSymbol()); - - // fill up to max 50% - bool grow = (d->alloc <= d->size*2); - - if (grow) { - ++d->numBits; - int newAlloc = qPrimeForNumBits(d->numBits); - IdentifierHashEntry *newEntries = (IdentifierHashEntry *)malloc(newAlloc * sizeof(IdentifierHashEntry)); - memset(newEntries, 0, newAlloc*sizeof(IdentifierHashEntry)); - for (int i = 0; i < d->alloc; ++i) { - const IdentifierHashEntry &e = d->entries[i]; - if (!e.identifier.isValid()) - continue; - uint idx = e.identifier.id() % newAlloc; - while (newEntries[idx].identifier.isValid()) { - ++idx; - idx %= newAlloc; - } - newEntries[idx] = e; - } - free(d->entries); - d->entries = newEntries; - d->alloc = newAlloc; - } - - uint idx = identifier.id() % d->alloc; - while (d->entries[idx].identifier.isValid()) { - Q_ASSERT(d->entries[idx].identifier != identifier); - ++idx; - idx %= d->alloc; - } - d->entries[idx].identifier = identifier; - ++d->size; - return d->entries + idx; -} - -const IdentifierHashEntry *IdentifierHash::lookup(PropertyKey identifier) const -{ - if (!d || !identifier.isStringOrSymbol()) - return nullptr; - Q_ASSERT(d->entries); - - uint idx = identifier.id() % d->alloc; - while (1) { - if (!d->entries[idx].identifier.isValid()) - return nullptr; - if (d->entries[idx].identifier == identifier) - return d->entries + idx; - ++idx; - idx %= d->alloc; - } -} - -const IdentifierHashEntry *IdentifierHash::lookup(const QString &str) const -{ - if (!d) - return nullptr; - - PropertyKey id = d->identifierTable->asPropertyKey(str); - return lookup(id); -} - -const IdentifierHashEntry *IdentifierHash::lookup(String *str) const -{ - if (!d) - return nullptr; - PropertyKey id = d->identifierTable->asPropertyKey(str); - if (id.isValid()) - return lookup(id); - return lookup(str->toQString()); -} - -const PropertyKey IdentifierHash::toIdentifier(const QString &str) const -{ - Q_ASSERT(d); - return d->identifierTable->asPropertyKey(str); -} - -const PropertyKey IdentifierHash::toIdentifier(Heap::String *str) const -{ - Q_ASSERT(d); - return d->identifierTable->asPropertyKey(str); -} - -QString QV4::IdentifierHash::findId(int value) const -{ - IdentifierHashEntry *e = d->entries; - IdentifierHashEntry *end = e + d->alloc; - while (e < end) { - if (e->identifier.isValid() && e->value == value) - return e->identifier.toQString(); - ++e; - } - return QString(); -} - -void IdentifierHashData::markObjects(MarkStack *markStack) const -{ - IdentifierHashEntry *e = entries; - IdentifierHashEntry *end = e + alloc; - while (e < end) { - if (Heap::Base *o = e->identifier.asStringOrSymbol()) - o->mark(markStack); - ++e; - } -} - - -} - -QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4identifier_p.h b/src/qml/jsruntime/qv4identifier_p.h deleted file mode 100644 index 32de8b7c8d..0000000000 --- a/src/qml/jsruntime/qv4identifier_p.h +++ /dev/null @@ -1,173 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef QV4IDENTIFIER_H -#define QV4IDENTIFIER_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -namespace QV4 { - -struct IdentifierHashEntry { - PropertyKey identifier; - int value; -}; - -struct IdentifierHashData -{ - IdentifierHashData(IdentifierTable *table, int numBits); - explicit IdentifierHashData(IdentifierHashData *other); - ~IdentifierHashData(); - void markObjects(MarkStack *markStack) const; - - QBasicAtomicInt refCount; - int alloc; - int size; - int numBits; - IdentifierTable *identifierTable; - IdentifierHashEntry *entries; -}; - -struct IdentifierHash -{ - - IdentifierHashData *d = nullptr; - - IdentifierHash() {} - IdentifierHash(ExecutionEngine *engine); - inline IdentifierHash(const IdentifierHash &other); - inline ~IdentifierHash(); - inline IdentifierHash &operator=(const IdentifierHash &other); - - bool isEmpty() const { return !d; } - - inline int count() const; - - void detach(); - - void add(const QString &str, int value); - void add(Heap::String *str, int value); - - inline int value(const QString &str) const; - inline int value(String *str) const; - QString findId(int value) const; - -protected: - IdentifierHashEntry *addEntry(PropertyKey i); - const IdentifierHashEntry *lookup(PropertyKey identifier) const; - const IdentifierHashEntry *lookup(const QString &str) const; - const IdentifierHashEntry *lookup(String *str) const; - const PropertyKey toIdentifier(const QString &str) const; - const PropertyKey toIdentifier(Heap::String *str) const; -}; - - -inline IdentifierHash::IdentifierHash(const IdentifierHash &other) -{ - d = other.d; - if (d) - d->refCount.ref(); -} - -inline IdentifierHash::~IdentifierHash() -{ - if (d && !d->refCount.deref()) - delete d; -} - -IdentifierHash &IdentifierHash::operator=(const IdentifierHash &other) -{ - if (other.d) - other.d->refCount.ref(); - if (d && !d->refCount.deref()) - delete d; - d = other.d; - return *this; -} - -inline int IdentifierHash::count() const -{ - return d ? d->size : 0; -} - -inline -void IdentifierHash::add(const QString &str, int value) -{ - IdentifierHashEntry *e = addEntry(toIdentifier(str)); - e->value = value; -} - -inline -void IdentifierHash::add(Heap::String *str, int value) -{ - IdentifierHashEntry *e = addEntry(toIdentifier(str)); - e->value = value; -} - -inline int IdentifierHash::value(const QString &str) const -{ - const IdentifierHashEntry *e = lookup(str); - return e ? e->value : -1; -} - -inline int IdentifierHash::value(String *str) const -{ - const IdentifierHashEntry *e = lookup(str); - return e ? e->value : -1; -} - -} - -QT_END_NAMESPACE - -#endif diff --git a/src/qml/jsruntime/qv4identifierhash.cpp b/src/qml/jsruntime/qv4identifierhash.cpp new file mode 100644 index 0000000000..7d2d3143b2 --- /dev/null +++ b/src/qml/jsruntime/qv4identifierhash.cpp @@ -0,0 +1,225 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +IdentifierHash::IdentifierHash(ExecutionEngine *engine) +{ + d = new IdentifierHashData(engine->identifierTable, 3); +} + +void IdentifierHash::detach() +{ + if (!d || d->refCount.loadAcquire() == 1) + return; + IdentifierHashData *newData = new IdentifierHashData(d); + if (d && !d->refCount.deref()) + delete d; + d = newData; +} + +inline +IdentifierHashEntry *IdentifierHash::addEntry(PropertyKey identifier) +{ + Q_ASSERT(identifier.isStringOrSymbol()); + + // fill up to max 50% + bool grow = (d->alloc <= d->size*2); + + if (grow) { + ++d->numBits; + int newAlloc = qPrimeForNumBits(d->numBits); + IdentifierHashEntry *newEntries = (IdentifierHashEntry *)malloc(newAlloc * sizeof(IdentifierHashEntry)); + memset(newEntries, 0, newAlloc*sizeof(IdentifierHashEntry)); + for (int i = 0; i < d->alloc; ++i) { + const IdentifierHashEntry &e = d->entries[i]; + if (!e.identifier.isValid()) + continue; + uint idx = e.identifier.id() % newAlloc; + while (newEntries[idx].identifier.isValid()) { + ++idx; + idx %= newAlloc; + } + newEntries[idx] = e; + } + free(d->entries); + d->entries = newEntries; + d->alloc = newAlloc; + } + + uint idx = identifier.id() % d->alloc; + while (d->entries[idx].identifier.isValid()) { + Q_ASSERT(d->entries[idx].identifier != identifier); + ++idx; + idx %= d->alloc; + } + d->entries[idx].identifier = identifier; + ++d->size; + return d->entries + idx; +} + +inline +const IdentifierHashEntry *IdentifierHash::lookup(PropertyKey identifier) const +{ + if (!d || !identifier.isStringOrSymbol()) + return nullptr; + Q_ASSERT(d->entries); + + uint idx = identifier.id() % d->alloc; + while (1) { + if (!d->entries[idx].identifier.isValid()) + return nullptr; + if (d->entries[idx].identifier == identifier) + return d->entries + idx; + ++idx; + idx %= d->alloc; + } +} + +inline +const IdentifierHashEntry *IdentifierHash::lookup(const QString &str) const +{ + if (!d) + return nullptr; + + PropertyKey id = d->identifierTable->asPropertyKey(str); + return lookup(id); +} + +inline +const IdentifierHashEntry *IdentifierHash::lookup(String *str) const +{ + if (!d) + return nullptr; + PropertyKey id = d->identifierTable->asPropertyKey(str); + if (id.isValid()) + return lookup(id); + return lookup(str->toQString()); +} + +inline +const PropertyKey IdentifierHash::toIdentifier(const QString &str) const +{ + Q_ASSERT(d); + return d->identifierTable->asPropertyKey(str); +} + +inline +const PropertyKey IdentifierHash::toIdentifier(Heap::String *str) const +{ + Q_ASSERT(d); + return d->identifierTable->asPropertyKey(str); +} + +QString QV4::IdentifierHash::findId(int value) const +{ + IdentifierHashEntry *e = d->entries; + IdentifierHashEntry *end = e + d->alloc; + while (e < end) { + if (e->identifier.isValid() && e->value == value) + return e->identifier.toQString(); + ++e; + } + return QString(); +} + +QV4::IdentifierHash::IdentifierHash(const IdentifierHash &other) +{ + d = other.d; + if (d) + d->refCount.ref(); +} + +QV4::IdentifierHash::~IdentifierHash() +{ + if (d && !d->refCount.deref()) + delete d; +} + +IdentifierHash &QV4::IdentifierHash::operator=(const IdentifierHash &other) +{ + if (other.d) + other.d->refCount.ref(); + if (d && !d->refCount.deref()) + delete d; + d = other.d; + return *this; +} + +int QV4::IdentifierHash::count() const +{ + return d ? d->size : 0; +} + +void QV4::IdentifierHash::add(const QString &str, int value) +{ + IdentifierHashEntry *e = addEntry(toIdentifier(str)); + e->value = value; +} + +void QV4::IdentifierHash::add(Heap::String *str, int value) +{ + IdentifierHashEntry *e = addEntry(toIdentifier(str)); + e->value = value; +} + +int QV4::IdentifierHash::value(const QString &str) const +{ + const IdentifierHashEntry *e = lookup(str); + return e ? e->value : -1; +} + +int QV4::IdentifierHash::value(String *str) const +{ + const IdentifierHashEntry *e = lookup(str); + return e ? e->value : -1; +} + + +} + +QT_END_NAMESPACE diff --git a/src/qml/jsruntime/qv4identifierhash_p.h b/src/qml/jsruntime/qv4identifierhash_p.h new file mode 100644 index 0000000000..07cb4b6ce3 --- /dev/null +++ b/src/qml/jsruntime/qv4identifierhash_p.h @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4IDENTIFIERHASH_P_H +#define QV4IDENTIFIERHASH_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct IdentifierHashEntry; +struct IdentifierHashData; +struct IdentifierHash +{ + IdentifierHash() = default; + IdentifierHash(ExecutionEngine *engine); + IdentifierHash(const IdentifierHash &other); + ~IdentifierHash(); + IdentifierHash &operator=(const IdentifierHash &other); + + bool isEmpty() const { return !d; } + + int count() const; + + void detach(); + + void add(const QString &str, int value); + void add(Heap::String *str, int value); + + int value(const QString &str) const; + int value(String *str) const; + QString findId(int value) const; + +private: + inline IdentifierHashEntry *addEntry(PropertyKey i); + inline const IdentifierHashEntry *lookup(PropertyKey identifier) const; + inline const IdentifierHashEntry *lookup(const QString &str) const; + inline const IdentifierHashEntry *lookup(String *str) const; + inline const PropertyKey toIdentifier(const QString &str) const; + inline const PropertyKey toIdentifier(Heap::String *str) const; + + IdentifierHashData *d = nullptr; +}; + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif // QV4_IDENTIFIERHASH_P_H diff --git a/src/qml/jsruntime/qv4identifierhashdata_p.h b/src/qml/jsruntime/qv4identifierhashdata_p.h new file mode 100644 index 0000000000..a4ea40c524 --- /dev/null +++ b/src/qml/jsruntime/qv4identifierhashdata_p.h @@ -0,0 +1,122 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtQml module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef QV4IDENTIFIERHASHDATA_H +#define QV4IDENTIFIERHASHDATA_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +struct IdentifierHashEntry { + PropertyKey identifier; + int value; +}; + +struct IdentifierHashData +{ + IdentifierHashData(IdentifierTable *table, int numBits) + : size(0) + , numBits(numBits) + , identifierTable(table) + { + refCount.storeRelaxed(1); + alloc = qPrimeForNumBits(numBits); + entries = (IdentifierHashEntry *)malloc(alloc*sizeof(IdentifierHashEntry)); + memset(entries, 0, alloc*sizeof(IdentifierHashEntry)); + identifierTable->addIdentifierHash(this); + } + + explicit IdentifierHashData(IdentifierHashData *other) + : size(other->size) + , numBits(other->numBits) + , identifierTable(other->identifierTable) + { + refCount.storeRelaxed(1); + alloc = other->alloc; + entries = (IdentifierHashEntry *)malloc(alloc*sizeof(IdentifierHashEntry)); + memcpy(entries, other->entries, alloc*sizeof(IdentifierHashEntry)); + identifierTable->addIdentifierHash(this); + } + + ~IdentifierHashData() { + free(entries); + if (identifierTable) + identifierTable->removeIdentifierHash(this); + } + + void markObjects(MarkStack *markStack) const + { + IdentifierHashEntry *e = entries; + IdentifierHashEntry *end = e + alloc; + while (e < end) { + if (Heap::Base *o = e->identifier.asStringOrSymbol()) + o->mark(markStack); + ++e; + } + } + + QBasicAtomicInt refCount; + int alloc; + int size; + int numBits; + IdentifierTable *identifierTable; + IdentifierHashEntry *entries; +}; + +} // namespace QV4 + +QT_END_NAMESPACE + +#endif // QV4IDENTIFIERHASHDATA_P_H diff --git a/src/qml/jsruntime/qv4identifiertable.cpp b/src/qml/jsruntime/qv4identifiertable.cpp index 21b47c3909..c4ce837360 100644 --- a/src/qml/jsruntime/qv4identifiertable.cpp +++ b/src/qml/jsruntime/qv4identifiertable.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include "qv4identifiertable_p.h" #include "qv4symbol_p.h" +#include #include QT_BEGIN_NAMESPACE diff --git a/src/qml/jsruntime/qv4identifiertable_p.h b/src/qml/jsruntime/qv4identifiertable_p.h index 78e2b6620e..6553ad6103 100644 --- a/src/qml/jsruntime/qv4identifiertable_p.h +++ b/src/qml/jsruntime/qv4identifiertable_p.h @@ -50,7 +50,7 @@ // We mean it. // -#include "qv4identifier_p.h" +#include "qv4identifierhash_p.h" #include "qv4string_p.h" #include "qv4engine_p.h" #include diff --git a/src/qml/jsruntime/qv4internalclass.cpp b/src/qml/jsruntime/qv4internalclass.cpp index 70849775cb..4287563c94 100644 --- a/src/qml/jsruntime/qv4internalclass.cpp +++ b/src/qml/jsruntime/qv4internalclass.cpp @@ -40,7 +40,7 @@ #include #include #include -#include +#include #include "qv4object_p.h" #include "qv4identifiertable_p.h" #include "qv4value_p.h" diff --git a/src/qml/jsruntime/qv4object.cpp b/src/qml/jsruntime/qv4object.cpp index b723141caa..11e2efb64c 100644 --- a/src/qml/jsruntime/qv4object.cpp +++ b/src/qml/jsruntime/qv4object.cpp @@ -46,7 +46,7 @@ #include "qv4scopedvalue_p.h" #include "qv4memberdata_p.h" #include "qv4objectiterator_p.h" -#include "qv4identifier_p.h" +#include "qv4identifierhash_p.h" #include "qv4string_p.h" #include "qv4identifiertable_p.h" #include "qv4jscall_p.h" diff --git a/src/qml/jsruntime/qv4objectiterator.cpp b/src/qml/jsruntime/qv4objectiterator.cpp index 65f6fa8b12..1711a4228c 100644 --- a/src/qml/jsruntime/qv4objectiterator.cpp +++ b/src/qml/jsruntime/qv4objectiterator.cpp @@ -39,7 +39,7 @@ #include "qv4objectiterator_p.h" #include "qv4object_p.h" #include "qv4stringobject_p.h" -#include "qv4identifier_p.h" +#include "qv4identifierhash_p.h" #include "qv4argumentsobject_p.h" #include "qv4string_p.h" #include "qv4iterator_p.h" diff --git a/src/qml/qml/qqmlcontextdata_p.h b/src/qml/qml/qqmlcontextdata_p.h index ccefd3a05b..ea0b010d21 100644 --- a/src/qml/qml/qqmlcontextdata_p.h +++ b/src/qml/qml/qqmlcontextdata_p.h @@ -55,7 +55,7 @@ #include #include #include -#include +#include #include QT_BEGIN_NAMESPACE -- cgit v1.2.3