Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1023)

Side by Side Diff: src/global-handles.cc

Issue 11959031: Add a new weak handle callback that passes an Isolate to an embedder (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Applied sven's comments Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/global-handles.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 #ifdef DEBUG 71 #ifdef DEBUG
72 ~Node() { 72 ~Node() {
73 // TODO(1428): if it's a weak handle we should have invoked its callback. 73 // TODO(1428): if it's a weak handle we should have invoked its callback.
74 // Zap the values for eager trapping. 74 // Zap the values for eager trapping.
75 object_ = NULL; 75 object_ = NULL;
76 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; 76 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
77 index_ = 0; 77 index_ = 0;
78 set_independent(false); 78 set_independent(false);
79 set_partially_dependent(false); 79 set_partially_dependent(false);
80 set_in_new_space_list(false); 80 set_in_new_space_list(false);
81 set_is_new_callback(false);
82 callback_.new_callback = NULL;
81 parameter_or_next_free_.next_free = NULL; 83 parameter_or_next_free_.next_free = NULL;
82 callback_ = NULL;
83 } 84 }
84 #endif 85 #endif
85 86
86 void Initialize(int index, Node** first_free) { 87 void Initialize(int index, Node** first_free) {
87 index_ = static_cast<uint8_t>(index); 88 index_ = static_cast<uint8_t>(index);
88 ASSERT(static_cast<int>(index_) == index); 89 ASSERT(static_cast<int>(index_) == index);
89 set_state(FREE); 90 set_state(FREE);
90 set_in_new_space_list(false); 91 set_in_new_space_list(false);
91 parameter_or_next_free_.next_free = *first_free; 92 parameter_or_next_free_.next_free = *first_free;
92 *first_free = this; 93 *first_free = this;
93 } 94 }
94 95
95 void Acquire(Object* object, GlobalHandles* global_handles) { 96 void Acquire(Object* object, GlobalHandles* global_handles) {
96 ASSERT(state() == FREE); 97 ASSERT(state() == FREE);
97 object_ = object; 98 object_ = object;
98 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId; 99 class_id_ = v8::HeapProfiler::kPersistentHandleNoClassId;
99 set_independent(false); 100 set_independent(false);
100 set_partially_dependent(false); 101 set_partially_dependent(false);
101 set_state(NORMAL); 102 set_state(NORMAL);
102 parameter_or_next_free_.parameter = NULL; 103 parameter_or_next_free_.parameter = NULL;
103 callback_ = NULL; 104 set_is_new_callback(false);
105 callback_.new_callback = NULL;
104 IncreaseBlockUses(global_handles); 106 IncreaseBlockUses(global_handles);
105 } 107 }
106 108
107 void Release(GlobalHandles* global_handles) { 109 void Release(GlobalHandles* global_handles) {
108 ASSERT(state() != FREE); 110 ASSERT(state() != FREE);
109 set_state(FREE); 111 set_state(FREE);
110 parameter_or_next_free_.next_free = global_handles->first_free_; 112 parameter_or_next_free_.next_free = global_handles->first_free_;
111 global_handles->first_free_ = this; 113 global_handles->first_free_ = this;
112 DecreaseBlockUses(global_handles); 114 DecreaseBlockUses(global_handles);
113 } 115 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 flags_ = IsPartiallyDependent::update(flags_, v); 149 flags_ = IsPartiallyDependent::update(flags_, v);
148 } 150 }
149 151
150 bool is_in_new_space_list() { 152 bool is_in_new_space_list() {
151 return IsInNewSpaceList::decode(flags_); 153 return IsInNewSpaceList::decode(flags_);
152 } 154 }
153 void set_in_new_space_list(bool v) { 155 void set_in_new_space_list(bool v) {
154 flags_ = IsInNewSpaceList::update(flags_, v); 156 flags_ = IsInNewSpaceList::update(flags_, v);
155 } 157 }
156 158
159 bool is_new_callback() {
160 return IsNewCallback::decode(flags_);
161 }
162 void set_is_new_callback(bool v) {
163 flags_ = IsNewCallback::update(flags_, v);
164 }
165
157 bool IsNearDeath() const { 166 bool IsNearDeath() const {
158 // Check for PENDING to ensure correct answer when processing callbacks. 167 // Check for PENDING to ensure correct answer when processing callbacks.
159 return state() == PENDING || state() == NEAR_DEATH; 168 return state() == PENDING || state() == NEAR_DEATH;
160 } 169 }
161 170
162 bool IsWeak() const { return state() == WEAK; } 171 bool IsWeak() const { return state() == WEAK; }
163 172
164 bool IsRetainer() const { return state() != FREE; } 173 bool IsRetainer() const { return state() != FREE; }
165 174
166 bool IsStrongRetainer() const { return state() == NORMAL; } 175 bool IsStrongRetainer() const { return state() == NORMAL; }
(...skipping 14 matching lines...) Expand all
181 } 190 }
182 191
183 void MarkPartiallyDependent(GlobalHandles* global_handles) { 192 void MarkPartiallyDependent(GlobalHandles* global_handles) {
184 ASSERT(state() != FREE); 193 ASSERT(state() != FREE);
185 if (global_handles->isolate()->heap()->InNewSpace(object_)) { 194 if (global_handles->isolate()->heap()->InNewSpace(object_)) {
186 set_partially_dependent(true); 195 set_partially_dependent(true);
187 } 196 }
188 } 197 }
189 void clear_partially_dependent() { set_partially_dependent(false); } 198 void clear_partially_dependent() { set_partially_dependent(false); }
190 199
191 // Callback accessor. 200 // Callback accessors.
192 WeakReferenceCallback callback() { return callback_; } 201 WeakReferenceCallback old_callback() { return callback_.old_callback; }
202 WeakHandleCallback new_callback() { return callback_.new_callback; }
203 bool callback_is_set() { return !!callback_.new_callback; }
193 204
194 // Callback parameter accessors. 205 // Callback parameter accessors.
195 void set_parameter(void* parameter) { 206 void set_parameter(void* parameter) {
196 ASSERT(state() != FREE); 207 ASSERT(state() != FREE);
197 parameter_or_next_free_.parameter = parameter; 208 parameter_or_next_free_.parameter = parameter;
198 } 209 }
199 void* parameter() const { 210 void* parameter() const {
200 ASSERT(state() != FREE); 211 ASSERT(state() != FREE);
201 return parameter_or_next_free_.parameter; 212 return parameter_or_next_free_.parameter;
202 } 213 }
203 214
204 // Accessors for next free node in the free list. 215 // Accessors for next free node in the free list.
205 Node* next_free() { 216 Node* next_free() {
206 ASSERT(state() == FREE); 217 ASSERT(state() == FREE);
207 return parameter_or_next_free_.next_free; 218 return parameter_or_next_free_.next_free;
208 } 219 }
209 void set_next_free(Node* value) { 220 void set_next_free(Node* value) {
210 ASSERT(state() == FREE); 221 ASSERT(state() == FREE);
211 parameter_or_next_free_.next_free = value; 222 parameter_or_next_free_.next_free = value;
212 } 223 }
213 224
214 void MakeWeak(GlobalHandles* global_handles, 225 void MakeWeak(GlobalHandles* global_handles,
215 void* parameter, 226 void* parameter,
216 WeakReferenceCallback callback) { 227 WeakReferenceCallback callback) {
217 ASSERT(state() != FREE); 228 ASSERT(state() != FREE);
218 set_state(WEAK); 229 set_state(WEAK);
219 set_parameter(parameter); 230 set_parameter(parameter);
220 callback_ = callback; 231 set_is_new_callback(false);
232 callback_.old_callback = callback;
233 }
234
235 void MakeWeak(GlobalHandles* global_handles,
236 void* parameter,
237 WeakHandleCallback callback) {
238 ASSERT(state() != FREE);
239 set_state(WEAK);
240 set_parameter(parameter);
241 set_is_new_callback(true);
242 callback_.new_callback = callback;
221 } 243 }
222 244
223 void ClearWeakness(GlobalHandles* global_handles) { 245 void ClearWeakness(GlobalHandles* global_handles) {
224 ASSERT(state() != FREE); 246 ASSERT(state() != FREE);
225 set_state(NORMAL); 247 set_state(NORMAL);
226 set_parameter(NULL); 248 set_parameter(NULL);
227 } 249 }
228 250
229 bool PostGarbageCollectionProcessing(Isolate* isolate, 251 bool PostGarbageCollectionProcessing(Isolate* isolate,
230 GlobalHandles* global_handles) { 252 GlobalHandles* global_handles) {
231 if (state() != Node::PENDING) return false; 253 if (state() != Node::PENDING) return false;
232 WeakReferenceCallback func = callback(); 254 if (!callback_is_set()) {
233 if (func == NULL) {
234 Release(global_handles); 255 Release(global_handles);
235 return false; 256 return false;
236 } 257 }
237 void* par = parameter(); 258 void* par = parameter();
238 set_state(NEAR_DEATH); 259 set_state(NEAR_DEATH);
239 set_parameter(NULL); 260 set_parameter(NULL);
240 261
241 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); 262 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle());
242 { 263 {
243 // Check that we are not passing a finalized external string to 264 // Check that we are not passing a finalized external string to
244 // the callback. 265 // the callback.
245 ASSERT(!object_->IsExternalAsciiString() || 266 ASSERT(!object_->IsExternalAsciiString() ||
246 ExternalAsciiString::cast(object_)->resource() != NULL); 267 ExternalAsciiString::cast(object_)->resource() != NULL);
247 ASSERT(!object_->IsExternalTwoByteString() || 268 ASSERT(!object_->IsExternalTwoByteString() ||
248 ExternalTwoByteString::cast(object_)->resource() != NULL); 269 ExternalTwoByteString::cast(object_)->resource() != NULL);
249 // Leaving V8. 270 // Leaving V8.
250 VMState state(isolate, EXTERNAL); 271 VMState state(isolate, EXTERNAL);
251 func(object, par); 272 if (is_new_callback()) {
273 WeakHandleCallback callback = new_callback();
274 callback(reinterpret_cast<v8::Isolate*>(isolate), object, par);
275 } else {
276 WeakReferenceCallback callback = old_callback();
277 callback(object, par);
278 }
252 } 279 }
253 // Absence of explicit cleanup or revival of weak handle 280 // Absence of explicit cleanup or revival of weak handle
254 // in most of the cases would lead to memory leak. 281 // in most of the cases would lead to memory leak.
255 ASSERT(state() != NEAR_DEATH); 282 ASSERT(state() != NEAR_DEATH);
256 return true; 283 return true;
257 } 284 }
258 285
259 private: 286 private:
260 inline NodeBlock* FindBlock(); 287 inline NodeBlock* FindBlock();
261 inline void IncreaseBlockUses(GlobalHandles* global_handles); 288 inline void IncreaseBlockUses(GlobalHandles* global_handles);
(...skipping 11 matching lines...) Expand all
273 300
274 // Index in the containing handle block. 301 // Index in the containing handle block.
275 uint8_t index_; 302 uint8_t index_;
276 303
277 // This stores three flags (independent, partially_dependent and 304 // This stores three flags (independent, partially_dependent and
278 // in_new_space_list) and a State. 305 // in_new_space_list) and a State.
279 class NodeState: public BitField<State, 0, 4> {}; 306 class NodeState: public BitField<State, 0, 4> {};
280 class IsIndependent: public BitField<bool, 4, 1> {}; 307 class IsIndependent: public BitField<bool, 4, 1> {};
281 class IsPartiallyDependent: public BitField<bool, 5, 1> {}; 308 class IsPartiallyDependent: public BitField<bool, 5, 1> {};
282 class IsInNewSpaceList: public BitField<bool, 6, 1> {}; 309 class IsInNewSpaceList: public BitField<bool, 6, 1> {};
310 class IsNewCallback: public BitField<bool, 7, 1> {};
283 311
284 uint8_t flags_; 312 uint8_t flags_;
285 313
286 // Handle specific callback. 314 // Handle specific callback.
287 WeakReferenceCallback callback_; 315 union {
316 WeakReferenceCallback old_callback;
317 WeakHandleCallback new_callback;
318 } callback_;
288 319
289 // Provided data for callback. In FREE state, this is used for 320 // Provided data for callback. In FREE state, this is used for
290 // the free list link. 321 // the free list link.
291 union { 322 union {
292 void* parameter; 323 void* parameter;
293 Node* next_free; 324 Node* next_free;
294 } parameter_or_next_free_; 325 } parameter_or_next_free_;
295 326
296 DISALLOW_COPY_AND_ASSIGN(Node); 327 DISALLOW_COPY_AND_ASSIGN(Node);
297 }; 328 };
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 } 481 }
451 482
452 483
453 void GlobalHandles::MakeWeak(Object** location, void* parameter, 484 void GlobalHandles::MakeWeak(Object** location, void* parameter,
454 WeakReferenceCallback callback) { 485 WeakReferenceCallback callback) {
455 ASSERT(callback != NULL); 486 ASSERT(callback != NULL);
456 Node::FromLocation(location)->MakeWeak(this, parameter, callback); 487 Node::FromLocation(location)->MakeWeak(this, parameter, callback);
457 } 488 }
458 489
459 490
491 void GlobalHandles::MakeWeak(Object** location, void* parameter,
492 WeakHandleCallback callback) {
493 ASSERT(callback != NULL);
494 Node::FromLocation(location)->MakeWeak(this, parameter, callback);
495 }
496
497
460 void GlobalHandles::ClearWeakness(Object** location) { 498 void GlobalHandles::ClearWeakness(Object** location) {
461 Node::FromLocation(location)->ClearWeakness(this); 499 Node::FromLocation(location)->ClearWeakness(this);
462 } 500 }
463 501
464 502
465 void GlobalHandles::MarkIndependent(Object** location) { 503 void GlobalHandles::MarkIndependent(Object** location) {
466 Node::FromLocation(location)->MarkIndependent(); 504 Node::FromLocation(location)->MarkIndependent();
467 } 505 }
468 506
469 507
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 implicit_ref_groups_.Clear(); 848 implicit_ref_groups_.Clear();
811 } 849 }
812 850
813 851
814 void GlobalHandles::TearDown() { 852 void GlobalHandles::TearDown() {
815 // TODO(1428): invoke weak callbacks. 853 // TODO(1428): invoke weak callbacks.
816 } 854 }
817 855
818 856
819 } } // namespace v8::internal 857 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/global-handles.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698