Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 private: | 394 private: |
| 395 WeakReference* next_; | 395 WeakReference* next_; |
| 396 Dart_Handle* keys_; | 396 Dart_Handle* keys_; |
| 397 intptr_t num_keys_; | 397 intptr_t num_keys_; |
| 398 Dart_Handle* values_; | 398 Dart_Handle* values_; |
| 399 intptr_t num_values_; | 399 intptr_t num_values_; |
| 400 DISALLOW_COPY_AND_ASSIGN(WeakReference); | 400 DISALLOW_COPY_AND_ASSIGN(WeakReference); |
| 401 }; | 401 }; |
| 402 | 402 |
| 403 | 403 |
| 404 // A container for garbage collection callback function pointers. | |
| 405 // Pointers to the callback methods are stored within linked list | |
| 406 // nodes managed by the container. | |
|
turnidge
2012/03/07 00:25:16
I dislike how much code ends up in this header fil
cshapiro
2012/03/07 03:06:10
Yeah, moved to its own file.
| |
| 407 template<typename T> | |
| 408 class GcCallbacks { | |
| 409 public: | |
| 410 GcCallbacks() : head_(NULL) { | |
| 411 } | |
| 412 | |
| 413 ~GcCallbacks() { | |
| 414 while (head_ != NULL) { | |
| 415 Link* prev = head_; | |
| 416 head_ = head_->next_; | |
| 417 delete prev; | |
| 418 } | |
| 419 } | |
| 420 | |
| 421 // Adds a new callback to the list. The new callback must not | |
| 422 // already be present in the list. | |
| 423 void Add(T callback) { | |
| 424 ASSERT(callback != NULL); | |
| 425 Link* link = new Link(callback, head_); | |
| 426 head_ = link; | |
| 427 } | |
| 428 | |
| 429 // Removes a callback from the list. The callback must be present | |
| 430 // in the list. | |
| 431 void Remove(T callback) { | |
| 432 ASSERT(callback != NULL); | |
| 433 if (head_ == NULL) return; | |
| 434 Link* prev = head_; | |
| 435 Link* curr = head_->next_; | |
| 436 if (prev->callback_ == callback) { | |
| 437 head_ = curr; | |
| 438 delete prev; | |
| 439 return; | |
| 440 } | |
| 441 while (curr != NULL) { | |
| 442 if (curr->callback_ == callback) { | |
| 443 prev->next_ = curr->next_; | |
| 444 delete curr; | |
| 445 return; | |
| 446 } | |
| 447 prev = curr; | |
| 448 curr = curr->next_; | |
| 449 } | |
| 450 } | |
| 451 | |
| 452 // Iterates through all of the callbacks in the list and invokes | |
| 453 // their callback methods. | |
| 454 void Invoke() const { | |
| 455 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 456 (*curr->callback_)(); | |
| 457 } | |
| 458 } | |
| 459 | |
| 460 // Returns the number of callbacks stored in the list. | |
| 461 intptr_t Count() const { | |
| 462 intptr_t sum = 0; | |
| 463 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 464 ++sum; | |
| 465 } | |
| 466 return sum; | |
| 467 } | |
| 468 | |
| 469 // Returns true if the specified callback has is present in the | |
|
turnidge
2012/03/07 00:25:16
Extra word "has"
| |
| 470 // container. | |
| 471 bool Contains(T callback) const { | |
| 472 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 473 if (curr->callback_ == callback) { | |
| 474 return true; | |
| 475 } | |
| 476 } | |
| 477 return false; | |
| 478 } | |
| 479 | |
| 480 private: | |
| 481 // A linked-list element. | |
| 482 struct Link { | |
| 483 Link(T callback, Link* next) : callback_(callback), next_(next) { | |
| 484 } | |
| 485 T callback_; | |
| 486 Link* next_; | |
| 487 }; | |
| 488 | |
| 489 Link* head_; | |
| 490 }; | |
| 491 | |
| 492 | |
| 493 // A container for storing garbage collection prologue methods. | |
| 494 class GcPrologueCallbacks : public GcCallbacks<Dart_GcPrologueCallback> {}; | |
| 495 | |
| 496 | |
| 497 // A container for storing garbage collection epilogue methods. | |
| 498 class GcEpilogueCallbacks : public GcCallbacks<Dart_GcEpilogueCallback> {}; | |
| 499 | |
| 500 | |
| 404 // Structure used for the implementation of local scopes used in dart_api. | 501 // Structure used for the implementation of local scopes used in dart_api. |
| 405 // These local scopes manage handles and memory allocated in the scope. | 502 // These local scopes manage handles and memory allocated in the scope. |
| 406 class ApiLocalScope { | 503 class ApiLocalScope { |
| 407 public: | 504 public: |
| 408 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : | 505 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : |
| 409 previous_(previous), stack_marker_(stack_marker) { } | 506 previous_(previous), stack_marker_(stack_marker) { } |
| 410 ~ApiLocalScope() { | 507 ~ApiLocalScope() { |
| 411 previous_ = NULL; | 508 previous_ = NULL; |
| 412 } | 509 } |
| 413 | 510 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 451 } | 548 } |
| 452 if (false_ != NULL) { | 549 if (false_ != NULL) { |
| 453 persistent_handles().FreeHandle(false_); | 550 persistent_handles().FreeHandle(false_); |
| 454 false_ = NULL; | 551 false_ = NULL; |
| 455 } | 552 } |
| 456 } | 553 } |
| 457 | 554 |
| 458 // Accessors. | 555 // Accessors. |
| 459 ApiLocalScope* top_scope() const { return top_scope_; } | 556 ApiLocalScope* top_scope() const { return top_scope_; } |
| 460 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } | 557 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } |
| 558 | |
| 461 PersistentHandles& persistent_handles() { return persistent_handles_; } | 559 PersistentHandles& persistent_handles() { return persistent_handles_; } |
| 462 WeakPersistentHandles& weak_persistent_handles() { | 560 WeakPersistentHandles& weak_persistent_handles() { |
| 463 return weak_persistent_handles_; | 561 return weak_persistent_handles_; |
| 464 } | 562 } |
| 563 | |
| 465 WeakReference* delayed_weak_references() { return delayed_weak_references_; } | 564 WeakReference* delayed_weak_references() { return delayed_weak_references_; } |
| 466 void set_delayed_weak_references(WeakReference* reference) { | 565 void set_delayed_weak_references(WeakReference* reference) { |
| 467 delayed_weak_references_ = reference; | 566 delayed_weak_references_ = reference; |
| 468 } | 567 } |
| 568 | |
| 569 GcPrologueCallbacks& gc_prologue_callbacks() { | |
| 570 return gc_prologue_callbacks_; | |
| 571 }; | |
| 572 | |
| 573 GcEpilogueCallbacks& gc_epilogue_callbacks() { | |
| 574 return gc_epilogue_callbacks_; | |
| 575 }; | |
| 576 | |
| 469 void UnwindScopes(uword sp) { | 577 void UnwindScopes(uword sp) { |
| 470 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { | 578 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { |
| 471 ApiLocalScope* scope = top_scope_; | 579 ApiLocalScope* scope = top_scope_; |
| 472 top_scope_ = top_scope_->previous(); | 580 top_scope_ = top_scope_->previous(); |
| 473 delete scope; | 581 delete scope; |
| 474 } | 582 } |
| 475 } | 583 } |
| 476 | 584 |
| 477 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 585 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 478 ApiLocalScope* scope = top_scope_; | 586 ApiLocalScope* scope = top_scope_; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 566 void DelayWeakReference(WeakReference* reference) { | 674 void DelayWeakReference(WeakReference* reference) { |
| 567 WeakReference::Push(reference, &delayed_weak_references_); | 675 WeakReference::Push(reference, &delayed_weak_references_); |
| 568 } | 676 } |
| 569 | 677 |
| 570 private: | 678 private: |
| 571 PersistentHandles persistent_handles_; | 679 PersistentHandles persistent_handles_; |
| 572 WeakPersistentHandles weak_persistent_handles_; | 680 WeakPersistentHandles weak_persistent_handles_; |
| 573 ApiLocalScope* top_scope_; | 681 ApiLocalScope* top_scope_; |
| 574 WeakReference* delayed_weak_references_; | 682 WeakReference* delayed_weak_references_; |
| 575 | 683 |
| 684 // | |
|
turnidge
2012/03/07 00:25:16
<empty comment>
| |
| 685 GcPrologueCallbacks gc_prologue_callbacks_; | |
| 686 GcEpilogueCallbacks gc_epilogue_callbacks_; | |
| 687 | |
| 576 // Persistent handles to important objects. | 688 // Persistent handles to important objects. |
| 577 PersistentHandle* null_; | 689 PersistentHandle* null_; |
| 578 PersistentHandle* true_; | 690 PersistentHandle* true_; |
| 579 PersistentHandle* false_; | 691 PersistentHandle* false_; |
| 580 | 692 |
| 581 DISALLOW_COPY_AND_ASSIGN(ApiState); | 693 DISALLOW_COPY_AND_ASSIGN(ApiState); |
| 582 }; | 694 }; |
| 583 | 695 |
| 584 | 696 |
| 585 class ApiNativeScope { | 697 class ApiNativeScope { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 ApiNativeScope::Current()->zone()->GetBaseZone()) {} | 733 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
| 622 ApiGrowableArray() | 734 ApiGrowableArray() |
| 623 : BaseGrowableArray<T, ValueObject>( | 735 : BaseGrowableArray<T, ValueObject>( |
| 624 ApiNativeScope::Current()->zone()->GetBaseZone()) {} | 736 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
| 625 }; | 737 }; |
| 626 | 738 |
| 627 | 739 |
| 628 } // namespace dart | 740 } // namespace dart |
| 629 | 741 |
| 630 #endif // VM_DART_API_STATE_H_ | 742 #endif // VM_DART_API_STATE_H_ |
| OLD | NEW |