| 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" |
| 11 #include "vm/bitfield.h" | 11 #include "vm/bitfield.h" |
| 12 #include "vm/dart_api_impl.h" | 12 #include "vm/dart_api_impl.h" |
| 13 #include "vm/flags.h" | 13 #include "vm/flags.h" |
| 14 #include "vm/growable_array.h" | 14 #include "vm/growable_array.h" |
| 15 #include "vm/handles.h" | 15 #include "vm/handles.h" |
| 16 #include "vm/object.h" | 16 #include "vm/object.h" |
| 17 #include "vm/os.h" | 17 #include "vm/os.h" |
| 18 #include "vm/raw_object.h" |
| 18 #include "vm/os_thread.h" | 19 #include "vm/os_thread.h" |
| 19 #include "vm/raw_object.h" | |
| 20 #include "vm/thread_pool.h" | |
| 21 #include "vm/visitor.h" | 20 #include "vm/visitor.h" |
| 22 #include "vm/weak_table.h" | 21 #include "vm/weak_table.h" |
| 23 | 22 |
| 24 #include "vm/handles_impl.h" | 23 #include "vm/handles_impl.h" |
| 25 | 24 |
| 26 namespace dart { | 25 namespace dart { |
| 27 | 26 |
| 28 class FinalizablePersistentHandle; | |
| 29 typedef MallocGrowableArray<FinalizablePersistentHandle*> FinalizationQueue; | |
| 30 | |
| 31 | |
| 32 class BackgroundFinalizer : public ThreadPool::Task { | |
| 33 public: | |
| 34 BackgroundFinalizer(Isolate* isolate, FinalizationQueue* queue); | |
| 35 virtual ~BackgroundFinalizer() { } | |
| 36 | |
| 37 void Run(); | |
| 38 | |
| 39 private: | |
| 40 Isolate* isolate_; | |
| 41 FinalizationQueue* queue_; | |
| 42 | |
| 43 DISALLOW_IMPLICIT_CONSTRUCTORS(BackgroundFinalizer); | |
| 44 }; | |
| 45 | |
| 46 | |
| 47 // Implementation of Zone support for very fast allocation of small chunks | 27 // Implementation of Zone support for very fast allocation of small chunks |
| 48 // of memory. The chunks cannot be deallocated individually, but instead | 28 // of memory. The chunks cannot be deallocated individually, but instead |
| 49 // zones support deallocating all chunks in one fast operation when the | 29 // zones support deallocating all chunks in one fast operation when the |
| 50 // scope is exited. | 30 // scope is exited. |
| 51 class ApiZone { | 31 class ApiZone { |
| 52 public: | 32 public: |
| 53 // Create an empty zone. | 33 // Create an empty zone. |
| 54 ApiZone() : zone_() { | 34 ApiZone() : zone_() { |
| 55 Thread* thread = Thread::Current(); | 35 Thread* thread = Thread::Current(); |
| 56 Zone* zone = thread != NULL ? thread->zone() : NULL; | 36 Zone* zone = thread != NULL ? thread->zone() : NULL; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 void SetExternalSize(intptr_t size, Isolate* isolate) { | 220 void SetExternalSize(intptr_t size, Isolate* isolate) { |
| 241 ASSERT(size >= 0); | 221 ASSERT(size >= 0); |
| 242 set_external_size(Utils::RoundUp(size, kObjectAlignment)); | 222 set_external_size(Utils::RoundUp(size, kObjectAlignment)); |
| 243 if (SpaceForExternal() == Heap::kNew) { | 223 if (SpaceForExternal() == Heap::kNew) { |
| 244 SetExternalNewSpaceBit(); | 224 SetExternalNewSpaceBit(); |
| 245 } | 225 } |
| 246 isolate->heap()->AllocateExternal(external_size(), SpaceForExternal()); | 226 isolate->heap()->AllocateExternal(external_size(), SpaceForExternal()); |
| 247 } | 227 } |
| 248 | 228 |
| 249 // Called when the referent becomes unreachable. | 229 // Called when the referent becomes unreachable. |
| 250 void UpdateUnreachable(Isolate* isolate, FinalizationQueue* queue) { | 230 void UpdateUnreachable(Isolate* isolate) { |
| 251 EnsureFreeExternal(isolate); | 231 EnsureFreeExternal(isolate); |
| 252 if (queue == NULL) { | 232 Finalize(isolate, this); |
| 253 Finalize(isolate, this); | |
| 254 } else { | |
| 255 MarkForFinalization(); | |
| 256 queue->Add(this); | |
| 257 } | |
| 258 } | 233 } |
| 259 | 234 |
| 260 // Called when the referent has moved, potentially between generations. | 235 // Called when the referent has moved, potentially between generations. |
| 261 void UpdateRelocated(Isolate* isolate) { | 236 void UpdateRelocated(Isolate* isolate) { |
| 262 if (IsSetNewSpaceBit() && (SpaceForExternal() == Heap::kOld)) { | 237 if (IsSetNewSpaceBit() && (SpaceForExternal() == Heap::kOld)) { |
| 263 isolate->heap()->PromoteExternal(external_size()); | 238 isolate->heap()->PromoteExternal(external_size()); |
| 264 ClearExternalNewSpaceBit(); | 239 ClearExternalNewSpaceBit(); |
| 265 } | 240 } |
| 266 } | 241 } |
| 267 | 242 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 SetNext(free_list); | 292 SetNext(free_list); |
| 318 } | 293 } |
| 319 | 294 |
| 320 void Clear() { | 295 void Clear() { |
| 321 raw_ = Object::null(); | 296 raw_ = Object::null(); |
| 322 peer_ = NULL; | 297 peer_ = NULL; |
| 323 external_data_ = 0; | 298 external_data_ = 0; |
| 324 callback_ = NULL; | 299 callback_ = NULL; |
| 325 } | 300 } |
| 326 | 301 |
| 327 void MarkForFinalization() { | |
| 328 raw_ = Object::null(); | |
| 329 ASSERT(callback_ != NULL); | |
| 330 } | |
| 331 | |
| 332 void set_raw(RawObject* raw) { raw_ = raw; } | 302 void set_raw(RawObject* raw) { raw_ = raw; } |
| 333 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } | 303 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } |
| 334 void set_raw(const Object& object) { raw_ = object.raw(); } | 304 void set_raw(const Object& object) { raw_ = object.raw(); } |
| 335 | 305 |
| 336 void set_peer(void* peer) { peer_ = peer; } | 306 void set_peer(void* peer) { peer_ = peer; } |
| 337 | 307 |
| 338 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { | 308 void set_callback(Dart_WeakPersistentHandleFinalizer callback) { |
| 339 callback_ = callback; | 309 callback_ = callback; |
| 340 } | 310 } |
| 341 | 311 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 356 external_data_ = ExternalNewSpaceBit::update(false, external_data_); | 326 external_data_ = ExternalNewSpaceBit::update(false, external_data_); |
| 357 } | 327 } |
| 358 | 328 |
| 359 // Returns the space to charge for the external size. | 329 // Returns the space to charge for the external size. |
| 360 Heap::Space SpaceForExternal() const { | 330 Heap::Space SpaceForExternal() const { |
| 361 // Non-heap and VM-heap objects count as old space here. | 331 // Non-heap and VM-heap objects count as old space here. |
| 362 return (raw_->IsHeapObject() && raw_->IsNewObject()) ? | 332 return (raw_->IsHeapObject() && raw_->IsNewObject()) ? |
| 363 Heap::kNew : Heap::kOld; | 333 Heap::kNew : Heap::kOld; |
| 364 } | 334 } |
| 365 | 335 |
| 366 friend class BackgroundFinalizer; | |
| 367 | |
| 368 RawObject* raw_; | 336 RawObject* raw_; |
| 369 void* peer_; | 337 void* peer_; |
| 370 uword external_data_; | 338 uword external_data_; |
| 371 Dart_WeakPersistentHandleFinalizer callback_; | 339 Dart_WeakPersistentHandleFinalizer callback_; |
| 372 | |
| 373 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. | 340 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. |
| 374 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); | 341 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); |
| 375 }; | 342 }; |
| 376 | 343 |
| 377 | 344 |
| 378 // Local handles repository structure. | 345 // Local handles repository structure. |
| 379 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; | 346 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; |
| 380 static const int kLocalHandlesPerChunk = 64; | 347 static const int kLocalHandlesPerChunk = 64; |
| 381 static const int kOffsetOfRawPtrInLocalHandle = 0; | 348 static const int kOffsetOfRawPtrInLocalHandle = 0; |
| 382 class LocalHandles : Handles<kLocalHandleSizeInWords, | 349 class LocalHandles : Handles<kLocalHandleSizeInWords, |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; | 494 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; |
| 528 class FinalizablePersistentHandles | 495 class FinalizablePersistentHandles |
| 529 : Handles<kFinalizablePersistentHandleSizeInWords, | 496 : Handles<kFinalizablePersistentHandleSizeInWords, |
| 530 kFinalizablePersistentHandlesPerChunk, | 497 kFinalizablePersistentHandlesPerChunk, |
| 531 kOffsetOfRawPtrInFinalizablePersistentHandle> { | 498 kOffsetOfRawPtrInFinalizablePersistentHandle> { |
| 532 public: | 499 public: |
| 533 FinalizablePersistentHandles() | 500 FinalizablePersistentHandles() |
| 534 : Handles<kFinalizablePersistentHandleSizeInWords, | 501 : Handles<kFinalizablePersistentHandleSizeInWords, |
| 535 kFinalizablePersistentHandlesPerChunk, | 502 kFinalizablePersistentHandlesPerChunk, |
| 536 kOffsetOfRawPtrInFinalizablePersistentHandle>(), | 503 kOffsetOfRawPtrInFinalizablePersistentHandle>(), |
| 537 free_list_(NULL), mutex_(new Mutex()) { } | 504 free_list_(NULL) { } |
| 538 ~FinalizablePersistentHandles() { | 505 ~FinalizablePersistentHandles() { |
| 539 free_list_ = NULL; | 506 free_list_ = NULL; |
| 540 delete mutex_; | |
| 541 mutex_ = NULL; | |
| 542 } | 507 } |
| 543 | 508 |
| 544 // Accessors. | 509 // Accessors. |
| 545 FinalizablePersistentHandle* free_list() const { return free_list_; } | 510 FinalizablePersistentHandle* free_list() const { return free_list_; } |
| 546 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } | 511 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } |
| 547 | 512 |
| 548 // Visit all handles stored in the various handle blocks. | 513 // Visit all handles stored in the various handle blocks. |
| 549 void VisitHandles(HandleVisitor* visitor) { | 514 void VisitHandles(HandleVisitor* visitor) { |
| 550 Handles<kFinalizablePersistentHandleSizeInWords, | 515 Handles<kFinalizablePersistentHandleSizeInWords, |
| 551 kFinalizablePersistentHandlesPerChunk, | 516 kFinalizablePersistentHandlesPerChunk, |
| 552 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit( | 517 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit( |
| 553 visitor); | 518 visitor); |
| 554 } | 519 } |
| 555 | 520 |
| 556 // Visit all object pointers stored in the various handles. | 521 // Visit all object pointers stored in the various handles. |
| 557 void VisitObjectPointers(ObjectPointerVisitor* visitor) { | 522 void VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 558 Handles<kFinalizablePersistentHandleSizeInWords, | 523 Handles<kFinalizablePersistentHandleSizeInWords, |
| 559 kFinalizablePersistentHandlesPerChunk, | 524 kFinalizablePersistentHandlesPerChunk, |
| 560 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( | 525 kOffsetOfRawPtrInFinalizablePersistentHandle>::VisitObjectPointers( |
| 561 visitor); | 526 visitor); |
| 562 } | 527 } |
| 563 | 528 |
| 564 // Allocates a persistent handle, these have to be destroyed explicitly | 529 // Allocates a persistent handle, these have to be destroyed explicitly |
| 565 // by calling FreeHandle. | 530 // by calling FreeHandle. |
| 566 FinalizablePersistentHandle* AllocateHandle() { | 531 FinalizablePersistentHandle* AllocateHandle() { |
| 567 FinalizablePersistentHandle* handle; | 532 FinalizablePersistentHandle* handle; |
| 568 { | 533 if (free_list_ != NULL) { |
| 569 MutexLocker ml(mutex_); | 534 handle = free_list_; |
| 570 if (free_list_ != NULL) { | 535 free_list_ = handle->Next(); |
| 571 handle = free_list_; | 536 handle->set_raw(Object::null()); |
| 572 free_list_ = handle->Next(); | 537 } else { |
| 573 handle->set_raw(Object::null()); | 538 handle = reinterpret_cast<FinalizablePersistentHandle*>( |
| 574 return handle; | 539 AllocateScopedHandle()); |
| 575 } | 540 handle->Clear(); |
| 576 } | 541 } |
| 577 | |
| 578 handle = reinterpret_cast<FinalizablePersistentHandle*>( | |
| 579 AllocateScopedHandle()); | |
| 580 handle->Clear(); | |
| 581 return handle; | 542 return handle; |
| 582 } | 543 } |
| 583 | 544 |
| 584 void FreeHandle(FinalizablePersistentHandle* handle) { | 545 void FreeHandle(FinalizablePersistentHandle* handle) { |
| 585 MutexLocker ml(mutex_); | |
| 586 handle->FreeHandle(free_list()); | 546 handle->FreeHandle(free_list()); |
| 587 set_free_list(handle); | 547 set_free_list(handle); |
| 588 } | 548 } |
| 589 | 549 |
| 590 // Validate if passed in handle is a Persistent Handle. | 550 // Validate if passed in handle is a Persistent Handle. |
| 591 bool IsValidHandle(Dart_WeakPersistentHandle object) const { | 551 bool IsValidHandle(Dart_WeakPersistentHandle object) const { |
| 592 MutexLocker ml(mutex_); | |
| 593 return IsValidScopedHandle(reinterpret_cast<uword>(object)); | 552 return IsValidScopedHandle(reinterpret_cast<uword>(object)); |
| 594 } | 553 } |
| 595 | 554 |
| 596 // Returns a count of active handles (used for testing purposes). | 555 // Returns a count of active handles (used for testing purposes). |
| 597 int CountHandles() const { | 556 int CountHandles() const { |
| 598 return CountScopedHandles(); | 557 return CountScopedHandles(); |
| 599 } | 558 } |
| 600 | 559 |
| 601 private: | 560 private: |
| 602 FinalizablePersistentHandle* free_list_; | 561 FinalizablePersistentHandle* free_list_; |
| 603 Mutex* mutex_; | |
| 604 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles); | 562 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles); |
| 605 }; | 563 }; |
| 606 | 564 |
| 607 | 565 |
| 608 // Structure used for the implementation of local scopes used in dart_api. | 566 // Structure used for the implementation of local scopes used in dart_api. |
| 609 // These local scopes manage handles and memory allocated in the scope. | 567 // These local scopes manage handles and memory allocated in the scope. |
| 610 class ApiLocalScope { | 568 class ApiLocalScope { |
| 611 public: | 569 public: |
| 612 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : | 570 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) : |
| 613 previous_(previous), stack_marker_(stack_marker) { } | 571 previous_(previous), stack_marker_(stack_marker) { } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 ref->set_peer(peer); | 762 ref->set_peer(peer); |
| 805 ref->set_callback(callback); | 763 ref->set_callback(callback); |
| 806 // This may trigger GC, so it must be called last. | 764 // This may trigger GC, so it must be called last. |
| 807 ref->SetExternalSize(external_size, isolate); | 765 ref->SetExternalSize(external_size, isolate); |
| 808 return ref; | 766 return ref; |
| 809 } | 767 } |
| 810 | 768 |
| 811 } // namespace dart | 769 } // namespace dart |
| 812 | 770 |
| 813 #endif // VM_DART_API_STATE_H_ | 771 #endif // VM_DART_API_STATE_H_ |
| OLD | NEW |