| 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/thread.h" | 10 #include "platform/thread.h" |
| 11 #include "platform/utils.h" |
| 11 #include "vm/dart_api_impl.h" | 12 #include "vm/dart_api_impl.h" |
| 12 #include "vm/flags.h" | 13 #include "vm/flags.h" |
| 13 #include "vm/growable_array.h" | 14 #include "vm/growable_array.h" |
| 14 #include "vm/handles.h" | 15 #include "vm/handles.h" |
| 15 #include "vm/object.h" | 16 #include "vm/object.h" |
| 16 #include "vm/os.h" | 17 #include "vm/os.h" |
| 17 #include "vm/raw_object.h" | 18 #include "vm/raw_object.h" |
| 18 #include "vm/visitor.h" | 19 #include "vm/visitor.h" |
| 19 | 20 |
| 20 #include "vm/handles_impl.h" | 21 #include "vm/handles_impl.h" |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 } | 206 } |
| 206 Dart_WeakPersistentHandle apiPrologueHandle() { | 207 Dart_WeakPersistentHandle apiPrologueHandle() { |
| 207 uword addr = reinterpret_cast<uword>(this); | 208 uword addr = reinterpret_cast<uword>(this); |
| 208 return reinterpret_cast<Dart_WeakPersistentHandle>( | 209 return reinterpret_cast<Dart_WeakPersistentHandle>( |
| 209 addr | kPrologueWeakPersistentTag); | 210 addr | kPrologueWeakPersistentTag); |
| 210 } | 211 } |
| 211 Dart_WeakPersistentHandle apiHandle() { | 212 Dart_WeakPersistentHandle apiHandle() { |
| 212 return reinterpret_cast<Dart_WeakPersistentHandle>(this); | 213 return reinterpret_cast<Dart_WeakPersistentHandle>(this); |
| 213 } | 214 } |
| 214 | 215 |
| 216 void SetExternalSize(intptr_t size, Isolate* isolate) { |
| 217 ASSERT(size >= 0); |
| 218 external_size_ = Utils::RoundUp(size, kObjectAlignment); |
| 219 // TODO(koda): On repeated/large external allocations for existing objects, |
| 220 // without any intervening normal allocation, GC will not trigger. |
| 221 isolate->heap()->AllocateExternal(external_size_, SpaceForExternal()); |
| 222 } |
| 223 |
| 224 // Called when the referent becomes unreachable. |
| 225 void UpdateUnreachable(Isolate* isolate, bool is_prologue_weak) { |
| 226 EnsureFreeExternal(isolate); |
| 227 Finalize(isolate, this, is_prologue_weak); |
| 228 } |
| 229 |
| 230 // Called when the referent has moved, potentially between generations. |
| 231 void UpdateRelocated(Heap::Space before, Isolate* isolate) { |
| 232 isolate->heap()->FreeExternal(external_size_, before); |
| 233 isolate->heap()->AllocateExternal(external_size_, SpaceForExternal()); |
| 234 } |
| 235 |
| 236 // Idempotent. Called when the handle is explicitly deleted or the |
| 237 // referent becomes unreachable. |
| 238 void EnsureFreeExternal(Isolate* isolate) { |
| 239 isolate->heap()->FreeExternal(external_size_, SpaceForExternal()); |
| 240 external_size_ = 0; |
| 241 } |
| 242 |
| 243 // Returns the space to charge for the external size. |
| 244 Heap::Space SpaceForExternal() const { |
| 245 // Non-heap and VM-heap objects count as old space here. |
| 246 return (raw_->IsHeapObject() && raw_->IsNewObject()) ? |
| 247 Heap::kNew : Heap::kOld; |
| 248 } |
| 249 |
| 215 static bool IsPrologueWeakPersistentHandle(Dart_WeakPersistentHandle handle) { | 250 static bool IsPrologueWeakPersistentHandle(Dart_WeakPersistentHandle handle) { |
| 216 uword addr = reinterpret_cast<uword>(handle); | 251 uword addr = reinterpret_cast<uword>(handle); |
| 217 return (addr & kWeakPersistentTagMask) == kPrologueWeakPersistentTag; | 252 return (addr & kWeakPersistentTagMask) == kPrologueWeakPersistentTag; |
| 218 } | 253 } |
| 219 static FinalizablePersistentHandle* Cast(Dart_WeakPersistentHandle handle); | 254 static FinalizablePersistentHandle* Cast(Dart_WeakPersistentHandle handle); |
| 255 |
| 256 private: |
| 257 enum { |
| 258 kWeakPersistentTag = 0, |
| 259 kPrologueWeakPersistentTag = 1, |
| 260 kWeakPersistentTagSize = 1, |
| 261 kWeakPersistentTagMask = 1, |
| 262 }; |
| 263 |
| 264 friend class FinalizablePersistentHandles; |
| 265 |
| 266 FinalizablePersistentHandle() |
| 267 : raw_(NULL), |
| 268 peer_(NULL), |
| 269 external_size_(0), |
| 270 callback_(NULL) { } |
| 271 ~FinalizablePersistentHandle() { } |
| 272 |
| 220 static void Finalize(Isolate* isolate, | 273 static void Finalize(Isolate* isolate, |
| 221 FinalizablePersistentHandle* handle, | 274 FinalizablePersistentHandle* handle, |
| 222 bool is_prologue_weak) { | 275 bool is_prologue_weak) { |
| 223 Dart_WeakPersistentHandleFinalizer callback = handle->callback(); | 276 Dart_WeakPersistentHandleFinalizer callback = handle->callback(); |
| 224 if (callback != NULL) { | 277 if (callback != NULL) { |
| 225 void* peer = handle->peer(); | 278 void* peer = handle->peer(); |
| 226 handle->Clear(); | 279 handle->Clear(); |
| 227 Dart_WeakPersistentHandle object = is_prologue_weak ? | 280 Dart_WeakPersistentHandle object = is_prologue_weak ? |
| 228 handle->apiPrologueHandle() : | 281 handle->apiPrologueHandle() : |
| 229 handle->apiHandle(); | 282 handle->apiHandle(); |
| 230 (*callback)(reinterpret_cast<Dart_Isolate>(isolate), object, peer); | 283 (*callback)(reinterpret_cast<Dart_Isolate>(isolate), object, peer); |
| 231 } else { | 284 } else { |
| 232 handle->Clear(); | 285 handle->Clear(); |
| 233 } | 286 } |
| 234 } | 287 } |
| 235 | 288 |
| 236 private: | |
| 237 enum { | |
| 238 kWeakPersistentTag = 0, | |
| 239 kPrologueWeakPersistentTag = 1, | |
| 240 kWeakPersistentTagSize = 1, | |
| 241 kWeakPersistentTagMask = 1, | |
| 242 }; | |
| 243 | |
| 244 friend class FinalizablePersistentHandles; | |
| 245 | |
| 246 FinalizablePersistentHandle() : raw_(NULL), peer_(NULL), callback_(NULL) { } | |
| 247 ~FinalizablePersistentHandle() { } | |
| 248 | |
| 249 // Overload the raw_ field as a next pointer when adding freed | 289 // Overload the raw_ field as a next pointer when adding freed |
| 250 // handles to the free list. | 290 // handles to the free list. |
| 251 FinalizablePersistentHandle* Next() { | 291 FinalizablePersistentHandle* Next() { |
| 252 return reinterpret_cast<FinalizablePersistentHandle*>(raw_); | 292 return reinterpret_cast<FinalizablePersistentHandle*>(raw_); |
| 253 } | 293 } |
| 254 void SetNext(FinalizablePersistentHandle* free_list) { | 294 void SetNext(FinalizablePersistentHandle* free_list) { |
| 255 raw_ = reinterpret_cast<RawObject*>(free_list); | 295 raw_ = reinterpret_cast<RawObject*>(free_list); |
| 256 ASSERT(!raw_->IsHeapObject()); | 296 ASSERT(!raw_->IsHeapObject()); |
| 257 } | 297 } |
| 258 void FreeHandle(FinalizablePersistentHandle* free_list) { | 298 void FreeHandle(FinalizablePersistentHandle* free_list) { |
| 259 Clear(); | 299 Clear(); |
| 260 SetNext(free_list); | 300 SetNext(free_list); |
| 261 } | 301 } |
| 262 | 302 |
| 263 void Clear() { | 303 void Clear() { |
| 264 raw_ = Object::null(); | 304 raw_ = Object::null(); |
| 265 peer_ = NULL; | 305 peer_ = NULL; |
| 306 external_size_ = 0; |
| 266 callback_ = NULL; | 307 callback_ = NULL; |
| 267 } | 308 } |
| 268 | 309 |
| 269 RawObject* raw_; | 310 RawObject* raw_; |
| 270 void* peer_; | 311 void* peer_; |
| 312 intptr_t external_size_; |
| 271 Dart_WeakPersistentHandleFinalizer callback_; | 313 Dart_WeakPersistentHandleFinalizer callback_; |
| 272 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. | 314 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. |
| 273 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); | 315 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); |
| 274 }; | 316 }; |
| 275 | 317 |
| 276 | 318 |
| 277 // Local handles repository structure. | 319 // Local handles repository structure. |
| 278 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; | 320 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; |
| 279 static const int kLocalHandlesPerChunk = 64; | 321 static const int kLocalHandlesPerChunk = 64; |
| 280 static const int kOffsetOfRawPtrInLocalHandle = 0; | 322 static const int kOffsetOfRawPtrInLocalHandle = 0; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 // by calling FreeHandle. | 505 // by calling FreeHandle. |
| 464 FinalizablePersistentHandle* AllocateHandle() { | 506 FinalizablePersistentHandle* AllocateHandle() { |
| 465 FinalizablePersistentHandle* handle; | 507 FinalizablePersistentHandle* handle; |
| 466 if (free_list_ != NULL) { | 508 if (free_list_ != NULL) { |
| 467 handle = free_list_; | 509 handle = free_list_; |
| 468 free_list_ = handle->Next(); | 510 free_list_ = handle->Next(); |
| 469 } else { | 511 } else { |
| 470 handle = reinterpret_cast<FinalizablePersistentHandle*>( | 512 handle = reinterpret_cast<FinalizablePersistentHandle*>( |
| 471 AllocateScopedHandle()); | 513 AllocateScopedHandle()); |
| 472 } | 514 } |
| 473 handle->set_callback(NULL); | 515 handle->Clear(); |
| 474 return handle; | 516 return handle; |
| 475 } | 517 } |
| 476 | 518 |
| 477 void FreeHandle(FinalizablePersistentHandle* handle) { | 519 void FreeHandle(FinalizablePersistentHandle* handle) { |
| 478 handle->FreeHandle(free_list()); | 520 handle->FreeHandle(free_list()); |
| 479 set_free_list(handle); | 521 set_free_list(handle); |
| 480 } | 522 } |
| 481 | 523 |
| 482 // Validate if passed in handle is a Persistent Handle. | 524 // Validate if passed in handle is a Persistent Handle. |
| 483 bool IsValidHandle(Dart_WeakPersistentHandle object) const { | 525 bool IsValidHandle(Dart_WeakPersistentHandle object) const { |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 ApiNativeScope::Current()->zone()) {} | 858 ApiNativeScope::Current()->zone()) {} |
| 817 ApiGrowableArray() | 859 ApiGrowableArray() |
| 818 : BaseGrowableArray<T, ValueObject>( | 860 : BaseGrowableArray<T, ValueObject>( |
| 819 ApiNativeScope::Current()->zone()) {} | 861 ApiNativeScope::Current()->zone()) {} |
| 820 }; | 862 }; |
| 821 | 863 |
| 822 | 864 |
| 823 } // namespace dart | 865 } // namespace dart |
| 824 | 866 |
| 825 #endif // VM_DART_API_STATE_H_ | 867 #endif // VM_DART_API_STATE_H_ |
| OLD | NEW |