Line data Source code
1 : // Copyright 2006-2008 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_INL_H_
6 : #define V8_HANDLES_INL_H_
7 :
8 : #include "src/handles.h"
9 : #include "src/isolate.h"
10 : #include "src/objects-inl.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 : HandleBase::HandleBase(Object* object, Isolate* isolate)
16 259360154 : : location_(HandleScope::GetHandle(isolate, object)) {}
17 :
18 :
19 : template <typename T>
20 : // Allocate a new handle for the object, do not canonicalize.
21 : Handle<T> Handle<T>::New(T* object, Isolate* isolate) {
22 : return Handle(
23 : reinterpret_cast<T**>(HandleScope::CreateHandle(isolate, object)));
24 : }
25 :
26 :
27 158277155 : HandleScope::HandleScope(Isolate* isolate) {
28 : HandleScopeData* data = isolate->handle_scope_data();
29 173958424 : isolate_ = isolate;
30 318063817 : prev_next_ = data->next;
31 318064229 : prev_limit_ = data->limit;
32 318064229 : data->level++;
33 158277155 : }
34 :
35 : template <typename T>
36 487025730 : Handle<T>::Handle(T* object) : Handle(object, object->GetIsolate()) {}
37 :
38 : template <typename T>
39 : Handle<T>::Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {}
40 :
41 : template <typename T>
42 : inline std::ostream& operator<<(std::ostream& os, Handle<T> handle) {
43 8 : return os << Brief(*handle);
44 : }
45 :
46 :
47 158277148 : HandleScope::~HandleScope() {
48 : #ifdef DEBUG
49 : if (FLAG_check_handle_count) {
50 : int before = NumberOfHandles(isolate_);
51 : CloseScope(isolate_, prev_next_, prev_limit_);
52 : int after = NumberOfHandles(isolate_);
53 : DCHECK_LT(after - before, kCheckHandleThreshold);
54 : DCHECK_LT(before, kCheckHandleThreshold);
55 : } else {
56 : #endif // DEBUG
57 317870884 : CloseScope(isolate_, prev_next_, prev_limit_);
58 : #ifdef DEBUG
59 : }
60 : #endif // DEBUG
61 158277153 : }
62 :
63 :
64 940750711 : void HandleScope::CloseScope(Isolate* isolate,
65 : Object** prev_next,
66 : Object** prev_limit) {
67 : HandleScopeData* current = isolate->handle_scope_data();
68 :
69 : std::swap(current->next, prev_next);
70 940750711 : current->level--;
71 940750711 : if (current->limit != prev_limit) {
72 1155643 : current->limit = prev_limit;
73 1155643 : DeleteExtensions(isolate);
74 : #ifdef ENABLE_HANDLE_ZAPPING
75 1155659 : ZapRange(current->next, prev_limit);
76 : } else {
77 939595068 : ZapRange(current->next, prev_next);
78 : #endif
79 : }
80 940750600 : }
81 :
82 :
83 : template <typename T>
84 14374963 : Handle<T> HandleScope::CloseAndEscape(Handle<T> handle_value) {
85 14374963 : HandleScopeData* current = isolate_->handle_scope_data();
86 :
87 : T* value = *handle_value;
88 : // Throw away all handles in the current scope.
89 14374963 : CloseScope(isolate_, prev_next_, prev_limit_);
90 : // Allocate one handle in the parent scope.
91 : DCHECK(current->level > current->sealed_level);
92 14374963 : Handle<T> result(value, isolate_);
93 : // Reinitialize the current scope (so that it's ready
94 : // to be used or closed again).
95 14374963 : prev_next_ = current->next;
96 14374963 : prev_limit_ = current->limit;
97 14374963 : current->level++;
98 14374963 : return result;
99 : }
100 :
101 : Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
102 : DCHECK(AllowHandleAllocation::IsAllowed());
103 110604916 : HandleScopeData* data = isolate->handle_scope_data();
104 :
105 1969311520 : Object** result = data->next;
106 1969311520 : if (result == data->limit) result = Extend(isolate);
107 : // Update the current next field, set the value in the created
108 : // handle, and return the result.
109 : DCHECK(result < data->limit);
110 1969311525 : data->next = result + 1;
111 :
112 1969311525 : *result = value;
113 : return result;
114 : }
115 :
116 :
117 : Object** HandleScope::GetHandle(Isolate* isolate, Object* value) {
118 : DCHECK(AllowHandleAllocation::IsAllowed());
119 1815377001 : HandleScopeData* data = isolate->handle_scope_data();
120 1887121856 : CanonicalHandleScope* canonical = data->canonical_scope;
121 1887121856 : return canonical ? canonical->Lookup(value) : CreateHandle(isolate, value);
122 : }
123 :
124 :
125 : #ifdef DEBUG
126 : inline SealHandleScope::SealHandleScope(Isolate* isolate) : isolate_(isolate) {
127 : // Make sure the current thread is allowed to create handles to begin with.
128 : DCHECK(AllowHandleAllocation::IsAllowed());
129 : HandleScopeData* current = isolate_->handle_scope_data();
130 : // Shrink the current handle scope to make it impossible to do
131 : // handle allocations without an explicit handle scope.
132 : prev_limit_ = current->limit;
133 : current->limit = current->next;
134 : prev_sealed_level_ = current->sealed_level;
135 : current->sealed_level = current->level;
136 : }
137 :
138 :
139 : inline SealHandleScope::~SealHandleScope() {
140 : // Restore state in current handle scope to re-enable handle
141 : // allocations.
142 : HandleScopeData* current = isolate_->handle_scope_data();
143 : DCHECK_EQ(current->next, current->limit);
144 : current->limit = prev_limit_;
145 : DCHECK_EQ(current->level, current->sealed_level);
146 : current->sealed_level = prev_sealed_level_;
147 : }
148 :
149 : #endif
150 :
151 : } // namespace internal
152 : } // namespace v8
153 :
154 : #endif // V8_HANDLES_INL_H_
|