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

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: 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
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 if (IsWeakRetainer()) { 111 if (IsWeakRetainer()) {
110 global_handles->number_of_weak_handles_--; 112 global_handles->number_of_weak_handles_--;
111 if (object_->IsJSGlobalObject()) { 113 if (object_->IsJSGlobalObject()) {
112 global_handles->number_of_global_object_weak_handles_--; 114 global_handles->number_of_global_object_weak_handles_--;
113 } 115 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 flags_ = IsPartiallyDependent::update(flags_, v); 156 flags_ = IsPartiallyDependent::update(flags_, v);
155 } 157 }
156 158
157 bool is_in_new_space_list() { 159 bool is_in_new_space_list() {
158 return IsInNewSpaceList::decode(flags_); 160 return IsInNewSpaceList::decode(flags_);
159 } 161 }
160 void set_in_new_space_list(bool v) { 162 void set_in_new_space_list(bool v) {
161 flags_ = IsInNewSpaceList::update(flags_, v); 163 flags_ = IsInNewSpaceList::update(flags_, v);
162 } 164 }
163 165
166 bool is_new_callback() {
167 return IsNewCallback::decode(flags_);
168 }
169 void set_is_new_callback(bool v) {
170 flags_ = IsNewCallback::update(flags_, v);
171 }
172
164 bool IsNearDeath() const { 173 bool IsNearDeath() const {
165 // Check for PENDING to ensure correct answer when processing callbacks. 174 // Check for PENDING to ensure correct answer when processing callbacks.
166 return state() == PENDING || state() == NEAR_DEATH; 175 return state() == PENDING || state() == NEAR_DEATH;
167 } 176 }
168 177
169 bool IsWeak() const { return state() == WEAK; } 178 bool IsWeak() const { return state() == WEAK; }
170 179
171 bool IsRetainer() const { return state() != FREE; } 180 bool IsRetainer() const { return state() != FREE; }
172 181
173 bool IsStrongRetainer() const { return state() == NORMAL; } 182 bool IsStrongRetainer() const { return state() == NORMAL; }
(...skipping 14 matching lines...) Expand all
188 } 197 }
189 198
190 void MarkPartiallyDependent(GlobalHandles* global_handles) { 199 void MarkPartiallyDependent(GlobalHandles* global_handles) {
191 ASSERT(state() != FREE); 200 ASSERT(state() != FREE);
192 if (global_handles->isolate()->heap()->InNewSpace(object_)) { 201 if (global_handles->isolate()->heap()->InNewSpace(object_)) {
193 set_partially_dependent(true); 202 set_partially_dependent(true);
194 } 203 }
195 } 204 }
196 void clear_partially_dependent() { set_partially_dependent(false); } 205 void clear_partially_dependent() { set_partially_dependent(false); }
197 206
198 // Callback accessor. 207 // Callback accessors.
199 WeakReferenceCallback callback() { return callback_; } 208 WeakReferenceCallback old_callback() { return callback_.old_callback; }
209 WeakenedReferenceCallback new_callback() { return callback_.new_callback; }
200 210
201 // Callback parameter accessors. 211 // Callback parameter accessors.
202 void set_parameter(void* parameter) { 212 void set_parameter(void* parameter) {
203 ASSERT(state() != FREE); 213 ASSERT(state() != FREE);
204 parameter_or_next_free_.parameter = parameter; 214 parameter_or_next_free_.parameter = parameter;
205 } 215 }
206 void* parameter() const { 216 void* parameter() const {
207 ASSERT(state() != FREE); 217 ASSERT(state() != FREE);
208 return parameter_or_next_free_.parameter; 218 return parameter_or_next_free_.parameter;
209 } 219 }
(...skipping 13 matching lines...) Expand all
223 WeakReferenceCallback callback) { 233 WeakReferenceCallback callback) {
224 ASSERT(state() != FREE); 234 ASSERT(state() != FREE);
225 if (!IsWeakRetainer()) { 235 if (!IsWeakRetainer()) {
226 global_handles->number_of_weak_handles_++; 236 global_handles->number_of_weak_handles_++;
227 if (object_->IsJSGlobalObject()) { 237 if (object_->IsJSGlobalObject()) {
228 global_handles->number_of_global_object_weak_handles_++; 238 global_handles->number_of_global_object_weak_handles_++;
229 } 239 }
230 } 240 }
231 set_state(WEAK); 241 set_state(WEAK);
232 set_parameter(parameter); 242 set_parameter(parameter);
233 callback_ = callback; 243 set_is_new_callback(false);
244 callback_.old_callback = callback;
245 }
246
247 void MakeWeak(GlobalHandles* global_handles,
248 void* parameter,
249 WeakenedReferenceCallback callback) {
250 ASSERT(state() != FREE);
251 if (!IsWeakRetainer()) {
252 global_handles->number_of_weak_handles_++;
253 if (object_->IsJSGlobalObject()) {
254 global_handles->number_of_global_object_weak_handles_++;
255 }
256 }
257 set_state(WEAK);
258 set_parameter(parameter);
259 set_is_new_callback(true);
260 callback_.new_callback = callback;
234 } 261 }
235 262
236 void ClearWeakness(GlobalHandles* global_handles) { 263 void ClearWeakness(GlobalHandles* global_handles) {
237 ASSERT(state() != FREE); 264 ASSERT(state() != FREE);
238 if (IsWeakRetainer()) { 265 if (IsWeakRetainer()) {
239 global_handles->number_of_weak_handles_--; 266 global_handles->number_of_weak_handles_--;
240 if (object_->IsJSGlobalObject()) { 267 if (object_->IsJSGlobalObject()) {
241 global_handles->number_of_global_object_weak_handles_--; 268 global_handles->number_of_global_object_weak_handles_--;
242 } 269 }
243 } 270 }
244 set_state(NORMAL); 271 set_state(NORMAL);
245 set_parameter(NULL); 272 set_parameter(NULL);
246 } 273 }
247 274
248 bool PostGarbageCollectionProcessing(Isolate* isolate, 275 bool PostGarbageCollectionProcessing(Isolate* isolate,
249 GlobalHandles* global_handles) { 276 GlobalHandles* global_handles) {
250 if (state() != Node::PENDING) return false; 277 if (state() != Node::PENDING) return false;
251 WeakReferenceCallback func = callback(); 278 if (new_callback() == NULL) {
Sven Panne 2013/01/18 08:08:12 This is a hack and works only because we don't act
haraken 2013/01/22 00:27:37 Done.
252 if (func == NULL) {
253 Release(global_handles); 279 Release(global_handles);
254 return false; 280 return false;
255 } 281 }
256 void* par = parameter(); 282 void* par = parameter();
257 set_state(NEAR_DEATH); 283 set_state(NEAR_DEATH);
258 set_parameter(NULL); 284 set_parameter(NULL);
259 285
260 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle()); 286 v8::Persistent<v8::Object> object = ToApi<v8::Object>(handle());
261 { 287 {
262 // Check that we are not passing a finalized external string to 288 // Check that we are not passing a finalized external string to
263 // the callback. 289 // the callback.
264 ASSERT(!object_->IsExternalAsciiString() || 290 ASSERT(!object_->IsExternalAsciiString() ||
265 ExternalAsciiString::cast(object_)->resource() != NULL); 291 ExternalAsciiString::cast(object_)->resource() != NULL);
266 ASSERT(!object_->IsExternalTwoByteString() || 292 ASSERT(!object_->IsExternalTwoByteString() ||
267 ExternalTwoByteString::cast(object_)->resource() != NULL); 293 ExternalTwoByteString::cast(object_)->resource() != NULL);
268 // Leaving V8. 294 // Leaving V8.
269 VMState state(isolate, EXTERNAL); 295 VMState state(isolate, EXTERNAL);
270 func(object, par); 296 if (is_new_callback()) {
297 WeakenedReferenceCallback callback = new_callback();
298 callback(reinterpret_cast<v8::Isolate*>(isolate), object, par);
299 } else {
300 WeakReferenceCallback callback = old_callback();
301 callback(object, par);
302 }
271 } 303 }
272 // Absence of explicit cleanup or revival of weak handle 304 // Absence of explicit cleanup or revival of weak handle
273 // in most of the cases would lead to memory leak. 305 // in most of the cases would lead to memory leak.
274 ASSERT(state() != NEAR_DEATH); 306 ASSERT(state() != NEAR_DEATH);
275 return true; 307 return true;
276 } 308 }
277 309
278 private: 310 private:
279 inline NodeBlock* FindBlock(); 311 inline NodeBlock* FindBlock();
280 inline void IncreaseBlockUses(GlobalHandles* global_handles); 312 inline void IncreaseBlockUses(GlobalHandles* global_handles);
(...skipping 11 matching lines...) Expand all
292 324
293 // Index in the containing handle block. 325 // Index in the containing handle block.
294 uint8_t index_; 326 uint8_t index_;
295 327
296 // This stores three flags (independent, partially_dependent and 328 // This stores three flags (independent, partially_dependent and
297 // in_new_space_list) and a State. 329 // in_new_space_list) and a State.
298 class NodeState: public BitField<State, 0, 4> {}; 330 class NodeState: public BitField<State, 0, 4> {};
299 class IsIndependent: public BitField<bool, 4, 1> {}; 331 class IsIndependent: public BitField<bool, 4, 1> {};
300 class IsPartiallyDependent: public BitField<bool, 5, 1> {}; 332 class IsPartiallyDependent: public BitField<bool, 5, 1> {};
301 class IsInNewSpaceList: public BitField<bool, 6, 1> {}; 333 class IsInNewSpaceList: public BitField<bool, 6, 1> {};
334 class IsNewCallback: public BitField<bool, 7, 1> {};
302 335
303 uint8_t flags_; 336 uint8_t flags_;
304 337
305 // Handle specific callback. 338 // Handle specific callback.
306 WeakReferenceCallback callback_; 339 union {
340 WeakReferenceCallback old_callback;
341 WeakenedReferenceCallback new_callback;
342 } callback_;
307 343
308 // Provided data for callback. In FREE state, this is used for 344 // Provided data for callback. In FREE state, this is used for
309 // the free list link. 345 // the free list link.
310 union { 346 union {
311 void* parameter; 347 void* parameter;
312 Node* next_free; 348 Node* next_free;
313 } parameter_or_next_free_; 349 } parameter_or_next_free_;
314 350
315 DISALLOW_COPY_AND_ASSIGN(Node); 351 DISALLOW_COPY_AND_ASSIGN(Node);
316 }; 352 };
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 } 507 }
472 508
473 509
474 void GlobalHandles::MakeWeak(Object** location, void* parameter, 510 void GlobalHandles::MakeWeak(Object** location, void* parameter,
475 WeakReferenceCallback callback) { 511 WeakReferenceCallback callback) {
476 ASSERT(callback != NULL); 512 ASSERT(callback != NULL);
477 Node::FromLocation(location)->MakeWeak(this, parameter, callback); 513 Node::FromLocation(location)->MakeWeak(this, parameter, callback);
478 } 514 }
479 515
480 516
517 void GlobalHandles::MakeWeak(Object** location, void* parameter,
518 WeakenedReferenceCallback callback) {
519 ASSERT(callback != NULL);
520 Node::FromLocation(location)->MakeWeak(this, parameter, callback);
521 }
522
523
481 void GlobalHandles::ClearWeakness(Object** location) { 524 void GlobalHandles::ClearWeakness(Object** location) {
482 Node::FromLocation(location)->ClearWeakness(this); 525 Node::FromLocation(location)->ClearWeakness(this);
483 } 526 }
484 527
485 528
486 void GlobalHandles::MarkIndependent(Object** location) { 529 void GlobalHandles::MarkIndependent(Object** location) {
487 Node::FromLocation(location)->MarkIndependent(); 530 Node::FromLocation(location)->MarkIndependent();
488 } 531 }
489 532
490 533
(...skipping 25 matching lines...) Expand all
516 return Node::FromLocation(location)->wrapper_class_id(); 559 return Node::FromLocation(location)->wrapper_class_id();
517 } 560 }
518 561
519 void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) { 562 void GlobalHandles::IterateWeakRoots(ObjectVisitor* v) {
520 for (NodeIterator it(this); !it.done(); it.Advance()) { 563 for (NodeIterator it(this); !it.done(); it.Advance()) {
521 if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location()); 564 if (it.node()->IsWeakRetainer()) v->VisitPointer(it.node()->location());
522 } 565 }
523 } 566 }
524 567
525 568
526 void GlobalHandles::IterateWeakRoots(WeakReferenceGuest f,
527 WeakReferenceCallback callback) {
528 for (NodeIterator it(this); !it.done(); it.Advance()) {
529 if (it.node()->IsWeak() && it.node()->callback() == callback) {
530 f(it.node()->object(), it.node()->parameter());
531 }
532 }
533 }
534
535
536 void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) { 569 void GlobalHandles::IdentifyWeakHandles(WeakSlotCallback f) {
537 for (NodeIterator it(this); !it.done(); it.Advance()) { 570 for (NodeIterator it(this); !it.done(); it.Advance()) {
538 if (it.node()->IsWeak() && f(it.node()->location())) { 571 if (it.node()->IsWeak() && f(it.node()->location())) {
539 it.node()->MarkPending(); 572 it.node()->MarkPending();
540 } 573 }
541 } 574 }
542 } 575 }
543 576
544 577
545 void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) { 578 void GlobalHandles::IterateNewSpaceStrongAndDependentRoots(ObjectVisitor* v) {
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
826 implicit_ref_groups_.Clear(); 859 implicit_ref_groups_.Clear();
827 } 860 }
828 861
829 862
830 void GlobalHandles::TearDown() { 863 void GlobalHandles::TearDown() {
831 // TODO(1428): invoke weak callbacks. 864 // TODO(1428): invoke weak callbacks.
832 } 865 }
833 866
834 867
835 } } // namespace v8::internal 868 } } // namespace v8::internal
OLDNEW
« src/api.cc ('K') | « 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