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

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

Issue 187113003: Track external allocated memory for weak persistent handles. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 9 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
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/gc_marker.cc » ('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 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
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);
220 static void Finalize(Isolate* isolate, 255 static void Finalize(Isolate* isolate,
221 FinalizablePersistentHandle* handle, 256 FinalizablePersistentHandle* handle,
222 bool is_prologue_weak) { 257 bool is_prologue_weak) {
siva 2014/03/07 19:53:03 This should now become a private method I suppose
koda 2014/03/07 20:46:54 It was still called by the isolate shutdown code,
223 Dart_WeakPersistentHandleFinalizer callback = handle->callback(); 258 Dart_WeakPersistentHandleFinalizer callback = handle->callback();
224 if (callback != NULL) { 259 if (callback != NULL) {
225 void* peer = handle->peer(); 260 void* peer = handle->peer();
226 handle->Clear(); 261 handle->Clear();
227 Dart_WeakPersistentHandle object = is_prologue_weak ? 262 Dart_WeakPersistentHandle object = is_prologue_weak ?
228 handle->apiPrologueHandle() : 263 handle->apiPrologueHandle() :
229 handle->apiHandle(); 264 handle->apiHandle();
230 (*callback)(reinterpret_cast<Dart_Isolate>(isolate), object, peer); 265 (*callback)(reinterpret_cast<Dart_Isolate>(isolate), object, peer);
231 } else { 266 } else {
232 handle->Clear(); 267 handle->Clear();
233 } 268 }
234 } 269 }
235 270
236 private: 271 private:
237 enum { 272 enum {
238 kWeakPersistentTag = 0, 273 kWeakPersistentTag = 0,
239 kPrologueWeakPersistentTag = 1, 274 kPrologueWeakPersistentTag = 1,
240 kWeakPersistentTagSize = 1, 275 kWeakPersistentTagSize = 1,
241 kWeakPersistentTagMask = 1, 276 kWeakPersistentTagMask = 1,
242 }; 277 };
243 278
244 friend class FinalizablePersistentHandles; 279 friend class FinalizablePersistentHandles;
245 280
246 FinalizablePersistentHandle() : raw_(NULL), peer_(NULL), callback_(NULL) { } 281 FinalizablePersistentHandle()
282 : raw_(NULL),
283 peer_(NULL),
284 external_size_(0),
285 callback_(NULL) { }
247 ~FinalizablePersistentHandle() { } 286 ~FinalizablePersistentHandle() { }
248 287
249 // Overload the raw_ field as a next pointer when adding freed 288 // Overload the raw_ field as a next pointer when adding freed
250 // handles to the free list. 289 // handles to the free list.
251 FinalizablePersistentHandle* Next() { 290 FinalizablePersistentHandle* Next() {
252 return reinterpret_cast<FinalizablePersistentHandle*>(raw_); 291 return reinterpret_cast<FinalizablePersistentHandle*>(raw_);
253 } 292 }
254 void SetNext(FinalizablePersistentHandle* free_list) { 293 void SetNext(FinalizablePersistentHandle* free_list) {
255 raw_ = reinterpret_cast<RawObject*>(free_list); 294 raw_ = reinterpret_cast<RawObject*>(free_list);
256 ASSERT(!raw_->IsHeapObject()); 295 ASSERT(!raw_->IsHeapObject());
257 } 296 }
258 void FreeHandle(FinalizablePersistentHandle* free_list) { 297 void FreeHandle(FinalizablePersistentHandle* free_list) {
259 Clear(); 298 Clear();
260 SetNext(free_list); 299 SetNext(free_list);
261 } 300 }
262 301
263 void Clear() { 302 void Clear() {
264 raw_ = Object::null(); 303 raw_ = Object::null();
265 peer_ = NULL; 304 peer_ = NULL;
305 external_size_ = 0;
266 callback_ = NULL; 306 callback_ = NULL;
267 } 307 }
268 308
269 RawObject* raw_; 309 RawObject* raw_;
270 void* peer_; 310 void* peer_;
311 intptr_t external_size_;
271 Dart_WeakPersistentHandleFinalizer callback_; 312 Dart_WeakPersistentHandleFinalizer callback_;
272 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods. 313 DISALLOW_ALLOCATION(); // Allocated through AllocateHandle methods.
273 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle); 314 DISALLOW_COPY_AND_ASSIGN(FinalizablePersistentHandle);
274 }; 315 };
275 316
276 317
277 // Local handles repository structure. 318 // Local handles repository structure.
278 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize; 319 static const int kLocalHandleSizeInWords = sizeof(LocalHandle) / kWordSize;
279 static const int kLocalHandlesPerChunk = 64; 320 static const int kLocalHandlesPerChunk = 64;
280 static const int kOffsetOfRawPtrInLocalHandle = 0; 321 static const int kOffsetOfRawPtrInLocalHandle = 0;
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 // by calling FreeHandle. 504 // by calling FreeHandle.
464 FinalizablePersistentHandle* AllocateHandle() { 505 FinalizablePersistentHandle* AllocateHandle() {
465 FinalizablePersistentHandle* handle; 506 FinalizablePersistentHandle* handle;
466 if (free_list_ != NULL) { 507 if (free_list_ != NULL) {
467 handle = free_list_; 508 handle = free_list_;
468 free_list_ = handle->Next(); 509 free_list_ = handle->Next();
469 } else { 510 } else {
470 handle = reinterpret_cast<FinalizablePersistentHandle*>( 511 handle = reinterpret_cast<FinalizablePersistentHandle*>(
471 AllocateScopedHandle()); 512 AllocateScopedHandle());
472 } 513 }
473 handle->set_callback(NULL); 514 handle->Clear();
474 return handle; 515 return handle;
475 } 516 }
476 517
477 void FreeHandle(FinalizablePersistentHandle* handle) { 518 void FreeHandle(FinalizablePersistentHandle* handle) {
478 handle->FreeHandle(free_list()); 519 handle->FreeHandle(free_list());
479 set_free_list(handle); 520 set_free_list(handle);
480 } 521 }
481 522
482 // Validate if passed in handle is a Persistent Handle. 523 // Validate if passed in handle is a Persistent Handle.
483 bool IsValidHandle(Dart_WeakPersistentHandle object) const { 524 bool IsValidHandle(Dart_WeakPersistentHandle object) const {
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 ApiNativeScope::Current()->zone()) {} 857 ApiNativeScope::Current()->zone()) {}
817 ApiGrowableArray() 858 ApiGrowableArray()
818 : BaseGrowableArray<T, ValueObject>( 859 : BaseGrowableArray<T, ValueObject>(
819 ApiNativeScope::Current()->zone()) {} 860 ApiNativeScope::Current()->zone()) {}
820 }; 861 };
821 862
822 863
823 } // namespace dart 864 } // namespace dart
824 865
825 #endif // VM_DART_API_STATE_H_ 866 #endif // VM_DART_API_STATE_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/gc_marker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698