Move the IdentifierTable into it's own file
authorLars Knoll <lars.knoll@digia.com>
Thu, 27 Jun 2013 21:02:45 +0000 (23:02 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 28 Jun 2013 12:56:29 +0000 (14:56 +0200)
This reduces compile time dependencies.

Change-Id: I7e68ec36022fb0b1dcc10be0cc345aa207ea110c
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
src/qml/qml/v4/qv4engine.cpp
src/qml/qml/v4/qv4identifier.cpp
src/qml/qml/v4/qv4identifier_p.h
src/qml/qml/v4/qv4identifiertable.cpp [new file with mode: 0644]
src/qml/qml/v4/qv4identifiertable_p.h [new file with mode: 0644]
src/qml/qml/v4/qv4internalclass.cpp
src/qml/qml/v4/qv4string.cpp
src/qml/qml/v4/v4.pri

index 035d707..721e323 100644 (file)
@@ -58,7 +58,7 @@
 #include <qv4dateobject_p.h>
 #include <qv4jsonobject_p.h>
 #include <qv4stringobject_p.h>
-#include <qv4identifier_p.h>
+#include <qv4identifiertable_p.h>
 #include <qv4unwindhelper_p.h>
 #include "qv4debugging_p.h"
 #include "qv4executableallocator_p.h"
index 52afb26..1a078bb 100644 (file)
@@ -39,6 +39,7 @@
 **
 ****************************************************************************/
 #include "qv4identifier_p.h"
+#include "qv4identifiertable_p.h"
 
 QT_BEGIN_NAMESPACE
 
@@ -140,131 +141,18 @@ const IdentifierHashEntry *IdentifierHashBase::lookup(String *str) const
     return lookup(d->identifierTable->identifier(str));
 }
 
-
-
-IdentifierTable::IdentifierTable(ExecutionEngine *engine)
-    : engine(engine)
-    , size(0)
-    , numBits(8)
+const Identifier *IdentifierHashBase::toIdentifier(const QString &str) const
 {
-    alloc = primeForNumBits(numBits);
-    entries = (String **)malloc(alloc*sizeof(String *));
-    memset(entries, 0, alloc*sizeof(String *));
+    Q_ASSERT(d);
+    return d->identifierTable->identifier(str);
 }
 
-IdentifierTable::~IdentifierTable()
+const Identifier *IdentifierHashBase::toIdentifier(String *str) const
 {
-    free(entries);
+    Q_ASSERT(d);
+    return d->identifierTable->identifier(str);
 }
 
-void IdentifierTable::addEntry(String *str)
-{
-    uint hash = str->hashValue();
-
-    if (str->subtype >= String::StringType_UInt)
-        return;
-
-    str->identifier = new Identifier;
-    str->identifier->string = str->toQString();
-    str->identifier->hashValue = hash;
-
-    bool grow = (alloc <= size*2);
-
-    if (grow) {
-        ++numBits;
-        int newAlloc = primeForNumBits(numBits);
-        String **newEntries = (String **)malloc(newAlloc*sizeof(String *));
-        memset(newEntries, 0, newAlloc*sizeof(String *));
-        for (uint i = 0; i < alloc; ++i) {
-            String *e = entries[i];
-            if (!e)
-                continue;
-            uint idx = e->stringHash % newAlloc;
-            while (newEntries[idx]) {
-                ++idx;
-                idx %= newAlloc;
-            }
-            newEntries[idx] = e;
-        }
-        free(entries);
-        entries = newEntries;
-        alloc = newAlloc;
-    }
-
-    uint idx = hash % alloc;
-    while (entries[idx]) {
-        ++idx;
-        idx %= alloc;
-    }
-    entries[idx] = str;
-    ++size;
-}
-
-
-
-String *IdentifierTable::insertString(const QString &s)
-{
-    uint hash = String::createHashValue(s.constData(), s.length());
-    uint idx = hash % alloc;
-    while (String *e = entries[idx]) {
-        if (e->stringHash == hash && e->toQString() == s)
-            return e;
-        ++idx;
-        idx %= alloc;
-    }
-
-    String *str = engine->newString(s);
-    addEntry(str);
-    return str;
-}
-
-
-Identifier *IdentifierTable::identifier(String *str)
-{
-    if (str->identifier)
-        return str->identifier;
-    uint hash = str->hashValue();
-    if (str->subtype >= String::StringType_UInt)
-        return 0;
-
-    uint idx = hash % alloc;
-    while (String *e = entries[idx]) {
-        if (e->stringHash == hash && e->isEqualTo(str)) {
-            str->identifier = e->identifier;
-            return e->identifier;
-        }
-        ++idx;
-        idx %= alloc;
-    }
-
-    addEntry(str);
-    return str->identifier;
-}
-
-Identifier *IdentifierTable::identifier(const QString &s)
-{
-    return insertString(s)->identifier;
-}
-
-Identifier *IdentifierTable::identifier(const char *s, int len)
-{
-    uint hash = String::createHashValue(s, len);
-    if (hash == UINT_MAX)
-        return identifier(QString::fromUtf8(s, len));
-
-    QLatin1String latin(s, len);
-    uint idx = hash % alloc;
-    while (String *e = entries[idx]) {
-        if (e->stringHash == hash && e->toQString() == latin)
-            return e->identifier;
-        ++idx;
-        idx %= alloc;
-    }
-
-    String *str = engine->newString(QString::fromLatin1(s, len));
-    addEntry(str);
-    return str->identifier;
-}
 
 }
 
index 8149d87..35f4d17 100644 (file)
 #ifndef QV4IDENTIFIER_H
 #define QV4IDENTIFIER_H
 
-#include "qv4string_p.h"
-#include "qv4engine_p.h"
-#include <limits.h>
+#include <qstring.h>
 
 QT_BEGIN_NAMESPACE
 
 namespace QV4 {
 
+struct String;
 struct IdentifierTable;
+struct ExecutionEngine;
 
 struct Identifier
 {
@@ -66,37 +66,6 @@ struct Identifier
     }
 };
 
-struct IdentifierTable
-{
-    ExecutionEngine *engine;
-
-    int alloc;
-    int size;
-    int numBits;
-    String **entries;
-
-    void addEntry(String *str);
-
-public:
-
-    IdentifierTable(ExecutionEngine *engine);
-    ~IdentifierTable();
-
-    String *insertString(const QString &s);
-
-    Identifier *identifier(String *str);
-    Identifier *identifier(const QString &s);
-    Identifier *identifier(const char *s, int len);
-
-    void mark() {
-        for (int i = 0; i < alloc; ++i)
-            if (entries[i])
-                entries[i]->mark();
-    }
-};
-
-
-
 
 struct IdentifierHashEntry {
     const Identifier *identifier;
@@ -149,6 +118,8 @@ protected:
     const IdentifierHashEntry *lookup(const Identifier *identifier) const;
     const IdentifierHashEntry *lookup(const QString &str) const;
     const IdentifierHashEntry *lookup(String *str) const;
+    const Identifier *toIdentifier(const QString &str) const;
+    const Identifier *toIdentifier(String *str) const;
 };
 
 
@@ -220,8 +191,7 @@ inline bool IdentifierHashBase::contains(String *str) const
 template<typename T>
 void IdentifierHash<T>::add(const QString &str, const T &value)
 {
-    Identifier *i = d->identifierTable->identifier(str);
-    IdentifierHashEntry *e = addEntry(i);
+    IdentifierHashEntry *e = addEntry(toIdentifier(str));
     e->value = value;
 }
 
diff --git a/src/qml/qml/v4/qv4identifiertable.cpp b/src/qml/qml/v4/qv4identifiertable.cpp
new file mode 100644 (file)
index 0000000..d3e0f4c
--- /dev/null
@@ -0,0 +1,184 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include "qv4identifiertable_p.h"
+
+QT_BEGIN_NAMESPACE
+
+namespace QV4 {
+
+static const uchar prime_deltas[] = {
+    0,  0,  1,  3,  1,  5,  3,  3,  1,  9,  7,  5,  3,  9, 25,  3,
+    1, 21,  3, 21,  7, 15,  9,  5,  3, 29, 15,  0,  0,  0,  0,  0
+};
+
+static inline int primeForNumBits(int numBits)
+{
+    return (1 << numBits) + prime_deltas[numBits];
+}
+
+
+IdentifierTable::IdentifierTable(ExecutionEngine *engine)
+    : engine(engine)
+    , size(0)
+    , numBits(8)
+{
+    alloc = primeForNumBits(numBits);
+    entries = (String **)malloc(alloc*sizeof(String *));
+    memset(entries, 0, alloc*sizeof(String *));
+}
+
+IdentifierTable::~IdentifierTable()
+{
+    free(entries);
+}
+
+void IdentifierTable::addEntry(String *str)
+{
+    uint hash = str->hashValue();
+
+    if (str->subtype >= String::StringType_UInt)
+        return;
+
+    str->identifier = new Identifier;
+    str->identifier->string = str->toQString();
+    str->identifier->hashValue = hash;
+
+    bool grow = (alloc <= size*2);
+
+    if (grow) {
+        ++numBits;
+        int newAlloc = primeForNumBits(numBits);
+        String **newEntries = (String **)malloc(newAlloc*sizeof(String *));
+        memset(newEntries, 0, newAlloc*sizeof(String *));
+        for (uint i = 0; i < alloc; ++i) {
+            String *e = entries[i];
+            if (!e)
+                continue;
+            uint idx = e->stringHash % newAlloc;
+            while (newEntries[idx]) {
+                ++idx;
+                idx %= newAlloc;
+            }
+            newEntries[idx] = e;
+        }
+        free(entries);
+        entries = newEntries;
+        alloc = newAlloc;
+    }
+
+    uint idx = hash % alloc;
+    while (entries[idx]) {
+        ++idx;
+        idx %= alloc;
+    }
+    entries[idx] = str;
+    ++size;
+}
+
+
+
+String *IdentifierTable::insertString(const QString &s)
+{
+    uint hash = String::createHashValue(s.constData(), s.length());
+    uint idx = hash % alloc;
+    while (String *e = entries[idx]) {
+        if (e->stringHash == hash && e->toQString() == s)
+            return e;
+        ++idx;
+        idx %= alloc;
+    }
+
+    String *str = engine->newString(s);
+    addEntry(str);
+    return str;
+}
+
+
+Identifier *IdentifierTable::identifier(String *str)
+{
+    if (str->identifier)
+        return str->identifier;
+    uint hash = str->hashValue();
+    if (str->subtype >= String::StringType_UInt)
+        return 0;
+
+    uint idx = hash % alloc;
+    while (String *e = entries[idx]) {
+        if (e->stringHash == hash && e->isEqualTo(str)) {
+            str->identifier = e->identifier;
+            return e->identifier;
+        }
+        ++idx;
+        idx %= alloc;
+    }
+
+    addEntry(str);
+    return str->identifier;
+}
+
+Identifier *IdentifierTable::identifier(const QString &s)
+{
+    return insertString(s)->identifier;
+}
+
+Identifier *IdentifierTable::identifier(const char *s, int len)
+{
+    uint hash = String::createHashValue(s, len);
+    if (hash == UINT_MAX)
+        return identifier(QString::fromUtf8(s, len));
+
+    QLatin1String latin(s, len);
+    uint idx = hash % alloc;
+    while (String *e = entries[idx]) {
+        if (e->stringHash == hash && e->toQString() == latin)
+            return e->identifier;
+        ++idx;
+        idx %= alloc;
+    }
+
+    String *str = engine->newString(QString::fromLatin1(s, len));
+    addEntry(str);
+    return str->identifier;
+}
+
+}
+
+QT_END_NAMESPACE
diff --git a/src/qml/qml/v4/qv4identifiertable_p.h b/src/qml/qml/v4/qv4identifiertable_p.h
new file mode 100644 (file)
index 0000000..0f9a592
--- /dev/null
@@ -0,0 +1,86 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** 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 Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file.  Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#ifndef QV4IDENTIFIERTABLE_H
+#define QV4IDENTIFIERTABLE_H
+
+#include "qv4identifier_p.h"
+#include "qv4string_p.h"
+#include "qv4engine_p.h"
+#include <limits.h>
+
+QT_BEGIN_NAMESPACE
+
+namespace QV4 {
+
+struct IdentifierTable
+{
+    ExecutionEngine *engine;
+
+    int alloc;
+    int size;
+    int numBits;
+    String **entries;
+
+    void addEntry(String *str);
+
+public:
+
+    IdentifierTable(ExecutionEngine *engine);
+    ~IdentifierTable();
+
+    String *insertString(const QString &s);
+
+    Identifier *identifier(String *str);
+    Identifier *identifier(const QString &s);
+    Identifier *identifier(const char *s, int len);
+
+    void mark() {
+        for (int i = 0; i < alloc; ++i)
+            if (entries[i])
+                entries[i]->mark();
+    }
+};
+
+}
+
+QT_END_NAMESPACE
+
+#endif
index f7f898d..043e623 100644 (file)
@@ -44,7 +44,7 @@
 #include <qv4engine_p.h>
 #include <qv4identifier_p.h>
 #include "qv4object_p.h"
-#include "qv4identifier_p.h"
+#include "qv4identifiertable_p.h"
 
 QT_BEGIN_NAMESPACE
 
index e4b9676..bfdee2d 100644 (file)
@@ -40,7 +40,7 @@
 ****************************************************************************/
 
 #include "qv4string_p.h"
-#include "qv4identifier_p.h"
+#include "qv4identifiertable_p.h"
 #include "qv4runtime_p.h"
 #include "qv4objectproto_p.h"
 #include "qv4stringobject_p.h"
index 7ccc881..46b4770 100644 (file)
@@ -25,6 +25,7 @@ SOURCES += \
     $$PWD/qv4debugging.cpp \
     $$PWD/qv4lookup.cpp \
     $$PWD/qv4identifier.cpp \
+    $$PWD/qv4identifiertable.cpp \
     $$PWD/qv4mm.cpp \
     $$PWD/qv4managed.cpp \
     $$PWD/qv4internalclass.cpp \
@@ -75,6 +76,7 @@ HEADERS += \
     $$PWD/qv4debugging_p.h \
     $$PWD/qv4lookup_p.h \
     $$PWD/qv4identifier_p.h \
+    $$PWD/qv4identifiertable_p.h \
     $$PWD/qv4mm_p.h \
     $$PWD/qv4managed_p.h \
     $$PWD/qv4internalclass_p.h \