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/utils.h" | 10 #include "platform/utils.h" |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 void SetExternalSize(intptr_t size, Isolate* isolate) { | 220 void SetExternalSize(intptr_t size, Isolate* isolate) { |
221 ASSERT(size >= 0); | 221 ASSERT(size >= 0); |
222 set_external_size(Utils::RoundUp(size, kObjectAlignment)); | 222 set_external_size(Utils::RoundUp(size, kObjectAlignment)); |
223 if (SpaceForExternal() == Heap::kNew) { | 223 if (SpaceForExternal() == Heap::kNew) { |
224 SetExternalNewSpaceBit(); | 224 SetExternalNewSpaceBit(); |
225 } | 225 } |
226 isolate->heap()->AllocateExternal(external_size(), SpaceForExternal()); | 226 isolate->heap()->AllocateExternal(external_size(), SpaceForExternal()); |
227 } | 227 } |
228 | 228 |
229 // Called when the referent becomes unreachable. | 229 // Called when the referent becomes unreachable. |
230 void UpdateUnreachable(Isolate* isolate) { | 230 void UpdateUnreachableFinalizeNow(Isolate* isolate) { |
231 EnsureFreeExternal(isolate); | 231 EnsureFreeExternal(isolate); |
232 Finalize(isolate, this); | 232 Finalize(isolate, this); |
233 } | 233 } |
234 | 234 |
235 void UpdateUnreachableFinalizeLater(Isolate* isolate) { | |
236 EnsureFreeExternal(isolate); | |
237 MarkForFinalization(); | |
238 } | |
siva
2016/06/02 20:18:05
Maybe more readable as
void UpdateUnreachable(Iso
rmacnak
2016/06/03 01:08:30
Yes, I like that better.
| |
239 | |
235 // Called when the referent has moved, potentially between generations. | 240 // Called when the referent has moved, potentially between generations. |
236 void UpdateRelocated(Isolate* isolate) { | 241 void UpdateRelocated(Isolate* isolate) { |
237 if (IsSetNewSpaceBit() && (SpaceForExternal() == Heap::kOld)) { | 242 if (IsSetNewSpaceBit() && (SpaceForExternal() == Heap::kOld)) { |
238 isolate->heap()->PromoteExternal(external_size()); | 243 isolate->heap()->PromoteExternal(external_size()); |
239 ClearExternalNewSpaceBit(); | 244 ClearExternalNewSpaceBit(); |
240 } | 245 } |
241 } | 246 } |
242 | 247 |
243 // Idempotent. Called when the handle is explicitly deleted or the | 248 // Idempotent. Called when the handle is explicitly deleted or the |
244 // referent becomes unreachable. | 249 // referent becomes unreachable. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 SetNext(free_list); | 297 SetNext(free_list); |
293 } | 298 } |
294 | 299 |
295 void Clear() { | 300 void Clear() { |
296 raw_ = Object::null(); | 301 raw_ = Object::null(); |
297 peer_ = NULL; | 302 peer_ = NULL; |
298 external_data_ = 0; | 303 external_data_ = 0; |
299 callback_ = NULL; | 304 callback_ = NULL; |
300 } | 305 } |
301 | 306 |
307 void MarkForFinalization() { | |
308 raw_ = Object::null(); | |
309 ASSERT(callback_ != NULL); | |
310 } | |
311 | |
302 void set_raw(RawObject* raw) { raw_ = raw; } | 312 void set_raw(RawObject* raw) { raw_ = raw; } |
303 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } | 313 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } |
304 void set_raw(const Object& object) { raw_ = object.raw(); } | 314 void set_raw(const Object& object) { raw_ = object.raw(); } |
305 | 315 |
306 void set_peer(void* peer) { peer_ = peer; } | 316 void set_peer(void* peer) { peer_ = peer; } |
307 | 317 |
308 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { | 318 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { |
309 callback_ = callback; | 319 callback_ = callback; |
310 } | 320 } |
311 | 321 |
(...skipping 14 matching lines...) Expand all Loading... | |
326 external_data_ = ExternalNewSpaceBit::update(false, external_data_); | 336 external_data_ = ExternalNewSpaceBit::update(false, external_data_); |
327 } | 337 } |
328 | 338 |
329 // Returns the space to charge for the external size. | 339 // Returns the space to charge for the external size. |
330 Heap::Space SpaceForExternal() const { | 340 Heap::Space SpaceForExternal() const { |
331 // Non-heap and VM-heap objects count as old space here. | 341 // Non-heap and VM-heap objects count as old space here. |
332 return (raw_->IsHeapObject() && raw_->IsNewObject()) ? | 342 return (raw_->IsHeapObject() && raw_->IsNewObject()) ? |
333 Heap::kNew : Heap::kOld; | 343 Heap::kNew : Heap::kOld; |
334 } | 344 } |
335 | 345 |
346 friend class BackgroundFinalizer; | |
347 | |
336 RawObject* raw_; | 348 RawObject* raw_; |
337 void* peer_; | 349 void* peer_; |
338 uword external_data_; | 350 uword external_data_; |
339 Dart_WeakPersistentHandleFinalizer callback_; | 351 Dart_WeakPersistentHandleFinalizer callback_; |
352 | |
340 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. | 353 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. |
341 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); | 354 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); |
342 }; | 355 }; |
343 | 356 |
344 | 357 |
345 // Local handles repository structure. | 358 // Local handles repository structure. |
346 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; | 359 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; |
347 static const int kLocalHandlesPerChunk = 64; | 360 static const int kLocalHandlesPerChunk = 64; |
348 static const int kOffsetOfRawPtrInLocalHandle = 0; | 361 static const int kOffsetOfRawPtrInLocalHandle = 0; |
349 class LocalHandles : Handles<kLocalHandleSizeInWords, | 362 class LocalHandles : Handles<kLocalHandleSizeInWords, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
494 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; | 507 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; |
495 class FinalizablePersistentHandles | 508 class FinalizablePersistentHandles |
496 : Handles<kFinalizablePersistentHandleSizeInWords, | 509 : Handles<kFinalizablePersistentHandleSizeInWords, |
497 kFinalizablePersistentHandlesPerChunk, | 510 kFinalizablePersistentHandlesPerChunk, |
498 kOffsetOfRawPtrInFinalizablePersistentHandle> { | 511 kOffsetOfRawPtrInFinalizablePersistentHandle> { |
499 public: | 512 public: |
500 FinalizablePersistentHandles() | 513 FinalizablePersistentHandles() |
501 : Handles<kFinalizablePersistentHandleSizeInWords, | 514 : Handles<kFinalizablePersistentHandleSizeInWords, |
502 kFinalizablePersistentHandlesPerChunk, | 515 kFinalizablePersistentHandlesPerChunk, |
503 kOffsetOfRawPtrInFinalizablePersistentHandle>(), | 516 kOffsetOfRawPtrInFinalizablePersistentHandle>(), |
504 free_list_(NULL) { } | 517 free_list_(NULL), mutex_(new Mutex()) { } |
505 ~FinalizablePersistentHandles() { | 518 ~FinalizablePersistentHandles() { |
506 free_list_ = NULL; | 519 free_list_ = NULL; |
520 delete mutex_; | |
521 mutex_ = NULL; | |
507 } | 522 } |
508 | 523 |
509 // Accessors. | 524 // Accessors. |
510 FinalizablePersistentHandle* free_list() const { return free_list_; } | 525 FinalizablePersistentHandle* free_list() const { return free_list_; } |
511 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } | 526 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } |
512 | 527 |
513 // Visit all handles stored in the various handle blocks. | 528 // Visit all handles stored in the various handle blocks. |
514 void VisitHandles(HandleVisitor* visitor) { | 529 void VisitHandles(HandleVisitor* visitor) { |
515 Handles<kFinalizablePersistentHandleSizeInWords, | 530 Handles<kFinalizablePersistentHandleSizeInWords, |
516 kFinalizablePersistentHandlesPerChunk, | 531 kFinalizablePersistentHandlesPerChunk, |
517 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit( | 532 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit( |
518 visitor); | 533 visitor); |
519 } | 534 } |
520 | 535 |
521 // Visit all object pointers stored in the various handles. | 536 // Visit all object pointers stored in the various handles. |
522 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 537 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
523 Handles<kFinalizablePersistentHandleSizeInWords, | 538 Handles<kFinalizablePersistentHandleSizeInWords, |
524 kFinalizablePersistentHandlesPerChunk, | 539 kFinalizablePersistentHandlesPerChunk, |
525 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( | 540 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( |
526 visitor); | 541 visitor); |
527 } | 542 } |
528 | 543 |
529 // Allocates a persistent handle, these have to be destroyed explicitly | 544 // Allocates a persistent handle, these have to be destroyed explicitly |
530 // by calling FreeHandle. | 545 // by calling FreeHandle. |
531 FinalizablePersistentHandle* AllocateHandle() { | 546 FinalizablePersistentHandle* AllocateHandle() { |
547 MutexLocker ml(mutex_); | |
532 FinalizablePersistentHandle* handle; | 548 FinalizablePersistentHandle* handle; |
533 if (free_list_ != NULL) { | 549 if (free_list_ != NULL) { |
534 handle = free_list_; | 550 handle = free_list_; |
535 free_list_ = handle->Next(); | 551 free_list_ = handle->Next(); |
536 handle->set_raw(Object::null()); | 552 handle->set_raw(Object::null()); |
537 } else { | 553 } else { |
538 handle = reinterpret_cast<FinalizablePersistentHandle*>( | 554 handle = reinterpret_cast<FinalizablePersistentHandle*>( |
539 AllocateScopedHandle()); | 555 AllocateScopedHandle()); |
540 handle->Clear(); | 556 handle->Clear(); |
541 } | 557 } |
542 return handle; | 558 return handle; |
543 } | 559 } |
544 | 560 |
545 void FreeHandle(FinalizablePersistentHandle* handle) { | 561 void FreeHandle(FinalizablePersistentHandle* handle) { |
562 MutexLocker ml(mutex_); | |
546 handle->FreeHandle(free_list()); | 563 handle->FreeHandle(free_list()); |
547 set_free_list(handle); | 564 set_free_list(handle); |
548 } | 565 } |
549 | 566 |
550 // Validate if passed in handle is a Persistent Handle. | 567 // Validate if passed in handle is a Persistent Handle. |
551 bool IsValidHandle(Dart_WeakPersistentHandle object) const { | 568 bool IsValidHandle(Dart_WeakPersistentHandle object) const { |
569 MutexLocker ml(mutex_); | |
552 return IsValidScopedHandle(reinterpret_cast<uword>(object)); | 570 return IsValidScopedHandle(reinterpret_cast<uword>(object)); |
553 } | 571 } |
554 | 572 |
555 // Returns a count of active handles (used for testing purposes). | 573 // Returns a count of active handles (used for testing purposes). |
556 int CountHandles() const { | 574 int CountHandles() const { |
557 return CountScopedHandles(); | 575 return CountScopedHandles(); |
558 } | 576 } |
559 | 577 |
560 private: | 578 private: |
561 FinalizablePersistentHandle* free_list_; | 579 FinalizablePersistentHandle* free_list_; |
580 Mutex* mutex_; | |
562 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles); | 581 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles); |
563 }; | 582 }; |
564 | 583 |
565 | 584 |
566 // Structure used for the implementation of local scopes used in dart_api. | 585 // Structure used for the implementation of local scopes used in dart_api. |
567 // These local scopes manage handles and memory allocated in the scope. | 586 // These local scopes manage handles and memory allocated in the scope. |
568 class ApiLocalScope { | 587 class ApiLocalScope { |
569 public: | 588 public: |
570 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : | 589 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : |
571 previous_(previous), stack_marker_(stack_marker) { } | 590 previous_(previous), stack_marker_(stack_marker) { } |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
762 ref->set_peer(peer); | 781 ref->set_peer(peer); |
763 ref->set_callback(callback); | 782 ref->set_callback(callback); |
764 // This may trigger GC, so it must be called last. | 783 // This may trigger GC, so it must be called last. |
765 ref->SetExternalSize(external_size, isolate); | 784 ref->SetExternalSize(external_size, isolate); |
766 return ref; | 785 return ref; |
767 } | 786 } |
768 | 787 |
769 } // namespace dart | 788 } // namespace dart |
770 | 789 |
771 #endif // VM_DART_API_STATE_H_ | 790 #endif // VM_DART_API_STATE_H_ |
OLD | NEW |