Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: runtime/vm/dart_api_state.h

Issue 2974233002: VM: Re-format to use at most one newline between functions (Closed)
Patch Set: Rebase and merge Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/dart_api_message.cc ('k') | runtime/vm/dart_entry.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 RUNTIME_VM_DART_API_STATE_H_ 5 #ifndef RUNTIME_VM_DART_API_STATE_H_
6 #define RUNTIME_VM_DART_API_STATE_H_ 6 #define RUNTIME_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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 114 }
115 115
116 private: 116 private:
117 Zone zone_; 117 Zone zone_;
118 118
119 template <typename T> 119 template <typename T>
120 friend class ApiGrowableArray; 120 friend class ApiGrowableArray;
121 DISALLOW_COPY_AND_ASSIGN(ApiZone); 121 DISALLOW_COPY_AND_ASSIGN(ApiZone);
122 }; 122 };
123 123
124
125 // Implementation of local handles which are handed out from every 124 // Implementation of local handles which are handed out from every
126 // dart API call, these handles are valid only in the present scope 125 // dart API call, these handles are valid only in the present scope
127 // and are destroyed when a Dart_ExitScope() is called. 126 // and are destroyed when a Dart_ExitScope() is called.
128 class LocalHandle { 127 class LocalHandle {
129 public: 128 public:
130 // Accessors. 129 // Accessors.
131 RawObject* raw() const { return raw_; } 130 RawObject* raw() const { return raw_; }
132 void set_raw(RawObject* raw) { raw_ = raw; } 131 void set_raw(RawObject* raw) { raw_ = raw; }
133 static intptr_t raw_offset() { return OFFSET_OF(LocalHandle, raw_); } 132 static intptr_t raw_offset() { return OFFSET_OF(LocalHandle, raw_); }
134 133
135 Dart_Handle apiHandle() { return reinterpret_cast<Dart_Handle>(this); } 134 Dart_Handle apiHandle() { return reinterpret_cast<Dart_Handle>(this); }
136 135
137 private: 136 private:
138 LocalHandle() {} 137 LocalHandle() {}
139 ~LocalHandle() {} 138 ~LocalHandle() {}
140 139
141 RawObject* raw_; 140 RawObject* raw_;
142 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 141 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
143 DISALLOW_COPY_AND_ASSIGN(LocalHandle); 142 DISALLOW_COPY_AND_ASSIGN(LocalHandle);
144 }; 143 };
145 144
146
147 // A distinguished callback which indicates that a persistent handle 145 // A distinguished callback which indicates that a persistent handle
148 // should not be deleted from the dart api. 146 // should not be deleted from the dart api.
149 void ProtectedHandleCallback(void* peer); 147 void ProtectedHandleCallback(void* peer);
150 148
151
152 // Implementation of persistent handles which are handed out through the 149 // Implementation of persistent handles which are handed out through the
153 // dart API. 150 // dart API.
154 class PersistentHandle { 151 class PersistentHandle {
155 public: 152 public:
156 // Accessors. 153 // Accessors.
157 RawObject* raw() const { return raw_; } 154 RawObject* raw() const { return raw_; }
158 void set_raw(RawObject* ref) { raw_ = ref; } 155 void set_raw(RawObject* ref) { raw_ = ref; }
159 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } 156 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); }
160 void set_raw(const Object& object) { raw_ = object.raw(); } 157 void set_raw(const Object& object) { raw_ = object.raw(); }
161 RawObject** raw_addr() { return &raw_; } 158 RawObject** raw_addr() { return &raw_; }
(...skipping 18 matching lines...) Expand all
180 raw_ = reinterpret_cast<RawObject*>(free_list); 177 raw_ = reinterpret_cast<RawObject*>(free_list);
181 ASSERT(!raw_->IsHeapObject()); 178 ASSERT(!raw_->IsHeapObject());
182 } 179 }
183 void FreeHandle(PersistentHandle* free_list) { SetNext(free_list); } 180 void FreeHandle(PersistentHandle* free_list) { SetNext(free_list); }
184 181
185 RawObject* raw_; 182 RawObject* raw_;
186 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 183 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
187 DISALLOW_COPY_AND_ASSIGN(PersistentHandle); 184 DISALLOW_COPY_AND_ASSIGN(PersistentHandle);
188 }; 185 };
189 186
190
191 // Implementation of persistent handles which are handed out through the 187 // Implementation of persistent handles which are handed out through the
192 // dart API. 188 // dart API.
193 class FinalizablePersistentHandle { 189 class FinalizablePersistentHandle {
194 public: 190 public:
195 static FinalizablePersistentHandle* New( 191 static FinalizablePersistentHandle* New(
196 Isolate* isolate, 192 Isolate* isolate,
197 const Object& object, 193 const Object& object,
198 void* peer, 194 void* peer,
199 Dart_WeakPersistentHandleFinalizer callback, 195 Dart_WeakPersistentHandleFinalizer callback,
200 intptr_t external_size); 196 intptr_t external_size);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 327
332 RawObject* raw_; 328 RawObject* raw_;
333 void* peer_; 329 void* peer_;
334 uword external_data_; 330 uword external_data_;
335 Dart_WeakPersistentHandleFinalizer callback_; 331 Dart_WeakPersistentHandleFinalizer callback_;
336 332
337 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 333 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
338 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); 334 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle);
339 }; 335 };
340 336
341
342 // Local handles repository structure. 337 // Local handles repository structure.
343 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; 338 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize;
344 static const int kLocalHandlesPerChunk = 64; 339 static const int kLocalHandlesPerChunk = 64;
345 static const int kOffsetOfRawPtrInLocalHandle = 0; 340 static const int kOffsetOfRawPtrInLocalHandle = 0;
346 class LocalHandles : Handles<kLocalHandleSizeInWords, 341 class LocalHandles : Handles<kLocalHandleSizeInWords,
347 kLocalHandlesPerChunk, 342 kLocalHandlesPerChunk,
348 kOffsetOfRawPtrInLocalHandle> { 343 kOffsetOfRawPtrInLocalHandle> {
349 public: 344 public:
350 LocalHandles() 345 LocalHandles()
351 : Handles<kLocalHandleSizeInWords, 346 : Handles<kLocalHandleSizeInWords,
352 kLocalHandlesPerChunk, 347 kLocalHandlesPerChunk,
353 kOffsetOfRawPtrInLocalHandle>() { 348 kOffsetOfRawPtrInLocalHandle>() {
354 if (FLAG_trace_handles) { 349 if (FLAG_trace_handles) {
355 OS::PrintErr("*** Starting a new Local handle block 0x%" Px "\n", 350 OS::PrintErr("*** Starting a new Local handle block 0x%" Px "\n",
356 reinterpret_cast<intptr_t>(this)); 351 reinterpret_cast<intptr_t>(this));
357 } 352 }
358 } 353 }
359 ~LocalHandles() { 354 ~LocalHandles() {
360 if (FLAG_trace_handles) { 355 if (FLAG_trace_handles) {
361 OS::PrintErr("*** Handle Counts for 0x(%" Px "):Scoped = %d\n", 356 OS::PrintErr("*** Handle Counts for 0x(%" Px "):Scoped = %d\n",
362 reinterpret_cast<intptr_t>(this), CountHandles()); 357 reinterpret_cast<intptr_t>(this), CountHandles());
363 OS::PrintErr("*** Deleting Local handle block 0x%" Px "\n", 358 OS::PrintErr("*** Deleting Local handle block 0x%" Px "\n",
364 reinterpret_cast<intptr_t>(this)); 359 reinterpret_cast<intptr_t>(this));
365 } 360 }
366 } 361 }
367 362
368
369 // Visit all object pointers stored in the various handles. 363 // Visit all object pointers stored in the various handles.
370 void VisitObjectPointers(ObjectPointerVisitor* visitor) { 364 void VisitObjectPointers(ObjectPointerVisitor* visitor) {
371 Handles<kLocalHandleSizeInWords, kLocalHandlesPerChunk, 365 Handles<kLocalHandleSizeInWords, kLocalHandlesPerChunk,
372 kOffsetOfRawPtrInLocalHandle>::VisitObjectPointers(visitor); 366 kOffsetOfRawPtrInLocalHandle>::VisitObjectPointers(visitor);
373 } 367 }
374 368
375 // Reset the local handles block for reuse. 369 // Reset the local handles block for reuse.
376 void Reset() { 370 void Reset() {
377 Handles<kLocalHandleSizeInWords, kLocalHandlesPerChunk, 371 Handles<kLocalHandleSizeInWords, kLocalHandlesPerChunk,
378 kOffsetOfRawPtrInLocalHandle>::Reset(); 372 kOffsetOfRawPtrInLocalHandle>::Reset();
(...skipping 11 matching lines...) Expand all
390 return IsValidScopedHandle(reinterpret_cast<uword>(object)); 384 return IsValidScopedHandle(reinterpret_cast<uword>(object));
391 } 385 }
392 386
393 // Returns a count of active handles (used for testing purposes). 387 // Returns a count of active handles (used for testing purposes).
394 int CountHandles() const { return CountScopedHandles(); } 388 int CountHandles() const { return CountScopedHandles(); }
395 389
396 private: 390 private:
397 DISALLOW_COPY_AND_ASSIGN(LocalHandles); 391 DISALLOW_COPY_AND_ASSIGN(LocalHandles);
398 }; 392 };
399 393
400
401 // Persistent handles repository structure. 394 // Persistent handles repository structure.
402 static const int kPersistentHandleSizeInWords = 395 static const int kPersistentHandleSizeInWords =
403 sizeof(PersistentHandle) / kWordSize; 396 sizeof(PersistentHandle) / kWordSize;
404 static const int kPersistentHandlesPerChunk = 64; 397 static const int kPersistentHandlesPerChunk = 64;
405 static const int kOffsetOfRawPtrInPersistentHandle = 0; 398 static const int kOffsetOfRawPtrInPersistentHandle = 0;
406 class PersistentHandles : Handles<kPersistentHandleSizeInWords, 399 class PersistentHandles : Handles<kPersistentHandleSizeInWords,
407 kPersistentHandlesPerChunk, 400 kPersistentHandlesPerChunk,
408 kOffsetOfRawPtrInPersistentHandle> { 401 kOffsetOfRawPtrInPersistentHandle> {
409 public: 402 public:
410 PersistentHandles() 403 PersistentHandles()
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 } 472 }
480 473
481 // Returns a count of active handles (used for testing purposes). 474 // Returns a count of active handles (used for testing purposes).
482 int CountHandles() const { return CountScopedHandles(); } 475 int CountHandles() const { return CountScopedHandles(); }
483 476
484 private: 477 private:
485 PersistentHandle* free_list_; 478 PersistentHandle* free_list_;
486 DISALLOW_COPY_AND_ASSIGN(PersistentHandles); 479 DISALLOW_COPY_AND_ASSIGN(PersistentHandles);
487 }; 480 };
488 481
489
490 // Finalizable persistent handles repository structure. 482 // Finalizable persistent handles repository structure.
491 static const int kFinalizablePersistentHandleSizeInWords = 483 static const int kFinalizablePersistentHandleSizeInWords =
492 sizeof(FinalizablePersistentHandle) / kWordSize; 484 sizeof(FinalizablePersistentHandle) / kWordSize;
493 static const int kFinalizablePersistentHandlesPerChunk = 64; 485 static const int kFinalizablePersistentHandlesPerChunk = 64;
494 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0; 486 static const int kOffsetOfRawPtrInFinalizablePersistentHandle = 0;
495 class FinalizablePersistentHandles 487 class FinalizablePersistentHandles
496 : Handles<kFinalizablePersistentHandleSizeInWords, 488 : Handles<kFinalizablePersistentHandleSizeInWords,
497 kFinalizablePersistentHandlesPerChunk, 489 kFinalizablePersistentHandlesPerChunk,
498 kOffsetOfRawPtrInFinalizablePersistentHandle> { 490 kOffsetOfRawPtrInFinalizablePersistentHandle> {
499 public: 491 public:
500 FinalizablePersistentHandles() 492 FinalizablePersistentHandles()
501 : Handles<kFinalizablePersistentHandleSizeInWords, 493 : Handles<kFinalizablePersistentHandleSizeInWords,
502 kFinalizablePersistentHandlesPerChunk, 494 kFinalizablePersistentHandlesPerChunk,
503 kOffsetOfRawPtrInFinalizablePersistentHandle>(), 495 kOffsetOfRawPtrInFinalizablePersistentHandle>(),
504 free_list_(NULL) {} 496 free_list_(NULL) {}
505 ~FinalizablePersistentHandles() { 497 ~FinalizablePersistentHandles() { free_list_ = NULL; }
506 free_list_ = NULL;
507 }
508 498
509 // Accessors. 499 // Accessors.
510 FinalizablePersistentHandle* free_list() const { return free_list_; } 500 FinalizablePersistentHandle* free_list() const { return free_list_; }
511 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; } 501 void set_free_list(FinalizablePersistentHandle* value) { free_list_ = value; }
512 502
513 // Visit all handles stored in the various handle blocks. 503 // Visit all handles stored in the various handle blocks.
514 void VisitHandles(HandleVisitor* visitor) { 504 void VisitHandles(HandleVisitor* visitor) {
515 Handles<kFinalizablePersistentHandleSizeInWords, 505 Handles<kFinalizablePersistentHandleSizeInWords,
516 kFinalizablePersistentHandlesPerChunk, 506 kFinalizablePersistentHandlesPerChunk,
517 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit(visitor); 507 kOffsetOfRawPtrInFinalizablePersistentHandle>::Visit(visitor);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 } 554 }
565 555
566 // Returns a count of active handles (used for testing purposes). 556 // Returns a count of active handles (used for testing purposes).
567 int CountHandles() const { return CountScopedHandles(); } 557 int CountHandles() const { return CountScopedHandles(); }
568 558
569 private: 559 private:
570 FinalizablePersistentHandle* free_list_; 560 FinalizablePersistentHandle* free_list_;
571 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles); 561 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandles);
572 }; 562 };
573 563
574
575 // Structure used for the implementation of local scopes used in dart_api. 564 // Structure used for the implementation of local scopes used in dart_api.
576 // These local scopes manage handles and memory allocated in the scope. 565 // These local scopes manage handles and memory allocated in the scope.
577 class ApiLocalScope { 566 class ApiLocalScope {
578 public: 567 public:
579 ApiLocalScope(ApiLocalScope* previous, uword stack_marker) 568 ApiLocalScope(ApiLocalScope* previous, uword stack_marker)
580 : previous_(previous), stack_marker_(stack_marker) {} 569 : previous_(previous), stack_marker_(stack_marker) {}
581 ~ApiLocalScope() { previous_ = NULL; } 570 ~ApiLocalScope() { previous_ = NULL; }
582 571
583 // Reinit the ApiLocalScope to new values. 572 // Reinit the ApiLocalScope to new values.
584 void Reinit(Thread* thread, ApiLocalScope* previous, uword stack_marker) { 573 void Reinit(Thread* thread, ApiLocalScope* previous, uword stack_marker) {
(...skipping 19 matching lines...) Expand all
604 593
605 private: 594 private:
606 ApiLocalScope* previous_; 595 ApiLocalScope* previous_;
607 uword stack_marker_; 596 uword stack_marker_;
608 LocalHandles local_handles_; 597 LocalHandles local_handles_;
609 ApiZone zone_; 598 ApiZone zone_;
610 599
611 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); 600 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope);
612 }; 601 };
613 602
614
615 class ApiNativeScope { 603 class ApiNativeScope {
616 public: 604 public:
617 ApiNativeScope() { 605 ApiNativeScope() {
618 // Currently no support for nesting native scopes. 606 // Currently no support for nesting native scopes.
619 ASSERT(Current() == NULL); 607 ASSERT(Current() == NULL);
620 OSThread::SetThreadLocal(Api::api_native_key_, 608 OSThread::SetThreadLocal(Api::api_native_key_,
621 reinterpret_cast<uword>(this)); 609 reinterpret_cast<uword>(this));
622 // We manually increment the memory usage counter since there is memory 610 // We manually increment the memory usage counter since there is memory
623 // initially allocated within the zone on creation. 611 // initially allocated within the zone on creation.
624 IncrementNativeScopeMemoryCapacity(zone_.GetZone()->CapacityInBytes()); 612 IncrementNativeScopeMemoryCapacity(zone_.GetZone()->CapacityInBytes());
(...skipping 30 matching lines...) Expand all
655 return result; 643 return result;
656 } 644 }
657 645
658 private: 646 private:
659 // The current total memory usage within ApiNativeScopes. 647 // The current total memory usage within ApiNativeScopes.
660 static intptr_t current_memory_usage_; 648 static intptr_t current_memory_usage_;
661 649
662 ApiZone zone_; 650 ApiZone zone_;
663 }; 651 };
664 652
665
666 // Api growable arrays use a zone for allocation. The constructor 653 // Api growable arrays use a zone for allocation. The constructor
667 // picks the zone from the current isolate if in an isolate 654 // picks the zone from the current isolate if in an isolate
668 // environment. When outside an isolate environment it picks the zone 655 // environment. When outside an isolate environment it picks the zone
669 // from the current native scope. 656 // from the current native scope.
670 template <typename T> 657 template <typename T>
671 class ApiGrowableArray : public BaseGrowableArray<T, ValueObject, Zone> { 658 class ApiGrowableArray : public BaseGrowableArray<T, ValueObject, Zone> {
672 public: 659 public:
673 explicit ApiGrowableArray(int initial_capacity) 660 explicit ApiGrowableArray(int initial_capacity)
674 : BaseGrowableArray<T, ValueObject, Zone>( 661 : BaseGrowableArray<T, ValueObject, Zone>(
675 initial_capacity, 662 initial_capacity,
676 ApiNativeScope::Current()->zone()) {} 663 ApiNativeScope::Current()->zone()) {}
677 ApiGrowableArray() 664 ApiGrowableArray()
678 : BaseGrowableArray<T, ValueObject, Zone>( 665 : BaseGrowableArray<T, ValueObject, Zone>(
679 ApiNativeScope::Current()->zone()) {} 666 ApiNativeScope::Current()->zone()) {}
680 ApiGrowableArray(intptr_t initial_capacity, Zone* zone) 667 ApiGrowableArray(intptr_t initial_capacity, Zone* zone)
681 : BaseGrowableArray<T, ValueObject, Zone>(initial_capacity, zone) {} 668 : BaseGrowableArray<T, ValueObject, Zone>(initial_capacity, zone) {}
682 }; 669 };
683 670
684
685 // Implementation of the API State used in dart api for maintaining 671 // Implementation of the API State used in dart api for maintaining
686 // local scopes, persistent handles etc. These are setup on a per isolate 672 // local scopes, persistent handles etc. These are setup on a per isolate
687 // basis and destroyed when the isolate is shutdown. 673 // basis and destroyed when the isolate is shutdown.
688 class ApiState { 674 class ApiState {
689 public: 675 public:
690 ApiState() 676 ApiState()
691 : persistent_handles_(), 677 : persistent_handles_(),
692 weak_persistent_handles_(), 678 weak_persistent_handles_(),
693 null_(NULL), 679 null_(NULL),
694 true_(NULL), 680 true_(NULL),
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 771
786 // Persistent handles to important objects. 772 // Persistent handles to important objects.
787 PersistentHandle* null_; 773 PersistentHandle* null_;
788 PersistentHandle* true_; 774 PersistentHandle* true_;
789 PersistentHandle* false_; 775 PersistentHandle* false_;
790 PersistentHandle* acquired_error_; 776 PersistentHandle* acquired_error_;
791 777
792 DISALLOW_COPY_AND_ASSIGN(ApiState); 778 DISALLOW_COPY_AND_ASSIGN(ApiState);
793 }; 779 };
794 780
795
796 inline FinalizablePersistentHandle* FinalizablePersistentHandle::New( 781 inline FinalizablePersistentHandle* FinalizablePersistentHandle::New(
797 Isolate* isolate, 782 Isolate* isolate,
798 const Object& object, 783 const Object& object,
799 void* peer, 784 void* peer,
800 Dart_WeakPersistentHandleFinalizer callback, 785 Dart_WeakPersistentHandleFinalizer callback,
801 intptr_t external_size) { 786 intptr_t external_size) {
802 ApiState* state = isolate->api_state(); 787 ApiState* state = isolate->api_state();
803 ASSERT(state != NULL); 788 ASSERT(state != NULL);
804 FinalizablePersistentHandle* ref = 789 FinalizablePersistentHandle* ref =
805 state->weak_persistent_handles().AllocateHandle(); 790 state->weak_persistent_handles().AllocateHandle();
806 ref->set_raw(object); 791 ref->set_raw(object);
807 ref->set_peer(peer); 792 ref->set_peer(peer);
808 ref->set_callback(callback); 793 ref->set_callback(callback);
809 // This may trigger GC, so it must be called last. 794 // This may trigger GC, so it must be called last.
810 ref->SetExternalSize(external_size, isolate); 795 ref->SetExternalSize(external_size, isolate);
811 return ref; 796 return ref;
812 } 797 }
813 798
814 } // namespace dart 799 } // namespace dart
815 800
816 #endif // RUNTIME_VM_DART_API_STATE_H_ 801 #endif // RUNTIME_VM_DART_API_STATE_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_message.cc ('k') | runtime/vm/dart_entry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698