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

Issue 198563002: Experimental: How to implement willFinalize()? (Closed)

Created:
6 years, 9 months ago by tkent
Modified:
6 years, 9 months ago
CC:
blink-reviews, kouhei+heap_chromium.org
Visibility:
Public.

Description

Experimental: How to implement willFinalize()? BUG=none

Patch Set 1 #

Total comments: 3
Unified diffs Side-by-side diffs Delta from patch set Stats (+89 lines, -0 lines) Patch
M Source/heap/HeapTest.cpp View 4 chunks +89 lines, -0 lines 3 comments Download

Messages

Total messages: 9 (0 generated)
zerny-chromium
This pattern should work and I'm ok with adding it to the heap tests. I'm ...
6 years, 9 months ago (2014-03-13 06:41:46 UTC) #1
kouhei (in TOK)
If this is ok, can we reconsider GarbageCollectedDelayedFinalized? It is doing the exactly same thing, ...
6 years, 9 months ago (2014-03-13 06:56:06 UTC) #2
haraken
I think that it would be much better if we could support touch-on-heap-object in destructors ...
6 years, 9 months ago (2014-03-13 07:00:12 UTC) #3
haraken
On 2014/03/13 06:56:06, kouhei wrote: > If this is ok, can we reconsider GarbageCollectedDelayedFinalized? It ...
6 years, 9 months ago (2014-03-13 07:01:57 UTC) #4
zerny-chromium
> > The benefit of this approach over DelayedFinalized is that we don't need > ...
6 years, 9 months ago (2014-03-13 07:09:55 UTC) #5
Mads Ager (chromium)
This makes sense, it is implementing Kouhei's pre death callback using the existing infrastructure. We ...
6 years, 9 months ago (2014-03-13 07:33:27 UTC) #6
Mads Ager (chromium)
There are multiple things that you have to be aware of when writing the code ...
6 years, 9 months ago (2014-03-13 07:42:28 UTC) #7
tkent
ok, I'll: 1. Make a CL for HeapTest.cpp to verify lifetime of on-heap objects. I ...
6 years, 9 months ago (2014-03-13 07:51:30 UTC) #8
Mads Ager (chromium)
6 years, 9 months ago (2014-03-13 07:53:38 UTC) #9
Sounds good! :)


On Thu, Mar 13, 2014 at 8:51 AM, <tkent@chromium.org> wrote:

> Reviewers: zerny-chromium, haraken, Mads Ager (chromium),
>
> Message:
> ok, I'll:
>
> 1. Make a CL for HeapTest.cpp to verify lifetime of on-heap objects.  I
> won't
> add WillFinalized class in this CL to HeapTest.cpp, and won't add anything
> to
> Handle.h.
> 2. Try to resolve the SQLTransactionSync issue with this pattern.
>
>
>
> Description:
> Experimental: How to implement willFinalize()?
>
> BUG=none
>
> Please review this at https://codereview.chromium.org/198563002/
>
> SVN Base: svn://svn.chromium.org/blink/trunk
>
> Affected files (+89, -0 lines):
>   M Source/heap/HeapTest.cpp
>
>
> Index: Source/heap/HeapTest.cpp
> diff --git a/Source/heap/HeapTest.cpp b/Source/heap/HeapTest.cpp
> index 9cd048780a643d7955f177fcf0db06f2ac18d55e..
> bbbb821ea9ff267b008f0ea18c8427f4f3dca0d7 100644
> --- a/Source/heap/HeapTest.cpp
> +++ b/Source/heap/HeapTest.cpp
> @@ -531,6 +531,7 @@ public:
>          m_magic = 0;
>          s_live--;
>      }
> +    bool isLive() const { return m_magic; }
>
>      virtual void trace(Visitor* visitor) { }
>      static unsigned s_live;
> @@ -562,6 +563,13 @@ public:
>
>      void clear() { m_bar.release(); }
>
> +    // willFinalize is called before finalize()/destructor.
> +    void willFinalize()
> +    {
> +        EXPECT_TRUE(m_bar->isLive());
> +        EXPECT_EQ(1u, Bar::s_live);
> +    }
> +
>  private:
>      explicit Baz(Bar* bar)
>          : m_bar(bar)
> @@ -865,6 +873,65 @@ private:
>      WeakMember<Bar> m_weakBar;
>  };
>
> +// FIXME: This should be moved to heap/Handle.h.
> +template <typename T> class Observer : public
> GarbageCollected<Observer<T>
>
>> {
>>
> +public:
> +    static Observer* create(T* data) { return new Observer(data); }
> +
> +    void trace(Visitor* visitor)
> +    {
> +        visitor->registerWeakMembers(this, zapWeakMembers);
> +    }
> +
> +    static void zapWeakMembers(Visitor* visitor, void* self)
> +    {
> +        Observer* o = reinterpret_cast<Observer*>(self);
> +        if (o->m_data && !visitor->isAlive(o->m_data)) {
> +            o->m_data->willFinalize();
> +            o->m_data = 0;
> +        }
> +    }
> +
> +private:
> +    Observer(T* data) : m_data(data) { }
> +    T* m_data;
> +};
> +
> +// This demonstrates how to use Observer<T> as a part object of the
> target.
> +class WillFinalized : public GarbageCollectedFinalized<WillFinalized> {
> +public:
> +    WillFinalized(Bar* bar)
> +        : m_data(2014)
> +        , m_bar(bar)
> +        , m_observer(Observer<WillFinalized>::create(this))
> +    {
> +    }
> +
> +    // willFinalize is called before finalize()/destructor.
> +    void willFinalize()
> +    {
> +        EXPECT_NE(0, m_data);
> +        EXPECT_TRUE(m_bar->isLive());
> +        m_data = 0;
> +    }
> +
> +    ~WillFinalized()
> +    {
> +        EXPECT_EQ(0, m_data);
> +    }
> +
> +    void trace(Visitor* visitor)
> +    {
> +        visitor->trace(m_bar);
> +    }
> +
> +private:
> +    int m_data;
> +    Member<Bar> m_bar;
>
> +    // This is a trick. Use Persistent for Observer<T> to avoid it is
> collected
> +    // with the target.
> +    Persistent<Observer<WillFinalized> > m_observer;
> +};
>
>  class SuperClass;
>
> @@ -2481,6 +2548,28 @@ TEST(HeapTest, WeakMembers)
>      EXPECT_EQ(0u, Bar::s_live); // All gone.
>  }
>
> +TEST(HeapTest, WillFinalize)
> +{
> +    // |o| observes |baz|.
> +    Persistent<Observer<Baz> > o;
> +    {
> +        Baz* baz = Baz::create(Bar::create());
> +        o = Observer<Baz>::create(baz);
> +    }
> +    // Observer doesn't have a strong reference to baz. So baz and its
> member
> +    // will be collected.
> +    Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
> +    EXPECT_EQ(0u, Bar::s_live);
> +
> +    // |wf| observes itself, and calls willFinalize().
> +    {
> +        WillFinalized* wf = new WillFinalized(Bar::create());
> +        wf = 0;
> +    }
> +    Heap::collectGarbage(ThreadState::NoHeapPointersOnStack);
> +    EXPECT_EQ(0u, Bar::s_live);
> +}
> +
>  TEST(HeapTest, Comparisons)
>  {
>      Persistent<Bar> barPersistent = Bar::create();
>
>
>

To unsubscribe from this group and stop receiving emails from it, send an email
to blink-reviews+unsubscribe@chromium.org.

Powered by Google App Engine
This is Rietveld 408576698