qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Barabash <alexander_barabash@mentor.com>
To: Anthony Liguori <anthony@codemonkey.ws>
Cc: pbonzini@redhat.com, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] qom: Make object_unref() free the object's memory when refcount goes to 0.
Date: Sun, 26 Feb 2012 11:54:02 +0200	[thread overview]
Message-ID: <4F4A013A.7000403@mentor.com> (raw)
In-Reply-To: <4F47A886.6010309@codemonkey.ws>

On 02/24/2012 05:11 PM, Anthony Liguori wrote:
> On 02/23/2012 10:21 AM, Alexander Barabash wrote:
>> On 02/22/2012 09:12 PM, Anthony Liguori wrote:
>>> On 02/22/2012 12:00 PM, alexander_barabash@mentor.com wrote:
>>>> From: Alexander Barabash<alexander_barabash@mentor.com>
>>>>
>> Why do you want to have a delete notifier list, rather than just a 
>> delete callback.
>
> Because a notifier list allows for third parties to receive the event 
> (think GObject signal/slots).
This is a valid point, but wouldn't it logical to issue an event before 
running the destructor?
Along the lines:

void object_finalize(void *data)
{
     Object *obj = data;
     TypeImpl *ti = obj->class->type;

     object_deinit(obj, ti);
     object_property_del_all(obj);

     g_assert(obj->ref == 0);

     object_finalized_notification(obj);
}

...

void object_unref(Object *obj)
{
     g_assert(obj->ref>  0);
     if (obj->ref == 1) {
         object_is_about_to_be_finalized_notification(obj);
     }
      obj->ref--;

     /* parent always holds a reference to its children */
     if (obj->ref == 0) {
         object_finalize(obj);
     }
}

Here, there is a notification while the object is still alive (in the 
sense that it has not been finalized).
Then, if the object is actually finalized, there is notification about that.

By the way, using weak references would spare us the notification list.
Object's memory will not be freed as long as a weak reference to it exists.
Access through a weak reference to a dead object will remove that weak 
reference.
This way, we shall also avoid problems with circular references between 
objects.

Regards,
Alex


>
>> At the point where refcount == 0, the destructor has been called 
>> already,
>> so there is not much to be done, except for reclaim the memory.
>
> Right, but the memory is not allocated by the core of Object.  This is 
> important in order to allow in-place object creation.  You could 
> special case this and have a flag to indicate whether the object has 
> allocated it's own memory or not but I think the two approaches end up 
> having equal complexity whereas the NotifierList gives you a lot more 
> flexibility.
>
> It makes it possible to use a small object allocator for Objects which 
> could be useful one day if we use objects in a fast path (like using 
> Objects to allocate packets in the network layer or requests in the 
> block layer).
>
> Regards,
>
> Anthony Liguori
>

      reply	other threads:[~2012-02-26  9:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-22 18:00 [Qemu-devel] [PATCH] qom: Make object_unref() free the object's memory when refcount goes to 0 alexander_barabash
2012-02-22 19:12 ` Anthony Liguori
2012-02-23 16:21   ` Alexander Barabash
2012-02-24 15:11     ` Anthony Liguori
2012-02-26  9:54       ` Alexander Barabash [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F4A013A.7000403@mentor.com \
    --to=alexander_barabash@mentor.com \
    --cc=anthony@codemonkey.ws \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).