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

Side by Side Diff: runtime/vm/dart_api_state.h

Issue 9531001: Implement weak references sets and provide an embedding API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address review comments Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/gc_marker.h » ('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 (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef VM_DART_API_STATE_H_ 5 #ifndef VM_DART_API_STATE_H_
6 #define VM_DART_API_STATE_H_ 6 #define VM_DART_API_STATE_H_
7 7
8 #include "include/dart_api.h" 8 #include "include/dart_api.h"
9 9
10 #include "platform/thread.h" 10 #include "platform/thread.h"
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 int CountHandles() const { 341 int CountHandles() const {
342 return CountScopedHandles(); 342 return CountScopedHandles();
343 } 343 }
344 344
345 private: 345 private:
346 WeakPersistentHandle* free_list_; 346 WeakPersistentHandle* free_list_;
347 DISALLOW_COPY_AND_ASSIGN(WeakPersistentHandles); 347 DISALLOW_COPY_AND_ASSIGN(WeakPersistentHandles);
348 }; 348 };
349 349
350 350
351 class WeakReference {
352 public:
353 WeakReference(Dart_Handle* keys, intptr_t keys_length,
354 Dart_Handle* values, intptr_t values_length)
355 : next_(NULL),
356 keys_(keys), num_keys_(keys_length),
357 values_(values), num_values_(values_length) {
358 }
359 ~WeakReference() {}
360
361 WeakReference* next() const { return next_; }
362
363 intptr_t num_keys() const { return num_keys_; }
364 RawObject** get_key(intptr_t i) {
365 ASSERT(i >= 0);
366 ASSERT(i < num_keys_);
367 return reinterpret_cast<RawObject**>(keys_[i]);
368 }
369
370 intptr_t num_values() const { return num_values_; }
371 RawObject** get_value(intptr_t i) {
372 ASSERT(i >= 0);
373 ASSERT(i < num_values_);
374 return reinterpret_cast<RawObject**>(values_[i]);
375 }
376
377 static WeakReference* Pop(WeakReference** queue) {
378 ASSERT(queue != NULL);
379 WeakReference* head = *queue;
380 if (head != NULL) {
381 *queue = head->next();
382 head->next_ = NULL;
383 }
384 return head;
385 }
386
387 static void Push(WeakReference* reference, WeakReference** queue) {
388 ASSERT(reference != NULL);
389 ASSERT(queue != NULL);
390 reference->next_ = *queue;
391 *queue = reference;
392 }
393
394 private:
395 WeakReference* next_;
396 Dart_Handle* keys_;
397 intptr_t num_keys_;
398 Dart_Handle* values_;
399 intptr_t num_values_;
400 DISALLOW_COPY_AND_ASSIGN(WeakReference);
401 };
402
403
351 // Structure used for the implementation of local scopes used in dart_api. 404 // Structure used for the implementation of local scopes used in dart_api.
352 // These local scopes manage handles and memory allocated in the scope. 405 // These local scopes manage handles and memory allocated in the scope.
353 class ApiLocalScope { 406 class ApiLocalScope {
354 public: 407 public:
355 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : 408 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) :
356 previous_(previous), stack_marker_(stack_marker) { } 409 previous_(previous), stack_marker_(stack_marker) { }
357 ~ApiLocalScope() { 410 ~ApiLocalScope() {
358 previous_ = NULL; 411 previous_ = NULL;
359 } 412 }
360 413
(...skipping 12 matching lines...) Expand all
373 426
374 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); 427 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope);
375 }; 428 };
376 429
377 430
378 // Implementation of the API State used in dart api for maintaining 431 // Implementation of the API State used in dart api for maintaining
379 // local scopes, persistent handles etc. These are setup on a per isolate 432 // local scopes, persistent handles etc. These are setup on a per isolate
380 // basis and destroyed when the isolate is shutdown. 433 // basis and destroyed when the isolate is shutdown.
381 class ApiState { 434 class ApiState {
382 public: 435 public:
383 ApiState() : top_scope_(NULL), null_(NULL), true_(NULL), false_(NULL) { } 436 ApiState() : top_scope_(NULL), delayed_weak_references_(NULL),
437 null_(NULL), true_(NULL), false_(NULL) { }
384 ~ApiState() { 438 ~ApiState() {
385 while (top_scope_ != NULL) { 439 while (top_scope_ != NULL) {
386 ApiLocalScope* scope = top_scope_; 440 ApiLocalScope* scope = top_scope_;
387 top_scope_ = top_scope_->previous(); 441 top_scope_ = top_scope_->previous();
388 delete scope; 442 delete scope;
389 } 443 }
390 if (null_ != NULL) { 444 if (null_ != NULL) {
391 persistent_handles().FreeHandle(null_); 445 persistent_handles().FreeHandle(null_);
392 null_ = NULL; 446 null_ = NULL;
393 } 447 }
394 if (true_ != NULL) { 448 if (true_ != NULL) {
395 persistent_handles().FreeHandle(true_); 449 persistent_handles().FreeHandle(true_);
396 true_ = NULL; 450 true_ = NULL;
397 } 451 }
398 if (false_ != NULL) { 452 if (false_ != NULL) {
399 persistent_handles().FreeHandle(false_); 453 persistent_handles().FreeHandle(false_);
400 false_ = NULL; 454 false_ = NULL;
401 } 455 }
402 } 456 }
403 457
404 // Accessors. 458 // Accessors.
405 ApiLocalScope* top_scope() const { return top_scope_; } 459 ApiLocalScope* top_scope() const { return top_scope_; }
406 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } 460 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; }
407 PersistentHandles& persistent_handles() { return persistent_handles_; } 461 PersistentHandles& persistent_handles() { return persistent_handles_; }
408 WeakPersistentHandles& weak_persistent_handles() { 462 WeakPersistentHandles& weak_persistent_handles() {
409 return weak_persistent_handles_; 463 return weak_persistent_handles_;
410 } 464 }
411 465 WeakReference* delayed_weak_references() { return delayed_weak_references_; }
466 void set_delayed_weak_references(WeakReference* reference) {
467 delayed_weak_references_ = reference;
468 }
412 void UnwindScopes(uword sp) { 469 void UnwindScopes(uword sp) {
413 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { 470 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) {
414 ApiLocalScope* scope = top_scope_; 471 ApiLocalScope* scope = top_scope_;
415 top_scope_ = top_scope_->previous(); 472 top_scope_ = top_scope_->previous();
416 delete scope; 473 delete scope;
417 } 474 }
418 } 475 }
419 476
420 void VisitObjectPointers(ObjectPointerVisitor* visitor) { 477 void VisitObjectPointers(ObjectPointerVisitor* visitor) {
421 ApiLocalScope* scope = top_scope_; 478 ApiLocalScope* scope = top_scope_;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 if (false_ == NULL) { 556 if (false_ == NULL) {
500 DARTSCOPE(Isolate::Current()); 557 DARTSCOPE(Isolate::Current());
501 558
502 const Object& false_object = Object::Handle(Bool::False()); 559 const Object& false_object = Object::Handle(Bool::False());
503 false_ = persistent_handles().AllocateHandle(); 560 false_ = persistent_handles().AllocateHandle();
504 false_->set_raw(false_object); 561 false_->set_raw(false_object);
505 } 562 }
506 return false_; 563 return false_;
507 } 564 }
508 565
566 void DelayWeakReference(WeakReference* reference) {
567 WeakReference::Push(reference, &delayed_weak_references_);
568 }
569
509 private: 570 private:
510 PersistentHandles persistent_handles_; 571 PersistentHandles persistent_handles_;
511 WeakPersistentHandles weak_persistent_handles_; 572 WeakPersistentHandles weak_persistent_handles_;
512 ApiLocalScope* top_scope_; 573 ApiLocalScope* top_scope_;
574 WeakReference* delayed_weak_references_;
513 575
514 // Persistent handles to important objects. 576 // Persistent handles to important objects.
515 PersistentHandle* null_; 577 PersistentHandle* null_;
516 PersistentHandle* true_; 578 PersistentHandle* true_;
517 PersistentHandle* false_; 579 PersistentHandle* false_;
518 580
519 DISALLOW_COPY_AND_ASSIGN(ApiState); 581 DISALLOW_COPY_AND_ASSIGN(ApiState);
520 }; 582 };
521 583
522 584
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 ApiNativeScope::Current()->zone()->GetBaseZone()) {} 621 ApiNativeScope::Current()->zone()->GetBaseZone()) {}
560 ApiGrowableArray() 622 ApiGrowableArray()
561 : BaseGrowableArray<T, ValueObject>( 623 : BaseGrowableArray<T, ValueObject>(
562 ApiNativeScope::Current()->zone()->GetBaseZone()) {} 624 ApiNativeScope::Current()->zone()->GetBaseZone()) {}
563 }; 625 };
564 626
565 627
566 } // namespace dart 628 } // namespace dart
567 629
568 #endif // VM_DART_API_STATE_H_ 630 #endif // VM_DART_API_STATE_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/gc_marker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698