1// Copyright (C) 2020 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#ifndef QV4IDENTIFIERHASHDATA_H
4#define QV4IDENTIFIERHASHDATA_H
5
6//
7// W A R N I N G
8// -------------
9//
10// This file is not part of the Qt API. It exists purely as an
11// implementation detail. This header file may change from version to
12// version without notice, or even be removed.
13//
14// We mean it.
15//
16
17#include <private/qv4global_p.h>
18#include <private/qv4propertykey_p.h>
19#include <private/qv4identifiertable_p.h>
20#include <QtCore/qatomic.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace QV4 {
25
26struct IdentifierHashEntry {
27 PropertyKey identifier;
28 int value;
29};
30
31struct IdentifierHashData
32{
33 IdentifierHashData(IdentifierTable *table, int numBits)
34 : size(0)
35 , numBits(numBits)
36 , identifierTable(table)
37 {
38 refCount.storeRelaxed(newValue: 1);
39 alloc = qPrimeForNumBits(numBits);
40 entries = (IdentifierHashEntry *)malloc(size: alloc*sizeof(IdentifierHashEntry));
41 memset(s: entries, c: 0, n: alloc*sizeof(IdentifierHashEntry));
42 identifierTable->addIdentifierHash(h: this);
43 }
44
45 explicit IdentifierHashData(IdentifierHashData *other)
46 : size(other->size)
47 , numBits(other->numBits)
48 , identifierTable(other->identifierTable)
49 {
50 refCount.storeRelaxed(newValue: 1);
51 alloc = other->alloc;
52 entries = (IdentifierHashEntry *)malloc(size: alloc*sizeof(IdentifierHashEntry));
53 memcpy(dest: entries, src: other->entries, n: alloc*sizeof(IdentifierHashEntry));
54 identifierTable->addIdentifierHash(h: this);
55 }
56
57 ~IdentifierHashData() {
58 free(ptr: entries);
59 if (identifierTable)
60 identifierTable->removeIdentifierHash(h: this);
61 }
62
63 void markObjects(MarkStack *markStack) const
64 {
65 IdentifierHashEntry *e = entries;
66 IdentifierHashEntry *end = e + alloc;
67 while (e < end) {
68 if (Heap::Base *o = e->identifier.asStringOrSymbol())
69 o->mark(markStack);
70 ++e;
71 }
72 }
73
74 QBasicAtomicInt refCount;
75 int alloc;
76 int size;
77 int numBits;
78 IdentifierTable *identifierTable;
79 IdentifierHashEntry *entries;
80};
81
82} // namespace QV4
83
84QT_END_NAMESPACE
85
86#endif // QV4IDENTIFIERHASHDATA_P_H
87

source code of qtdeclarative/src/qml/jsruntime/qv4identifierhashdata_p.h