v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
persistent-handles.h
Go to the documentation of this file.
1// Copyright 2020 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_HANDLES_PERSISTENT_HANDLES_H_
6#define V8_HANDLES_PERSISTENT_HANDLES_H_
7
8#include <vector>
9
10#include "include/v8-internal.h"
11#include "src/api/api.h"
12#include "src/base/macros.h"
15#include "testing/gtest/include/gtest/gtest_prod.h" // nogncheck
16
17namespace v8 {
18namespace internal {
19
20class Heap;
21
22// PersistentHandles serves as a container for handles that can be passed back
23// and forth between threads. Allocation and deallocation of this class is
24// thread-safe and the isolate tracks all PersistentHandles containers.
26 public:
29
32
34
35 template <typename T>
37#ifdef DEBUG
38 CheckOwnerIsNotParked();
39#endif
40 return IndirectHandle<T>(GetHandle(obj.ptr()));
41 }
42
43 template <typename T>
47
48 template <typename T>
52
53 template <typename T>
55 static_assert(kTaggedCanConvertToRawObjects);
56 return NewHandle(Tagged<T>(obj));
57 }
58
59 Isolate* isolate() const { return isolate_; }
60
61#ifdef DEBUG
62 V8_EXPORT_PRIVATE bool Contains(Address* location);
63#endif
64
65 private:
66 void AddBlock();
68
69#ifdef DEBUG
70 void Attach(LocalHeap* local_heap);
71 void Detach();
72 V8_EXPORT_PRIVATE void CheckOwnerIsNotParked();
73
74 LocalHeap* owner_ = nullptr;
75
76#else
77 void Attach(LocalHeap*) {}
78 void Detach() {}
79#endif
80
82 std::vector<Address*> blocks_;
83
86
89
90#ifdef DEBUG
91 std::set<Address*> ordered_blocks_;
92#endif
93
95 friend class LocalHeap;
97
98 FRIEND_TEST(PersistentHandlesTest, OrderOfBlocks);
99};
100
102 public:
104
105 void Iterate(RootVisitor* visitor, Isolate* isolate);
106
107 private:
108 void Add(PersistentHandles* persistent_handles);
109 void Remove(PersistentHandles* persistent_handles);
110
113
114 friend class PersistentHandles;
115};
116
117// PersistentHandlesScope sets up a scope in which all created main thread
118// handles become persistent handles that can be sent to another thread.
120 public:
123
124 // Moves all blocks of this scope into PersistentHandles and returns it.
125 V8_EXPORT_PRIVATE std::unique_ptr<PersistentHandles> Detach();
126
127 // Returns true if the current active handle scope is a persistent handle
128 // scope, thus all handles created become persistent handles.
129 V8_EXPORT_PRIVATE static bool IsActive(Isolate* isolate);
130
131 private:
132 Address* first_block_;
133 Address* prev_limit_;
134 Address* prev_next_;
136
137#ifdef DEBUG
138 bool handles_detached_ = false;
139 int prev_level_;
140#endif
141};
142
143} // namespace internal
144} // namespace v8
145
146#endif // V8_HANDLES_PERSISTENT_HANDLES_H_
void Iterate(RootVisitor *visitor, Isolate *isolate)
void Remove(PersistentHandles *persistent_handles)
void Add(PersistentHandles *persistent_handles)
HandleScopeImplementer *const impl_
V8_EXPORT_PRIVATE Address * GetHandle(Address value)
PersistentHandles & operator=(const PersistentHandles &)=delete
V8_EXPORT_PRIVATE PersistentHandles(Isolate *isolate)
std::vector< Address * > blocks_
IndirectHandle< T > NewHandle(DirectHandle< T > obj)
FRIEND_TEST(PersistentHandlesTest, OrderOfBlocks)
PersistentHandles(const PersistentHandles &)=delete
IndirectHandle< T > NewHandle(Tagged< T > obj)
IndirectHandle< T > NewHandle(IndirectHandle< T > obj)
IndirectHandle< T > NewHandle(T obj)
V8_EXPORT_PRIVATE void Iterate(RootVisitor *visitor)
V8_INLINE constexpr StorageType ptr() const
const MapRef owner_
static constexpr bool kTaggedCanConvertToRawObjects
Definition tagged.h:329
Handle< T > IndirectHandle
Definition globals.h:1086
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_NODISCARD
Definition v8config.h:693