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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 OS::PrintErr("*** Starting a new Api zone 0x%"Px"(0x%"Px")\n", | 43 OS::PrintErr("*** Starting a new Api zone 0x%"Px"(0x%"Px")\n", |
44 reinterpret_cast<intptr_t>(this), | 44 reinterpret_cast<intptr_t>(this), |
45 reinterpret_cast<intptr_t>(&zone_)); | 45 reinterpret_cast<intptr_t>(&zone_)); |
46 } | 46 } |
47 #endif | 47 #endif |
48 } | 48 } |
49 | 49 |
50 // Delete all memory associated with the zone. | 50 // Delete all memory associated with the zone. |
51 ~ApiZone() { | 51 ~ApiZone() { |
52 Isolate* isolate = Isolate::Current(); | 52 Isolate* isolate = Isolate::Current(); |
53 if (isolate != NULL && isolate->current_zone() == &zone_) { | 53 if ((isolate != NULL) && (isolate->current_zone() == &zone_)) { |
54 isolate->set_current_zone(zone_.previous_); | 54 isolate->set_current_zone(zone_.previous_); |
55 } | 55 } |
56 #ifdef DEBUG | 56 #ifdef DEBUG |
57 if (FLAG_trace_zones) { | 57 if (FLAG_trace_zones) { |
58 OS::PrintErr("*** Deleting Api zone 0x%"Px"(0x%"Px")\n", | 58 OS::PrintErr("*** Deleting Api zone 0x%"Px"(0x%"Px")\n", |
59 reinterpret_cast<intptr_t>(this), | 59 reinterpret_cast<intptr_t>(this), |
60 reinterpret_cast<intptr_t>(&zone_)); | 60 reinterpret_cast<intptr_t>(&zone_)); |
61 } | 61 } |
62 #endif | 62 #endif |
63 } | 63 } |
(...skipping 21 matching lines...) Expand all Loading... |
85 // check for integer overflow. If you use AllocUnsafe, you are | 85 // check for integer overflow. If you use AllocUnsafe, you are |
86 // responsible for avoiding integer overflow yourself. | 86 // responsible for avoiding integer overflow yourself. |
87 uword AllocUnsafe(intptr_t size) { return zone_.AllocUnsafe(size); } | 87 uword AllocUnsafe(intptr_t size) { return zone_.AllocUnsafe(size); } |
88 | 88 |
89 // Compute the total size of this zone. This includes wasted space that is | 89 // Compute the total size of this zone. This includes wasted space that is |
90 // due to internal fragmentation in the segments. | 90 // due to internal fragmentation in the segments. |
91 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 91 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
92 | 92 |
93 Zone* GetZone() { return &zone_; } | 93 Zone* GetZone() { return &zone_; } |
94 | 94 |
| 95 void Reinit(Isolate* isolate) { |
| 96 if (isolate == NULL) { |
| 97 zone_.Link(NULL); |
| 98 } else { |
| 99 zone_.Link(isolate->current_zone()); |
| 100 isolate->set_current_zone(&zone_); |
| 101 } |
| 102 } |
| 103 |
| 104 void Reset(Isolate* isolate) { |
| 105 if ((isolate != NULL) && (isolate->current_zone() == &zone_)) { |
| 106 isolate->set_current_zone(zone_.previous_); |
| 107 } |
| 108 zone_.DeleteAll(); |
| 109 } |
| 110 |
95 private: | 111 private: |
96 Zone zone_; | 112 Zone zone_; |
97 | 113 |
98 template<typename T> friend class ApiGrowableArray; | 114 template<typename T> friend class ApiGrowableArray; |
99 DISALLOW_COPY_AND_ASSIGN(ApiZone); | 115 DISALLOW_COPY_AND_ASSIGN(ApiZone); |
100 }; | 116 }; |
101 | 117 |
102 | 118 |
103 // Implementation of local handles which are handed out from every | 119 // Implementation of local handles which are handed out from every |
104 // dart API call, these handles are valid only in the present scope | 120 // dart API call, these handles are valid only in the present scope |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 } | 274 } |
259 | 275 |
260 | 276 |
261 // Visit all object pointers stored in the various handles. | 277 // Visit all object pointers stored in the various handles. |
262 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 278 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
263 Handles<kLocalHandleSizeInWords, | 279 Handles<kLocalHandleSizeInWords, |
264 kLocalHandlesPerChunk, | 280 kLocalHandlesPerChunk, |
265 kOffsetOfRawPtrInLocalHandle>::VisitObjectPointers(visitor); | 281 kOffsetOfRawPtrInLocalHandle>::VisitObjectPointers(visitor); |
266 } | 282 } |
267 | 283 |
| 284 // Reset the local handles block for reuse. |
| 285 void Reset() { |
| 286 Handles<kLocalHandleSizeInWords, |
| 287 kLocalHandlesPerChunk, |
| 288 kOffsetOfRawPtrInLocalHandle>::Reset(); |
| 289 } |
| 290 |
268 // Allocates a handle in the current handle scope. This handle is valid only | 291 // Allocates a handle in the current handle scope. This handle is valid only |
269 // in the current handle scope and is destroyed when the current handle | 292 // in the current handle scope and is destroyed when the current handle |
270 // scope ends. | 293 // scope ends. |
271 LocalHandle* AllocateHandle() { | 294 LocalHandle* AllocateHandle() { |
272 return reinterpret_cast<LocalHandle*>(AllocateScopedHandle()); | 295 return reinterpret_cast<LocalHandle*>(AllocateScopedHandle()); |
273 } | 296 } |
274 | 297 |
275 // Validate if passed in handle is a Local Handle. | 298 // Validate if passed in handle is a Local Handle. |
276 bool IsValidHandle(Dart_Handle object) const { | 299 bool IsValidHandle(Dart_Handle object) const { |
277 return IsValidScopedHandle(reinterpret_cast<uword>(object)); | 300 return IsValidScopedHandle(reinterpret_cast<uword>(object)); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 // Structure used for the implementation of local scopes used in dart_api. | 521 // Structure used for the implementation of local scopes used in dart_api. |
499 // These local scopes manage handles and memory allocated in the scope. | 522 // These local scopes manage handles and memory allocated in the scope. |
500 class ApiLocalScope { | 523 class ApiLocalScope { |
501 public: | 524 public: |
502 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : | 525 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : |
503 previous_(previous), stack_marker_(stack_marker) { } | 526 previous_(previous), stack_marker_(stack_marker) { } |
504 ~ApiLocalScope() { | 527 ~ApiLocalScope() { |
505 previous_ = NULL; | 528 previous_ = NULL; |
506 } | 529 } |
507 | 530 |
| 531 // Reinit the ApiLocalScope to new values. |
| 532 void Reinit(Isolate* isolate, ApiLocalScope* previous, uword stack_marker) { |
| 533 previous_ = previous; |
| 534 stack_marker_ = stack_marker; |
| 535 zone_.Reinit(isolate); |
| 536 } |
| 537 |
| 538 // Reset the ApiLocalScope so that it can be reused again. |
| 539 void Reset(Isolate* isolate) { |
| 540 local_handles_.Reset(); |
| 541 zone_.Reset(isolate); |
| 542 previous_ = NULL; |
| 543 stack_marker_ = 0; |
| 544 } |
| 545 |
508 // Accessors. | 546 // Accessors. |
509 ApiLocalScope* previous() const { return previous_; } | 547 ApiLocalScope* previous() const { return previous_; } |
510 uword stack_marker() const { return stack_marker_; } | 548 uword stack_marker() const { return stack_marker_; } |
511 void set_previous(ApiLocalScope* value) { previous_ = value; } | 549 void set_previous(ApiLocalScope* value) { previous_ = value; } |
512 LocalHandles* local_handles() { return &local_handles_; } | 550 LocalHandles* local_handles() { return &local_handles_; } |
513 Zone* zone() { return zone_.GetZone(); } | 551 Zone* zone() { return zone_.GetZone(); } |
514 | 552 |
515 private: | 553 private: |
516 ApiLocalScope* previous_; | 554 ApiLocalScope* previous_; |
517 uword stack_marker_; | 555 uword stack_marker_; |
518 LocalHandles local_handles_; | 556 LocalHandles local_handles_; |
519 ApiZone zone_; | 557 ApiZone zone_; |
520 | 558 |
521 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); | 559 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); |
522 }; | 560 }; |
523 | 561 |
524 | 562 |
525 // Implementation of the API State used in dart api for maintaining | 563 // Implementation of the API State used in dart api for maintaining |
526 // local scopes, persistent handles etc. These are setup on a per isolate | 564 // local scopes, persistent handles etc. These are setup on a per isolate |
527 // basis and destroyed when the isolate is shutdown. | 565 // basis and destroyed when the isolate is shutdown. |
528 class ApiState { | 566 class ApiState { |
529 public: | 567 public: |
530 ApiState() : top_scope_(NULL), delayed_weak_reference_sets_(NULL), | 568 ApiState() : persistent_handles_(), |
531 null_(NULL), true_(NULL), false_(NULL), | 569 weak_persistent_handles_(), |
| 570 prologue_weak_persistent_handles_(), |
| 571 reusable_scope_(NULL), |
| 572 top_scope_(NULL), |
| 573 delayed_weak_reference_sets_(NULL), |
| 574 null_(NULL), |
| 575 true_(NULL), |
| 576 false_(NULL), |
532 acquired_error_(NULL) {} | 577 acquired_error_(NULL) {} |
533 ~ApiState() { | 578 ~ApiState() { |
534 while (top_scope_ != NULL) { | 579 while (top_scope_ != NULL) { |
535 ApiLocalScope* scope = top_scope_; | 580 ApiLocalScope* scope = top_scope_; |
536 top_scope_ = top_scope_->previous(); | 581 top_scope_ = top_scope_->previous(); |
537 delete scope; | 582 delete scope; |
538 } | 583 } |
539 if (null_ != NULL) { | 584 if (null_ != NULL) { |
540 persistent_handles().FreeHandle(null_); | 585 persistent_handles().FreeHandle(null_); |
541 null_ = NULL; | 586 null_ = NULL; |
542 } | 587 } |
543 if (true_ != NULL) { | 588 if (true_ != NULL) { |
544 persistent_handles().FreeHandle(true_); | 589 persistent_handles().FreeHandle(true_); |
545 true_ = NULL; | 590 true_ = NULL; |
546 } | 591 } |
547 if (false_ != NULL) { | 592 if (false_ != NULL) { |
548 persistent_handles().FreeHandle(false_); | 593 persistent_handles().FreeHandle(false_); |
549 false_ = NULL; | 594 false_ = NULL; |
550 } | 595 } |
551 if (acquired_error_ != NULL) { | 596 if (acquired_error_ != NULL) { |
552 persistent_handles().FreeHandle(acquired_error_); | 597 persistent_handles().FreeHandle(acquired_error_); |
553 acquired_error_ = NULL; | 598 acquired_error_ = NULL; |
554 } | 599 } |
555 } | 600 } |
556 | 601 |
557 // Accessors. | 602 // Accessors. |
| 603 ApiLocalScope* reusable_scope() const { return reusable_scope_; } |
| 604 void set_reusable_scope(ApiLocalScope* value) { |
| 605 ASSERT(value == NULL || reusable_scope_ == NULL); |
| 606 reusable_scope_ = value; |
| 607 } |
558 ApiLocalScope* top_scope() const { return top_scope_; } | 608 ApiLocalScope* top_scope() const { return top_scope_; } |
559 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } | 609 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } |
560 | 610 |
561 PersistentHandles& persistent_handles() { return persistent_handles_; } | 611 PersistentHandles& persistent_handles() { return persistent_handles_; } |
562 | 612 |
563 FinalizablePersistentHandles& weak_persistent_handles() { | 613 FinalizablePersistentHandles& weak_persistent_handles() { |
564 return weak_persistent_handles_; | 614 return weak_persistent_handles_; |
565 } | 615 } |
566 | 616 |
567 FinalizablePersistentHandles& prologue_weak_persistent_handles() { | 617 FinalizablePersistentHandles& prologue_weak_persistent_handles() { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 } | 723 } |
674 | 724 |
675 void DelayWeakReferenceSet(WeakReferenceSet* reference_set) { | 725 void DelayWeakReferenceSet(WeakReferenceSet* reference_set) { |
676 WeakReferenceSet::Push(reference_set, &delayed_weak_reference_sets_); | 726 WeakReferenceSet::Push(reference_set, &delayed_weak_reference_sets_); |
677 } | 727 } |
678 | 728 |
679 private: | 729 private: |
680 PersistentHandles persistent_handles_; | 730 PersistentHandles persistent_handles_; |
681 FinalizablePersistentHandles weak_persistent_handles_; | 731 FinalizablePersistentHandles weak_persistent_handles_; |
682 FinalizablePersistentHandles prologue_weak_persistent_handles_; | 732 FinalizablePersistentHandles prologue_weak_persistent_handles_; |
| 733 ApiLocalScope* reusable_scope_; |
683 ApiLocalScope* top_scope_; | 734 ApiLocalScope* top_scope_; |
684 WeakReferenceSet* delayed_weak_reference_sets_; | 735 WeakReferenceSet* delayed_weak_reference_sets_; |
685 | 736 |
686 // Persistent handles to important objects. | 737 // Persistent handles to important objects. |
687 PersistentHandle* null_; | 738 PersistentHandle* null_; |
688 PersistentHandle* true_; | 739 PersistentHandle* true_; |
689 PersistentHandle* false_; | 740 PersistentHandle* false_; |
690 PersistentHandle* acquired_error_; | 741 PersistentHandle* acquired_error_; |
691 | 742 |
692 DISALLOW_COPY_AND_ASSIGN(ApiState); | 743 DISALLOW_COPY_AND_ASSIGN(ApiState); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 ApiNativeScope::Current()->zone()) {} | 782 ApiNativeScope::Current()->zone()) {} |
732 ApiGrowableArray() | 783 ApiGrowableArray() |
733 : BaseGrowableArray<T, ValueObject>( | 784 : BaseGrowableArray<T, ValueObject>( |
734 ApiNativeScope::Current()->zone()) {} | 785 ApiNativeScope::Current()->zone()) {} |
735 }; | 786 }; |
736 | 787 |
737 | 788 |
738 } // namespace dart | 789 } // namespace dart |
739 | 790 |
740 #endif // VM_DART_API_STATE_H_ | 791 #endif // VM_DART_API_STATE_H_ |
OLD | NEW |