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 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 PersistentHandle* Next() { | 164 PersistentHandle* Next() { |
165 return reinterpret_cast<PersistentHandle*>(raw_); | 165 return reinterpret_cast<PersistentHandle*>(raw_); |
166 } | 166 } |
167 void SetNext(PersistentHandle* free_list) { | 167 void SetNext(PersistentHandle* free_list) { |
168 raw_ = reinterpret_cast<RawObject*>(free_list); | 168 raw_ = reinterpret_cast<RawObject*>(free_list); |
169 ASSERT(!raw_->IsHeapObject()); | 169 ASSERT(!raw_->IsHeapObject()); |
170 } | 170 } |
171 void FreeHandle(PersistentHandle* free_list) { | 171 void FreeHandle(PersistentHandle* free_list) { |
172 SetNext(free_list); | 172 SetNext(free_list); |
173 } | 173 } |
174 | 174 |
Ivan Posva
2014/03/03 21:00:19
external and Cast might need to be added here.
siva
2014/03/04 00:24:48
Done.
| |
175 RawObject* raw_; | 175 RawObject* raw_; |
176 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. | 176 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. |
177 DISALLOW_COPY_AND_ASSIGN(PersistentHandle); | 177 DISALLOW_COPY_AND_ASSIGN(PersistentHandle); |
178 }; | 178 }; |
179 | 179 |
180 | 180 |
181 // Implementation of persistent handles which are handed out through the | 181 // Implementation of persistent handles which are handed out through the |
182 // dart API. | 182 // dart API. |
183 class FinalizablePersistentHandle { | 183 class FinalizablePersistentHandle { |
184 public: | 184 public: |
185 // Accessors. | 185 // Accessors. |
186 RawObject* raw() const { return raw_; } | 186 RawObject* raw() const { return raw_; } |
187 void set_raw(RawObject* raw) { raw_ = raw; } | 187 void set_raw(RawObject* raw) { raw_ = raw; } |
188 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } | 188 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } |
189 void set_raw(const Object& object) { raw_ = object.raw(); } | 189 void set_raw(const Object& object) { raw_ = object.raw(); } |
190 RawObject** raw_addr() { return &raw_; } | 190 RawObject** raw_addr() { return &raw_; } |
191 static intptr_t raw_offset() { | 191 static intptr_t raw_offset() { |
192 return OFFSET_OF(FinalizablePersistentHandle, raw_); | 192 return OFFSET_OF(FinalizablePersistentHandle, raw_); |
193 } | 193 } |
194 void* peer() const { return peer_; } | 194 void* peer() const { return peer_; } |
195 void set_peer(void* peer) { peer_ = peer; } | 195 void set_peer(void* peer) { peer_ = peer; } |
196 Dart_WeakPersistentHandleFinalizer callback() const { return callback_; } | 196 Dart_WeakPersistentHandleFinalizer callback() const { return callback_; } |
197 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { | 197 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { |
198 callback_ = callback; | 198 callback_ = callback; |
199 } | 199 } |
200 | 200 static bool IsPrologueWeakPersistentHandle(Dart_WeakPersistentHandle handle) { |
201 static void Finalize(FinalizablePersistentHandle* handle) { | 201 uword addr = reinterpret_cast<uword>(handle); |
202 return (addr & kWeakPersistentTagMask) == kPrologueWeakPersistentTag; | |
203 } | |
204 static Dart_WeakPersistentHandle AsDartPrologueWeakPersistentHandle( | |
Ivan Posva
2014/03/03 21:00:19
Make this a method such as external on Finalizable
siva
2014/03/04 00:24:48
Done.
| |
205 FinalizablePersistentHandle* handle) { | |
206 uword addr = reinterpret_cast<uword>(handle); | |
207 return reinterpret_cast<Dart_WeakPersistentHandle>( | |
208 addr | kPrologueWeakPersistentTag); | |
209 } | |
210 static Dart_WeakPersistentHandle AsDartWeakPersistentHandle( | |
211 FinalizablePersistentHandle* handle) { | |
212 return reinterpret_cast<Dart_WeakPersistentHandle>(handle); | |
213 } | |
214 static FinalizablePersistentHandle* AsFinalizablePersistentHandle( | |
Ivan Posva
2014/03/03 21:00:19
How about renaming this as FinalizablePersistentHa
siva
2014/03/04 00:24:48
Done.
| |
215 Dart_WeakPersistentHandle handle) { | |
216 uword addr = reinterpret_cast<uword>(handle); | |
217 return reinterpret_cast<FinalizablePersistentHandle*>( | |
218 addr & ~kWeakPersistentTagMask); | |
219 } | |
220 static void Finalize(FinalizablePersistentHandle* handle, | |
221 bool is_prologue_weak) { | |
202 Dart_WeakPersistentHandleFinalizer callback = handle->callback(); | 222 Dart_WeakPersistentHandleFinalizer callback = handle->callback(); |
203 if (callback != NULL) { | 223 if (callback != NULL) { |
204 void* peer = handle->peer(); | 224 void* peer = handle->peer(); |
205 handle->Clear(); | 225 handle->Clear(); |
206 (*callback)(reinterpret_cast<Dart_WeakPersistentHandle>(handle), peer); | 226 Dart_WeakPersistentHandle object = |
227 is_prologue_weak ? AsDartPrologueWeakPersistentHandle(handle) : | |
228 AsDartWeakPersistentHandle(handle); | |
229 (*callback)(object, peer); | |
207 } else { | 230 } else { |
208 handle->Clear(); | 231 handle->Clear(); |
209 } | 232 } |
210 } | 233 } |
211 | 234 |
212 private: | 235 private: |
236 enum { | |
237 kWeakPersistentTag = 0, | |
238 kPrologueWeakPersistentTag = 1, | |
239 kWeakPersistentTagSize = 1, | |
240 kWeakPersistentTagMask = 1, | |
241 }; | |
242 | |
213 friend class FinalizablePersistentHandles; | 243 friend class FinalizablePersistentHandles; |
214 | 244 |
215 FinalizablePersistentHandle() : raw_(NULL), peer_(NULL), callback_(NULL) { } | 245 FinalizablePersistentHandle() : raw_(NULL), peer_(NULL), callback_(NULL) { } |
216 ~FinalizablePersistentHandle() { } | 246 ~FinalizablePersistentHandle() { } |
217 | 247 |
218 // Overload the raw_ field as a next pointer when adding freed | 248 // Overload the raw_ field as a next pointer when adding freed |
219 // handles to the free list. | 249 // handles to the free list. |
220 FinalizablePersistentHandle* Next() { | 250 FinalizablePersistentHandle* Next() { |
221 return reinterpret_cast<FinalizablePersistentHandle*>(raw_); | 251 return reinterpret_cast<FinalizablePersistentHandle*>(raw_); |
222 } | 252 } |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; | 426 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; |
397 class FinalizablePersistentHandles | 427 class FinalizablePersistentHandles |
398 : Handles<kFinalizablePersistentHandleSizeInWords, | 428 : Handles<kFinalizablePersistentHandleSizeInWords, |
399 kFinalizablePersistentHandlesPerChunk, | 429 kFinalizablePersistentHandlesPerChunk, |
400 kOffsetOfRawPtrInFinalizablePersistentHandle> { | 430 kOffsetOfRawPtrInFinalizablePersistentHandle> { |
401 public: | 431 public: |
402 FinalizablePersistentHandles() | 432 FinalizablePersistentHandles() |
403 : Handles<kFinalizablePersistentHandleSizeInWords, | 433 : Handles<kFinalizablePersistentHandleSizeInWords, |
404 kFinalizablePersistentHandlesPerChunk, | 434 kFinalizablePersistentHandlesPerChunk, |
405 kOffsetOfRawPtrInFinalizablePersistentHandle>(), | 435 kOffsetOfRawPtrInFinalizablePersistentHandle>(), |
406 free_list_(NULL) { } | 436 free_list_(NULL) { } |
407 ~FinalizablePersistentHandles() { | 437 ~FinalizablePersistentHandles() { |
408 free_list_ = NULL; | 438 free_list_ = NULL; |
409 } | 439 } |
410 | 440 |
411 // Accessors. | 441 // Accessors. |
412 FinalizablePersistentHandle* free_list() const { return free_list_; } | 442 FinalizablePersistentHandle* free_list() const { return free_list_; } |
413 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } | 443 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } |
414 | 444 |
415 // Visit all handles stored in the various handle blocks. | 445 // Visit all handles stored in the various handle blocks. |
416 void VisitHandles(HandleVisitor* visitor) { | 446 void VisitHandles(HandleVisitor* visitor, bool is_prologue_weak) { |
417 Handles<kFinalizablePersistentHandleSizeInWords, | 447 Handles<kFinalizablePersistentHandleSizeInWords, |
418 kFinalizablePersistentHandlesPerChunk, | 448 kFinalizablePersistentHandlesPerChunk, |
419 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit(visitor); | 449 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit( |
450 visitor, is_prologue_weak); | |
420 } | 451 } |
421 | 452 |
422 // Visit all object pointers stored in the various handles. | 453 // Visit all object pointers stored in the various handles. |
423 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 454 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
424 Handles<kFinalizablePersistentHandleSizeInWords, | 455 Handles<kFinalizablePersistentHandleSizeInWords, |
425 kFinalizablePersistentHandlesPerChunk, | 456 kFinalizablePersistentHandlesPerChunk, |
426 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( | 457 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( |
427 visitor); | 458 visitor); |
428 } | 459 } |
429 | 460 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 values_(values), num_values_(values_length) { | 503 values_(values), num_values_(values_length) { |
473 } | 504 } |
474 ~WeakReferenceSet() {} | 505 ~WeakReferenceSet() {} |
475 | 506 |
476 WeakReferenceSet* next() const { return next_; } | 507 WeakReferenceSet* next() const { return next_; } |
477 | 508 |
478 intptr_t num_keys() const { return num_keys_; } | 509 intptr_t num_keys() const { return num_keys_; } |
479 RawObject** get_key(intptr_t i) { | 510 RawObject** get_key(intptr_t i) { |
480 ASSERT(i >= 0); | 511 ASSERT(i >= 0); |
481 ASSERT(i < num_keys_); | 512 ASSERT(i < num_keys_); |
482 return (reinterpret_cast<FinalizablePersistentHandle*>(keys_[i]))-> | 513 FinalizablePersistentHandle* ref = |
483 raw_addr(); | 514 FinalizablePersistentHandle::AsFinalizablePersistentHandle(keys_[i]); |
515 return ref->raw_addr(); | |
484 } | 516 } |
485 | 517 |
486 intptr_t num_values() const { return num_values_; } | 518 intptr_t num_values() const { return num_values_; } |
487 RawObject** get_value(intptr_t i) { | 519 RawObject** get_value(intptr_t i) { |
488 ASSERT(i >= 0); | 520 ASSERT(i >= 0); |
489 ASSERT(i < num_values_); | 521 ASSERT(i < num_values_); |
490 return (reinterpret_cast<FinalizablePersistentHandle*>(values_[i]))-> | 522 FinalizablePersistentHandle* ref = |
491 raw_addr(); | 523 FinalizablePersistentHandle::AsFinalizablePersistentHandle(values_[i]); |
524 return ref->raw_addr(); | |
492 } | 525 } |
493 | 526 |
494 static WeakReferenceSet* Pop(WeakReferenceSet** queue) { | 527 static WeakReferenceSet* Pop(WeakReferenceSet** queue) { |
495 ASSERT(queue != NULL); | 528 ASSERT(queue != NULL); |
496 WeakReferenceSet* head = *queue; | 529 WeakReferenceSet* head = *queue; |
497 if (head != NULL) { | 530 if (head != NULL) { |
498 *queue = head->next(); | 531 *queue = head->next(); |
499 head->next_ = NULL; | 532 head->next_ = NULL; |
500 } | 533 } |
501 return head; | 534 return head; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
645 scope = scope->previous(); | 678 scope = scope->previous(); |
646 } | 679 } |
647 persistent_handles().VisitObjectPointers(visitor); | 680 persistent_handles().VisitObjectPointers(visitor); |
648 if (visit_prologue_weak_handles) { | 681 if (visit_prologue_weak_handles) { |
649 prologue_weak_persistent_handles().VisitObjectPointers(visitor); | 682 prologue_weak_persistent_handles().VisitObjectPointers(visitor); |
650 } | 683 } |
651 } | 684 } |
652 | 685 |
653 void VisitWeakHandles(HandleVisitor* visitor, | 686 void VisitWeakHandles(HandleVisitor* visitor, |
654 bool visit_prologue_weak_handles) { | 687 bool visit_prologue_weak_handles) { |
655 weak_persistent_handles().VisitHandles(visitor); | 688 weak_persistent_handles().VisitHandles(visitor, false); |
656 if (visit_prologue_weak_handles) { | 689 if (visit_prologue_weak_handles) { |
657 prologue_weak_persistent_handles().VisitHandles(visitor); | 690 prologue_weak_persistent_handles().VisitHandles(visitor, true); |
658 } | 691 } |
659 } | 692 } |
660 | 693 |
661 bool IsValidLocalHandle(Dart_Handle object) const { | 694 bool IsValidLocalHandle(Dart_Handle object) const { |
662 ApiLocalScope* scope = top_scope_; | 695 ApiLocalScope* scope = top_scope_; |
663 while (scope != NULL) { | 696 while (scope != NULL) { |
664 if (scope->local_handles()->IsValidHandle(object)) { | 697 if (scope->local_handles()->IsValidHandle(object)) { |
665 return true; | 698 return true; |
666 } | 699 } |
667 scope = scope->previous(); | 700 scope = scope->previous(); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
782 ApiNativeScope::Current()->zone()) {} | 815 ApiNativeScope::Current()->zone()) {} |
783 ApiGrowableArray() | 816 ApiGrowableArray() |
784 : BaseGrowableArray<T, ValueObject>( | 817 : BaseGrowableArray<T, ValueObject>( |
785 ApiNativeScope::Current()->zone()) {} | 818 ApiNativeScope::Current()->zone()) {} |
786 }; | 819 }; |
787 | 820 |
788 | 821 |
789 } // namespace dart | 822 } // namespace dart |
790 | 823 |
791 #endif // VM_DART_API_STATE_H_ | 824 #endif // VM_DART_API_STATE_H_ |
OLD | NEW |