v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-table.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_OBJECTS_STRING_TABLE_H_
6#define V8_OBJECTS_STRING_TABLE_H_
7
10#include "src/roots/roots.h"
11
12// Has to be the last include (doesn't have include guards):
14
15namespace v8 {
16namespace internal {
17
18// A generic key for lookups into the string table, which allows heteromorphic
19// lookup and on-demand creation of new strings.
21 public:
22 virtual ~StringTableKey() = default;
23 inline StringTableKey(uint32_t raw_hash_field, uint32_t length);
24
25 uint32_t raw_hash_field() const {
27 return raw_hash_field_;
28 }
29
30 inline uint32_t hash() const;
31 uint32_t length() const { return length_; }
32
33 protected:
34 inline void set_raw_hash_field(uint32_t raw_hash_field);
35
36 private:
37 uint32_t raw_hash_field_ = 0;
38 uint32_t length_;
39};
40
42
43// StringTable, for internalizing strings. The Lookup methods are designed to be
44// thread-safe, in combination with GC safepoints.
45//
46// The string table layout is defined by its Data implementation class, see
47// StringTable::Data for details.
49 public:
50 static constexpr Tagged<Smi> empty_element() { return Smi::FromInt(0); }
51 static constexpr Tagged<Smi> deleted_element() { return Smi::FromInt(1); }
52
53 explicit StringTable(Isolate* isolate);
55
56 int Capacity() const;
57 int NumberOfElements() const;
58
59 // Find string in the string table. If it is not there yet, it is
60 // added. The return value is the string found.
62
63 // Find string in the string table, using the given key. If the string is not
64 // there yet, it is created (by the key) and added. The return value is the
65 // string found.
66 template <typename StringTableKey, typename IsolateT>
67 DirectHandle<String> LookupKey(IsolateT* isolate, StringTableKey* key);
68
69 // {raw_string} must be a tagged String pointer.
70 // Returns a tagged pointer: either a Smi if the string is an array index, an
71 // internalized string, or a Smi sentinel.
72 static Address TryStringToIndexOrLookupExisting(Isolate* isolate,
73 Address raw_string);
74
75 // Insert a range of strings. Only for use during isolate deserialization.
76 void InsertForIsolateDeserialization(
77 Isolate* isolate, const base::Vector<DirectHandle<String>>& strings);
78
79 // Insert the single empty string. Only for use during heap bootstrapping.
80 void InsertEmptyStringForBootstrapping(Isolate* isolate);
81
82 void Print(PtrComprCageBase cage_base) const;
83 size_t GetCurrentMemoryUsage() const;
84
85 // The following methods must be called either while holding the write lock,
86 // or while in a Heap safepoint.
87 void IterateElements(RootVisitor* visitor);
88 void DropOldData();
89 void NotifyElementsRemoved(int count);
90
91 void VerifyIfOwnedBy(Isolate* isolate);
92
93 private:
95 class Data;
96
97 Data* EnsureCapacity(PtrComprCageBase cage_base, int additional_elements);
98
99 std::atomic<Data*> data_;
100 // Write mutex is mutable so that readers of concurrently mutated values (e.g.
101 // NumberOfElements) are allowed to lock it while staying const.
104};
105
106} // namespace internal
107} // namespace v8
108
110
111#endif // V8_OBJECTS_STRING_TABLE_H_
virtual ~StringTableKey()=default
uint32_t raw_hash_field() const
void set_raw_hash_field(uint32_t raw_hash_field)
StringTableKey(uint32_t raw_hash_field, uint32_t length)
static constexpr Tagged< Smi > empty_element()
void VerifyIfOwnedBy(Isolate *isolate)
static constexpr Tagged< Smi > deleted_element()
std::atomic< Data * > data_
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::unique_ptr< ValueMirror > key