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

Side by Side Diff: third_party/WebKit/Source/platform/heap/ThreadState.h

Issue 2565983002: Implicit prefinalizer registration. (Closed)
Patch Set: recast registration class Created 4 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
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // HeapHashMap<WeakMember<Foo>, std::unique_ptr<Disposer>> idiom requires an 84 // HeapHashMap<WeakMember<Foo>, std::unique_ptr<Disposer>> idiom requires an
85 // assumption that the HeapHashMap outlives objects pointed by WeakMembers. 85 // assumption that the HeapHashMap outlives objects pointed by WeakMembers.
86 // FIXME: Replace all of the 86 // FIXME: Replace all of the
87 // HeapHashMap<WeakMember<Foo>, std::unique_ptr<Disposer>> idiom usages with the 87 // HeapHashMap<WeakMember<Foo>, std::unique_ptr<Disposer>> idiom usages with the
88 // pre-finalizer if the replacement won't cause performance regressions. 88 // pre-finalizer if the replacement won't cause performance regressions.
89 // 89 //
90 // Usage: 90 // Usage:
91 // 91 //
92 // class Foo : GarbageCollected<Foo> { 92 // class Foo : GarbageCollected<Foo> {
93 // USING_PRE_FINALIZER(Foo, dispose); 93 // USING_PRE_FINALIZER(Foo, dispose);
94 // public:
95 // Foo()
96 // {
97 // ThreadState::current()->registerPreFinalizer(this);
98 // }
99 // private: 94 // private:
100 // void dispose() 95 // void dispose()
101 // { 96 // {
102 // m_bar->...; // It is safe to touch other on-heap objects. 97 // m_bar->...; // It is safe to touch other on-heap objects.
103 // } 98 // }
104 // Member<Bar> m_bar; 99 // Member<Bar> m_bar;
105 // }; 100 // };
106 #define USING_PRE_FINALIZER(Class, preFinalizer) \ 101 #define USING_PRE_FINALIZER(Class, preFinalizer) \
107 public: \ 102 public: \
108 static bool invokePreFinalizer(void* object) { \ 103 static bool invokePreFinalizer(void* object) { \
109 Class* self = reinterpret_cast<Class*>(object); \ 104 Class* self = reinterpret_cast<Class*>(object); \
110 if (ThreadHeap::isHeapObjectAlive(self)) \ 105 if (ThreadHeap::isHeapObjectAlive(self)) \
111 return false; \ 106 return false; \
112 self->Class::preFinalizer(); \ 107 self->Class::preFinalizer(); \
113 return true; \ 108 return true; \
114 } \ 109 } \
110 \
111 private: \
112 ThreadState::PrefinalizerRegistration<Class> m_prefinalizerDummy = this; \
115 using UsingPreFinalizerMacroNeedsTrailingSemiColon = char 113 using UsingPreFinalizerMacroNeedsTrailingSemiColon = char
116 114
117 class PLATFORM_EXPORT ThreadState { 115 class PLATFORM_EXPORT ThreadState {
118 USING_FAST_MALLOC(ThreadState); 116 USING_FAST_MALLOC(ThreadState);
119 WTF_MAKE_NONCOPYABLE(ThreadState); 117 WTF_MAKE_NONCOPYABLE(ThreadState);
120 118
121 public: 119 public:
122 typedef std::pair<void*, PreFinalizerCallback> PreFinalizer; 120 typedef std::pair<void*, PreFinalizerCallback> PreFinalizer;
123 121
124 // See setGCState() for possible state transitions. 122 // See setGCState() for possible state transitions.
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 Vector<size_t> liveSize; 406 Vector<size_t> liveSize;
409 Vector<size_t> deadSize; 407 Vector<size_t> deadSize;
410 }; 408 };
411 409
412 void pushThreadLocalWeakCallback(void*, WeakCallback); 410 void pushThreadLocalWeakCallback(void*, WeakCallback);
413 bool popAndInvokeThreadLocalWeakCallback(Visitor*); 411 bool popAndInvokeThreadLocalWeakCallback(Visitor*);
414 void threadLocalWeakProcessing(); 412 void threadLocalWeakProcessing();
415 413
416 size_t objectPayloadSizeForTesting(); 414 size_t objectPayloadSizeForTesting();
417 415
418 // Register the pre-finalizer for the |self| object. This method is normally 416 // TODO: no longer needed, remove all uses.
419 // called in the constructor of the |self| object. The class T must have
420 // USING_PRE_FINALIZER().
421 template <typename T> 417 template <typename T>
422 void registerPreFinalizer(T* self) { 418 void registerPreFinalizer(T* self) {
423 static_assert(sizeof(&T::invokePreFinalizer) > 0,
424 "USING_PRE_FINALIZER(T) must be defined.");
425 ASSERT(checkThread());
426 ASSERT(!sweepForbidden());
427 ASSERT(!m_orderedPreFinalizers.contains(
428 PreFinalizer(self, T::invokePreFinalizer)));
429 m_orderedPreFinalizers.add(PreFinalizer(self, T::invokePreFinalizer));
430 }
431
432 // Unregister the pre-finalizer for the |self| object.
433 template <typename T>
434 void unregisterPreFinalizer(T* self) {
435 static_assert(sizeof(&T::invokePreFinalizer) > 0,
436 "USING_PRE_FINALIZER(T) must be defined.");
437 ASSERT(checkThread());
438 // Ignore pre-finalizers called during pre-finalizers or destructors.
439 if (sweepForbidden())
440 return;
441 ASSERT(m_orderedPreFinalizers.contains(
442 PreFinalizer(self, T::invokePreFinalizer)));
443 m_orderedPreFinalizers.remove(PreFinalizer(self, &T::invokePreFinalizer));
444 } 419 }
445 420
446 void shouldFlushHeapDoesNotContainCache() { 421 void shouldFlushHeapDoesNotContainCache() {
447 m_shouldFlushHeapDoesNotContainCache = true; 422 m_shouldFlushHeapDoesNotContainCache = true;
448 } 423 }
449 424
450 void registerTraceDOMWrappers( 425 void registerTraceDOMWrappers(
451 v8::Isolate* isolate, 426 v8::Isolate* isolate,
452 void (*traceDOMWrappers)(v8::Isolate*, Visitor*), 427 void (*traceDOMWrappers)(v8::Isolate*, Visitor*),
453 void (*invalidateDeadObjectsInWrappersMarkingDeque)(v8::Isolate*), 428 void (*invalidateDeadObjectsInWrappersMarkingDeque)(v8::Isolate*),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 void increaseMarkedObjectSize(size_t); 533 void increaseMarkedObjectSize(size_t);
559 534
560 void callThreadShutdownHooks(); 535 void callThreadShutdownHooks();
561 536
562 v8::Isolate* isolate() const { return m_isolate; } 537 v8::Isolate* isolate() const { return m_isolate; }
563 538
564 void collectGarbage(BlinkGC::StackState, BlinkGC::GCType, BlinkGC::GCReason); 539 void collectGarbage(BlinkGC::StackState, BlinkGC::GCType, BlinkGC::GCReason);
565 void collectGarbageForTerminatingThread(); 540 void collectGarbageForTerminatingThread();
566 void collectAllGarbage(); 541 void collectAllGarbage();
567 542
543 // Register the pre-finalizer for the |self| object. The class T must have
544 // USING_PRE_FINALIZER().
545 template <typename T>
546 class PrefinalizerRegistration final {
547 public:
548 PrefinalizerRegistration(T* self) {
549 static_assert(sizeof(&T::invokePreFinalizer) > 0,
550 "USING_PRE_FINALIZER(T) must be defined.");
551 ThreadState* state = ThreadState::current();
552 #if ENABLE(ASSERT)
553 DCHECK(state->checkThread());
554 #endif
555 DCHECK(!state->sweepForbidden());
556 DCHECK(!state->m_orderedPreFinalizers.contains(
557 PreFinalizer(self, T::invokePreFinalizer)));
558 state->m_orderedPreFinalizers.add(
559 PreFinalizer(self, T::invokePreFinalizer));
560 }
561 };
562
568 private: 563 private:
564 template <typename T>
565 friend class PrefinalizerRegistration;
569 enum SnapshotType { HeapSnapshot, FreelistSnapshot }; 566 enum SnapshotType { HeapSnapshot, FreelistSnapshot };
570 567
571 explicit ThreadState(BlinkGC::ThreadHeapMode); 568 explicit ThreadState(BlinkGC::ThreadHeapMode);
572 ~ThreadState(); 569 ~ThreadState();
573 570
574 NO_SANITIZE_ADDRESS void copyStackUntilSafePointScope(); 571 NO_SANITIZE_ADDRESS void copyStackUntilSafePointScope();
575 void clearSafePointScopeMarker() { 572 void clearSafePointScopeMarker() {
576 m_safePointStackCopy.clear(); 573 m_safePointStackCopy.clear();
577 m_safePointScopeMarker = nullptr; 574 m_safePointScopeMarker = nullptr;
578 } 575 }
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 class ThreadStateFor<AnyThread> { 749 class ThreadStateFor<AnyThread> {
753 STATIC_ONLY(ThreadStateFor); 750 STATIC_ONLY(ThreadStateFor);
754 751
755 public: 752 public:
756 static ThreadState* state() { return ThreadState::current(); } 753 static ThreadState* state() { return ThreadState::current(); }
757 }; 754 };
758 755
759 } // namespace blink 756 } // namespace blink
760 757
761 #endif // ThreadState_h 758 #endif // ThreadState_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/heap/HeapTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698