OLD | NEW |
1 // Copyright (c) 2011, 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 "vm/dart_api_impl.h" | 11 #include "vm/dart_api_impl.h" |
11 #include "vm/flags.h" | 12 #include "vm/flags.h" |
| 13 #include "vm/growable_array.h" |
12 #include "vm/handles.h" | 14 #include "vm/handles.h" |
13 #include "vm/object.h" | 15 #include "vm/object.h" |
14 #include "vm/os.h" | 16 #include "vm/os.h" |
15 #include "vm/raw_object.h" | 17 #include "vm/raw_object.h" |
16 #include "vm/visitor.h" | 18 #include "vm/visitor.h" |
17 | 19 |
18 #include "vm/handles_impl.h" | 20 #include "vm/handles_impl.h" |
19 | 21 |
20 namespace dart { | 22 namespace dart { |
21 | 23 |
(...skipping 17 matching lines...) Expand all Loading... |
39 // 'data' into new allocated memory. Uses current zone. | 41 // 'data' into new allocated memory. Uses current zone. |
40 uword Reallocate(uword data, intptr_t old_size, intptr_t new_size) { | 42 uword Reallocate(uword data, intptr_t old_size, intptr_t new_size) { |
41 return zone_.Reallocate(data, old_size, new_size); | 43 return zone_.Reallocate(data, old_size, new_size); |
42 } | 44 } |
43 | 45 |
44 // Compute the total size of this zone. This includes wasted space that is | 46 // Compute the total size of this zone. This includes wasted space that is |
45 // due to internal fragmentation in the segments. | 47 // due to internal fragmentation in the segments. |
46 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } | 48 intptr_t SizeInBytes() const { return zone_.SizeInBytes(); } |
47 | 49 |
48 private: | 50 private: |
| 51 BaseZone* GetBaseZone() { return &zone_; } |
| 52 |
49 BaseZone zone_; | 53 BaseZone zone_; |
50 | 54 |
| 55 template<typename T> friend class ApiGrowableArray; |
51 DISALLOW_COPY_AND_ASSIGN(ApiZone); | 56 DISALLOW_COPY_AND_ASSIGN(ApiZone); |
52 }; | 57 }; |
53 | 58 |
54 | 59 |
55 // Implementation of local handles which are handed out from every | 60 // Implementation of local handles which are handed out from every |
56 // dart API call, these handles are valid only in the present scope | 61 // dart API call, these handles are valid only in the present scope |
57 // and are destroyed when a Dart_ExitScope() is called. | 62 // and are destroyed when a Dart_ExitScope() is called. |
58 class LocalHandle { | 63 class LocalHandle { |
59 public: | 64 public: |
60 // Accessors. | 65 // Accessors. |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 ApiLocalScope* top_scope_; | 505 ApiLocalScope* top_scope_; |
501 | 506 |
502 // Persistent handles to important objects. | 507 // Persistent handles to important objects. |
503 PersistentHandle* null_; | 508 PersistentHandle* null_; |
504 PersistentHandle* true_; | 509 PersistentHandle* true_; |
505 PersistentHandle* false_; | 510 PersistentHandle* false_; |
506 | 511 |
507 DISALLOW_COPY_AND_ASSIGN(ApiState); | 512 DISALLOW_COPY_AND_ASSIGN(ApiState); |
508 }; | 513 }; |
509 | 514 |
| 515 |
| 516 class ApiNativeScope { |
| 517 public: |
| 518 ApiNativeScope() { |
| 519 // Currently no support for nesting native scopes. |
| 520 ASSERT(Current() == NULL); |
| 521 Thread::SetThreadLocal(Api::api_native_key_, reinterpret_cast<uword>(this)); |
| 522 } |
| 523 |
| 524 ~ApiNativeScope() { |
| 525 ASSERT(Current() == this); |
| 526 Thread::SetThreadLocal(Api::api_native_key_, NULL); |
| 527 } |
| 528 |
| 529 static inline ApiNativeScope* Current() { |
| 530 return reinterpret_cast<ApiNativeScope*>( |
| 531 Thread::GetThreadLocal(Api::api_native_key_)); |
| 532 } |
| 533 |
| 534 ApiZone* zone() { return &zone_; } |
| 535 |
| 536 private: |
| 537 ApiZone zone_; |
| 538 }; |
| 539 |
| 540 |
| 541 // Api growable arrays use a zone for allocation. The constructor |
| 542 // picks the zone from the current isolate if in an isolate |
| 543 // environment. When outside an isolate environment it picks the zone |
| 544 // from the current native scope. |
| 545 template<typename T> |
| 546 class ApiGrowableArray : public BaseGrowableArray<T, ValueObject> { |
| 547 public: |
| 548 explicit ApiGrowableArray(int initial_capacity) |
| 549 : BaseGrowableArray<T, ValueObject>( |
| 550 initial_capacity, |
| 551 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
| 552 ApiGrowableArray() |
| 553 : BaseGrowableArray<T, ValueObject>( |
| 554 ApiNativeScope::Current()->zone()->GetBaseZone()) {} |
| 555 }; |
| 556 |
| 557 |
510 } // namespace dart | 558 } // namespace dart |
511 | 559 |
512 #endif // VM_DART_API_STATE_H_ | 560 #endif // VM_DART_API_STATE_H_ |
OLD | NEW |