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

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

Issue 8984006: Implement weak persistent handles in the Dart API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix whitespace. Created 9 years 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 74
75 // A distinguished callback which indicates that a persistent handle 75 // A distinguished callback which indicates that a persistent handle
76 // should not be deleted from the dart api. 76 // should not be deleted from the dart api.
77 void ProtectedHandleCallback(); 77 void ProtectedHandleCallback();
78 78
79 79
80 // Implementation of persistent handles which are handed out through the 80 // Implementation of persistent handles which are handed out through the
81 // dart API. 81 // dart API.
82 class PersistentHandle { 82 class PersistentHandle {
83 public: 83 public:
84 enum { 84 enum Kind {
85 StrongReference = 0, 85 StrongReference = 0,
86 WeakReference, 86 WeakReference,
87 }; 87 };
88 88
89 // Adaptor for visiting handles with matching reference kind.
90 class Visitor : public HandleVisitor {
91 public:
92 explicit Visitor(ObjectPointerVisitor* visitor, Kind kind) {
Ivan Posva 2011/12/19 22:41:36 explicit not needed here.
cshapiro 2011/12/20 00:58:57 Right, done.
93 ASSERT(visitor != NULL);
94 kind_ = kind;
95 object_pointer_visitor_ = visitor;
96 }
97
98 void Visit(uword* addr) {
99 PersistentHandle* handle = reinterpret_cast<PersistentHandle*>(addr);
100 if (handle->kind() == kind_) {
101 object_pointer_visitor_->VisitPointer(&handle->raw_);
102 }
103 }
104
105 ~Visitor() {}
106
107 private:
108 Kind kind_;
109 ObjectPointerVisitor* object_pointer_visitor_;
110
111 DISALLOW_COPY_AND_ASSIGN(Visitor);
112 };
113
89 // Accessors. 114 // Accessors.
90 RawObject* raw() const { return raw_; } 115 RawObject* raw() const { return raw_; }
91 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); } 116 void set_raw(const LocalHandle& ref) { raw_ = ref.raw(); }
92 void set_raw(const Object& object) { raw_ = object.raw(); } 117 void set_raw(const Object& object) { raw_ = object.raw(); }
93 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); } 118 static intptr_t raw_offset() { return OFFSET_OF(PersistentHandle, raw_); }
94 void* callback() const { return callback_; } 119 void* callback() const { return callback_; }
95 void set_callback(void* value) { callback_ = value; } 120 void set_callback(void* value) { callback_ = value; }
96 intptr_t type() const { return type_; } 121 Kind kind() const { return kind_; }
97 void set_type(intptr_t value) { type_ = value; } 122 void set_kind(Kind value) { kind_ = value; }
98 123
99 // Some handles are protected from being freed via the external dart api. 124 // Some handles are protected from being freed via the external dart api.
100 bool IsProtected() { 125 bool IsProtected() {
101 return callback() == &ProtectedHandleCallback; 126 return callback() == &ProtectedHandleCallback;
102 } 127 }
103 128
104 private: 129 private:
105 friend class PersistentHandles; 130 friend class PersistentHandles;
106 131
107 PersistentHandle() { } 132 PersistentHandle() { }
108 ~PersistentHandle() { } 133 ~PersistentHandle() { }
109 134
110 // Overload the callback_ field as a next pointer when adding freed 135 // Overload the callback_ field as a next pointer when adding freed
111 // handles to the free list. 136 // handles to the free list.
112 PersistentHandle* Next() { 137 PersistentHandle* Next() {
113 return reinterpret_cast<PersistentHandle*>(callback_); 138 return reinterpret_cast<PersistentHandle*>(callback_);
114 } 139 }
115 void SetNext(PersistentHandle* free_list) { 140 void SetNext(PersistentHandle* free_list) {
116 callback_ = reinterpret_cast<void*>(free_list); 141 callback_ = reinterpret_cast<void*>(free_list);
117 } 142 }
118 void FreeHandle(PersistentHandle* free_list) { 143 void FreeHandle(PersistentHandle* free_list) {
119 raw_ = NULL; 144 raw_ = NULL;
120 SetNext(free_list); 145 SetNext(free_list);
121 } 146 }
122 147
123 RawObject* raw_; 148 RawObject* raw_;
124 void* callback_; 149 void* callback_;
125 intptr_t type_; 150 void* peer_;
151 Kind kind_;
126 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 152 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
127 DISALLOW_COPY_AND_ASSIGN(PersistentHandle); 153 DISALLOW_COPY_AND_ASSIGN(PersistentHandle);
128 }; 154 };
129 155
130 156
131 // Local handles repository structure. 157 // Local handles repository structure.
132 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; 158 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize;
133 static const int kLocalHandlesPerChunk = 64; 159 static const int kLocalHandlesPerChunk = 64;
134 static const int kOffsetOfRawPtrInLocalHandle = 0; 160 static const int kOffsetOfRawPtrInLocalHandle = 0;
135 class LocalHandles : Handles<kLocalHandleSizeInWords, 161 class LocalHandles : Handles<kLocalHandleSizeInWords,
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 PersistentHandle* free_list() const { return free_list_; } 218 PersistentHandle* free_list() const { return free_list_; }
193 void set_free_list(PersistentHandle* value) { free_list_ = value; } 219 void set_free_list(PersistentHandle* value) { free_list_ = value; }
194 220
195 // Visit all object pointers stored in the various handles. 221 // Visit all object pointers stored in the various handles.
196 void VisitObjectPointers(ObjectPointerVisitor* visitor) { 222 void VisitObjectPointers(ObjectPointerVisitor* visitor) {
197 Handles<kPersistentHandleSizeInWords, 223 Handles<kPersistentHandleSizeInWords,
198 kPersistentHandlesPerChunk, 224 kPersistentHandlesPerChunk,
199 kOffsetOfRawPtrInPersistentHandle>::VisitObjectPointers(visitor); 225 kOffsetOfRawPtrInPersistentHandle>::VisitObjectPointers(visitor);
200 } 226 }
201 227
228 // Visits the object pointers in strong persistent handles.
229 void VisitStrongObjectPointers(ObjectPointerVisitor* visitor) {
230 PersistentHandle::Visitor strong_visitor(visitor,
231 PersistentHandle::StrongReference);
232 Handles<kPersistentHandleSizeInWords,
233 kPersistentHandlesPerChunk,
234 kOffsetOfRawPtrInPersistentHandle>::Visit(&strong_visitor);
235 }
236
237 // Visits the object pointers in weak persistent handles.
238 void VisitWeakObjectPointers(ObjectPointerVisitor* visitor) {
239 PersistentHandle::Visitor weak_visitor(visitor,
240 PersistentHandle::WeakReference);
241 Handles<kPersistentHandleSizeInWords,
242 kPersistentHandlesPerChunk,
243 kOffsetOfRawPtrInPersistentHandle>::Visit(&weak_visitor);
244 }
245
202 // Allocates a persistent handle, these have to be destroyed explicitly 246 // Allocates a persistent handle, these have to be destroyed explicitly
203 // by calling FreeHandle. 247 // by calling FreeHandle.
204 PersistentHandle* AllocateHandle() { 248 PersistentHandle* AllocateHandle() {
205 PersistentHandle* handle; 249 PersistentHandle* handle;
206 if (free_list_ != NULL) { 250 if (free_list_ != NULL) {
207 handle = free_list_; 251 handle = free_list_;
208 free_list_ = handle->Next(); 252 free_list_ = handle->Next();
209 } else { 253 } else {
210 handle = reinterpret_cast<PersistentHandle*>(AllocateScopedHandle()); 254 handle = reinterpret_cast<PersistentHandle*>(AllocateScopedHandle());
211 } 255 }
212 handle->set_callback(NULL); 256 handle->set_callback(NULL);
213 handle->set_type(PersistentHandle::StrongReference); 257 handle->set_kind(PersistentHandle::StrongReference);
214 return handle; 258 return handle;
215 } 259 }
216 260
217 void FreeHandle(PersistentHandle* handle) { 261 void FreeHandle(PersistentHandle* handle) {
218 handle->FreeHandle(free_list()); 262 handle->FreeHandle(free_list());
219 set_free_list(handle); 263 set_free_list(handle);
220 } 264 }
221 265
222 // Validate if passed in handle is a Persistent Handle. 266 // Validate if passed in handle is a Persistent Handle.
223 bool IsValidHandle(Dart_Handle object) const { 267 bool IsValidHandle(Dart_Handle object) const {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 PersistentHandles& persistent_handles() { return persistent_handles_; } 338 PersistentHandles& persistent_handles() { return persistent_handles_; }
295 339
296 void UnwindScopes(uword sp) { 340 void UnwindScopes(uword sp) {
297 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { 341 while (top_scope_ != NULL && top_scope_->stack_marker() < sp) {
298 ApiLocalScope* scope = top_scope_; 342 ApiLocalScope* scope = top_scope_;
299 top_scope_ = top_scope_->previous(); 343 top_scope_ = top_scope_->previous();
300 delete scope; 344 delete scope;
301 } 345 }
302 } 346 }
303 347
304 void VisitObjectPointers(ObjectPointerVisitor* visitor) { 348 void VisitStrongObjectPointers(ObjectPointerVisitor* visitor) {
305 ApiLocalScope* scope = top_scope_; 349 ApiLocalScope* scope = top_scope_;
306 while (scope != NULL) { 350 while (scope != NULL) {
307 scope->local_handles()->VisitObjectPointers(visitor); 351 scope->local_handles()->VisitObjectPointers(visitor);
308 scope = scope->previous(); 352 scope = scope->previous();
309 } 353 }
310 persistent_handles().VisitObjectPointers(visitor); 354
355 persistent_handles().VisitStrongObjectPointers(visitor);
356 }
357
358 void VisitWeakObjectPointers(ObjectPointerVisitor* visitor) {
359 persistent_handles().VisitWeakObjectPointers(visitor);
360 }
361
362 void VisitObjectPointers(ObjectPointerVisitor* visitor) {
363 VisitStrongObjectPointers(visitor);
364 VisitWeakObjectPointers(visitor);
311 } 365 }
312 366
313 bool IsValidLocalHandle(Dart_Handle object) const { 367 bool IsValidLocalHandle(Dart_Handle object) const {
314 ApiLocalScope* scope = top_scope_; 368 ApiLocalScope* scope = top_scope_;
315 while (scope != NULL) { 369 while (scope != NULL) {
316 if (scope->local_handles()->IsValidHandle(object)) { 370 if (scope->local_handles()->IsValidHandle(object)) {
317 return true; 371 return true;
318 } 372 }
319 scope = scope->previous(); 373 scope = scope->previous();
320 } 374 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 PersistentHandle* null_; 441 PersistentHandle* null_;
388 PersistentHandle* true_; 442 PersistentHandle* true_;
389 PersistentHandle* false_; 443 PersistentHandle* false_;
390 444
391 DISALLOW_COPY_AND_ASSIGN(ApiState); 445 DISALLOW_COPY_AND_ASSIGN(ApiState);
392 }; 446 };
393 447
394 } // namespace dart 448 } // namespace dart
395 449
396 #endif // VM_DART_API_STATE_H_ 450 #endif // VM_DART_API_STATE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698