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

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

Issue 8380020: Refactor the dart api a bit: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "vm/dart_api_impl.h" 10 #include "vm/dart_api_impl.h"
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 class PersistentHandle { 77 class PersistentHandle {
78 public: 78 public:
79 enum { 79 enum {
80 StrongReference = 0, 80 StrongReference = 0,
81 WeakReference, 81 WeakReference,
82 }; 82 };
83 83
84 // Accessors. 84 // Accessors.
85 RawObject* raw() const { return raw_; } 85 RawObject* raw() const { return raw_; }
86 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } 86 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); }
87 void set_raw(const Object& object) { raw_ = object.raw(); }
87 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); } 88 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); }
88 void* callback() const { return callback_; } 89 void* callback() const { return callback_; }
89 void set_callback(void* value) { callback_ = value; } 90 void set_callback(void* value) { callback_ = value; }
90 intptr_t type() const { return type_; } 91 intptr_t type() const { return type_; }
91 void set_type(intptr_t value) { type_ = value; } 92 void set_type(intptr_t value) { type_ = value; }
92 93
94 private:
95 friend class PersistentHandles;
96
97 PersistentHandle() { }
98 ~PersistentHandle() { }
99
93 // Overload the callback_ field as a next pointer when adding freed 100 // Overload the callback_ field as a next pointer when adding freed
94 // handles to the free list. 101 // handles to the free list.
95 PersistentHandle* Next() { 102 PersistentHandle* Next() {
96 return reinterpret_cast<PersistentHandle*>(callback_); 103 return reinterpret_cast<PersistentHandle*>(callback_);
97 } 104 }
98 void SetNext(PersistentHandle* free_list) { 105 void SetNext(PersistentHandle* free_list) {
99 callback_ = reinterpret_cast<void*>(free_list); 106 callback_ = reinterpret_cast<void*>(free_list);
100 } 107 }
101 void FreeHandle(PersistentHandle* free_list) { 108 void FreeHandle(PersistentHandle* free_list) {
102 raw_ = NULL; 109 raw_ = NULL;
103 SetNext(free_list); 110 SetNext(free_list);
104 } 111 }
105 112
106 private:
107 PersistentHandle() { }
108 ~PersistentHandle() { }
109
110 RawObject* raw_; 113 RawObject* raw_;
111 void* callback_; 114 void* callback_;
112 intptr_t type_; 115 intptr_t type_;
113 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 116 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
114 DISALLOW_COPY_AND_ASSIGN(PersistentHandle); 117 DISALLOW_COPY_AND_ASSIGN(PersistentHandle);
115 }; 118 };
116 119
117 120
118 // Local handles repository structure. 121 // Local handles repository structure.
119 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; 122 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 handle = free_list_; 197 handle = free_list_;
195 free_list_ = handle->Next(); 198 free_list_ = handle->Next();
196 } else { 199 } else {
197 handle = reinterpret_cast<PersistentHandle*>(AllocateScopedHandle()); 200 handle = reinterpret_cast<PersistentHandle*>(AllocateScopedHandle());
198 } 201 }
199 handle->set_callback(NULL); 202 handle->set_callback(NULL);
200 handle->set_type(PersistentHandle::StrongReference); 203 handle->set_type(PersistentHandle::StrongReference);
201 return handle; 204 return handle;
202 } 205 }
203 206
207 void FreeHandle(PersistentHandle* handle) {
208 handle->FreeHandle(free_list());
209 set_free_list(handle);
210 }
211
204 // Validate if passed in handle is a Persistent Handle. 212 // Validate if passed in handle is a Persistent Handle.
205 bool IsValidHandle(Dart_Handle object) const { 213 bool IsValidHandle(Dart_Handle object) const {
206 return IsValidScopedHandle(reinterpret_cast<uword>(object)); 214 return IsValidScopedHandle(reinterpret_cast<uword>(object));
207 } 215 }
208 216
209 // Returns a count of active handles (used for testing purposes). 217 // Returns a count of active handles (used for testing purposes).
210 int CountHandles() const { 218 int CountHandles() const {
211 return CountScopedHandles(); 219 return CountScopedHandles();
212 } 220 }
213 221
(...skipping 28 matching lines...) Expand all
242 250
243 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope); 251 DISALLOW_COPY_AND_ASSIGN(ApiLocalScope);
244 }; 252 };
245 253
246 254
247 // Implementation of the API State used in dart api for maintaining 255 // Implementation of the API State used in dart api for maintaining
248 // local scopes, persistent handles etc. These are setup on a per isolate 256 // local scopes, persistent handles etc. These are setup on a per isolate
249 // basis and destroyed when the isolate is shutdown. 257 // basis and destroyed when the isolate is shutdown.
250 class ApiState { 258 class ApiState {
251 public: 259 public:
252 ApiState() : top_scope_(NULL) { } 260 ApiState() : top_scope_(NULL), true_(NULL) { }
253 ~ApiState() { 261 ~ApiState() {
254 while (top_scope_ != NULL) { 262 while (top_scope_ != NULL) {
255 ApiLocalScope* scope = top_scope_; 263 ApiLocalScope* scope = top_scope_;
256 top_scope_ = top_scope_->previous(); 264 top_scope_ = top_scope_->previous();
257 delete scope; 265 delete scope;
258 } 266 }
267 if (true_ != NULL) {
268 persistent_handles().FreeHandle(true_);
269 true_ = NULL;
270 }
259 } 271 }
260 272
261 // Accessors. 273 // Accessors.
262 ApiLocalScope* top_scope() const { return top_scope_; } 274 ApiLocalScope* top_scope() const { return top_scope_; }
263 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } 275 void set_top_scope(ApiLocalScope* value) { top_scope_ = value; }
264 PersistentHandles& persistent_handles() { return persistent_handles_; } 276 PersistentHandles& persistent_handles() { return persistent_handles_; }
265 277
266 void UnwindScopes(uword sp) { 278 void UnwindScopes(uword sp) {
267 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { 279 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) {
268 ApiLocalScope* scope = top_scope_; 280 ApiLocalScope* scope = top_scope_;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 } 320 }
309 int ZoneSizeInBytes() const { 321 int ZoneSizeInBytes() const {
310 int total = 0; 322 int total = 0;
311 ApiLocalScope* scope = top_scope_; 323 ApiLocalScope* scope = top_scope_;
312 while (scope != NULL) { 324 while (scope != NULL) {
313 total += scope->zone().SizeInBytes(); 325 total += scope->zone().SizeInBytes();
314 scope = scope->previous(); 326 scope = scope->previous();
315 } 327 }
316 return total; 328 return total;
317 } 329 }
330 PersistentHandle* True() {
331 if (true_ == NULL) {
332 Zone zone; // Setup a VM zone as we are creating some handles.
333 HandleScope scope; // Setup a VM handle scope.
334
335 const Object& true_object = Object::Handle(Bool::True());
336 true_ = persistent_handles().AllocateHandle();
337 true_->set_raw(true_object);
338 }
339 return true_;
340 }
318 341
319 private: 342 private:
320 PersistentHandles persistent_handles_; 343 PersistentHandles persistent_handles_;
321 ApiLocalScope* top_scope_; 344 ApiLocalScope* top_scope_;
322 345
346 // A persistent handle to the "True" object.
347 PersistentHandle* true_;
348
323 DISALLOW_COPY_AND_ASSIGN(ApiState); 349 DISALLOW_COPY_AND_ASSIGN(ApiState);
324 }; 350 };
325 351
326 } // namespace dart 352 } // namespace dart
327 353
328 #endif // VM_DART_API_STATE_H_ 354 #endif // VM_DART_API_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698