v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
persistent-handles.cc
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
6
7#include "src/api/api.h"
8#include "src/heap/heap-inl.h"
11
12namespace v8 {
13namespace internal {
14
16 : isolate_(isolate),
17 block_next_(nullptr),
18 block_limit_(nullptr),
19 prev_(nullptr),
20 next_(nullptr) {
21 isolate->persistent_handles_list()->Add(this);
22}
23
26
27 for (Address* block_start : blocks_) {
28#if ENABLE_GLOBAL_HANDLE_ZAPPING
29 HandleScope::ZapRange(block_start, block_start + kHandleBlockSize,
31#endif
32 DeleteArray(block_start);
33 }
34}
35
36#ifdef DEBUG
37void PersistentHandles::Attach(LocalHeap* local_heap) {
39 owner_ = local_heap;
40}
41
44 owner_ = nullptr;
45}
46
47void PersistentHandles::CheckOwnerIsNotParked() {
48 if (owner_) DCHECK(!owner_->IsParked());
49}
50
51bool PersistentHandles::Contains(Address* location) {
52 auto it = ordered_blocks_.upper_bound(location);
53 if (it == ordered_blocks_.begin()) return false;
54 --it;
55 DCHECK_LE(*it, location);
56 if (*it == blocks_.back()) {
57 // The last block is a special case because it may have
58 // less than block_size_ handles.
59 return location < block_next_;
60 }
61 return location < *it + kHandleBlockSize;
62}
63#endif
64
67
69 blocks_.push_back(block_start);
70
71 block_next_ = block_start;
72 block_limit_ = block_start + kHandleBlockSize;
73
74#ifdef DEBUG
75 ordered_blocks_.insert(block_start);
76#endif
77}
78
88
90 for (int i = 0; i < static_cast<int>(blocks_.size()) - 1; i++) {
91 Address* block_start = blocks_[i];
92 Address* block_end = block_start + kHandleBlockSize;
93 visitor->VisitRootPointers(Root::kHandleScope, nullptr,
94 FullObjectSlot(block_start),
95 FullObjectSlot(block_end));
96 }
97
98 if (!blocks_.empty()) {
99 Address* block_start = blocks_.back();
100 visitor->VisitRootPointers(Root::kHandleScope, nullptr,
101 FullObjectSlot(block_start),
103 }
104}
105
109 persistent_handles_head_->prev_ = persistent_handles;
110 persistent_handles->prev_ = nullptr;
111 persistent_handles->next_ = persistent_handles_head_;
112 persistent_handles_head_ = persistent_handles;
113}
114
117 if (persistent_handles->next_)
118 persistent_handles->next_->prev_ = persistent_handles->prev_;
119 if (persistent_handles->prev_)
120 persistent_handles->prev_->next_ = persistent_handles->next_;
121 else
122 persistent_handles_head_ = persistent_handles->next_;
123}
124
126 isolate->heap()->safepoint()->AssertActive();
128 current = current->next_) {
129 current->Iterate(visitor);
130 }
131}
132
134 : impl_(isolate->handle_scope_implementer()) {
136 HandleScopeData* data = impl_->isolate()->handle_scope_data();
137 Address* new_next = impl_->GetSpareOrNewBlock();
138 Address* new_limit = &new_next[kHandleBlockSize];
139 impl_->blocks()->push_back(new_next);
140
141#ifdef DEBUG
142 prev_level_ = data->level;
143#endif
144 data->level++;
145 first_block_ = new_next;
146 prev_limit_ = data->limit;
147 prev_next_ = data->next;
148 data->next = new_next;
149 data->limit = new_limit;
150}
151
157
158std::unique_ptr<PersistentHandles> PersistentHandlesScope::Detach() {
159 std::unique_ptr<PersistentHandles> ph = impl_->DetachPersistent(first_block_);
161 data->next = prev_next_;
162 data->limit = prev_limit_;
163#ifdef DEBUG
164 handles_detached_ = true;
165#endif
166 return ph;
167}
168
169// static
171 return isolate->handle_scope_implementer()->HasPersistentScope();
172}
173
174} // namespace internal
175} // namespace v8
Isolate * isolate_
internal::Address * GetSpareOrNewBlock()
Definition api.h:471
Isolate * isolate() const
Definition api.h:380
DetachableVector< Address * > * blocks()
Definition api.h:379
std::unique_ptr< PersistentHandles > DetachPersistent(Address *first_block)
Definition api.cc:12063
V8_INLINE HandleScopeData * handle_scope_data()
Definition isolate.h:1393
PersistentHandlesList * persistent_handles_list() const
Definition isolate.h:1687
void Iterate(RootVisitor *visitor, Isolate *isolate)
void Remove(PersistentHandles *persistent_handles)
void Add(PersistentHandles *persistent_handles)
HandleScopeImplementer *const impl_
static V8_EXPORT_PRIVATE bool IsActive(Isolate *isolate)
V8_EXPORT_PRIVATE std::unique_ptr< PersistentHandles > Detach()
V8_EXPORT_PRIVATE PersistentHandlesScope(Isolate *isolate)
V8_EXPORT_PRIVATE Address * GetHandle(Address value)
V8_EXPORT_PRIVATE PersistentHandles(Isolate *isolate)
std::vector< Address * > blocks_
V8_EXPORT_PRIVATE void Iterate(RootVisitor *visitor)
virtual void VisitRootPointers(Root root, const char *description, FullObjectSlot start, FullObjectSlot end)=0
const MapRef owner_
LineAndColumn current
std::ostream & impl_
constexpr uint32_t kPersistentHandleZapValue
Definition globals.h:1008
void DeleteArray(T *array)
Definition allocation.h:63
const int kHandleBlockSize
Definition api.h:444
return value
Definition map-inl.h:893
T * NewArray(size_t size)
Definition allocation.h:43
Node * prev_
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NULL(val)
Definition logging.h:491
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485