From d857d87754cafdb18378fc82f92bb95c7a0be5dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Denis-Courmont?= Date: Wed, 25 May 2011 19:44:57 +0300 Subject: [PATCH] Attach object during creation Attaching an object makes it visible in the hierarchy. This would have been a problem in some cases with vlc_object_find(), but this function was removed. --- src/misc/objects.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/misc/objects.c b/src/misc/objects.c index 2135f2bad2..d250bd5b62 100644 --- a/src/misc/objects.c +++ b/src/misc/objects.c @@ -133,7 +133,8 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length, vlc_spin_init (&priv->ref_spin); priv->i_refcount = 1; priv->pf_destructor = NULL; - priv->prev = priv->next = priv->first = NULL; + priv->prev = NULL; + priv->first = NULL; vlc_object_t *obj = (vlc_object_t *)(priv + 1); obj->psz_object_type = typename; @@ -143,8 +144,21 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length, if (likely(parent != NULL)) { + vlc_object_internals_t *papriv = vlc_internals (parent); + obj->i_flags = parent->i_flags; obj->p_libvlc = parent->p_libvlc; + + /* Attach the child to its parent (no lock needed) */ + obj->p_parent = vlc_object_hold (parent); + + /* Attach the parent to its child (structure lock needed) */ + libvlc_lock (obj->p_libvlc); + priv->next = papriv->first; + if (priv->next != NULL) + priv->next->prev = priv; + papriv->first = priv; + libvlc_unlock (obj->p_libvlc); } else { @@ -152,6 +166,8 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length, obj->i_flags = 0; obj->p_libvlc = self; + obj->p_parent = NULL; + priv->next = NULL; vlc_mutex_init (&(libvlc_priv (self)->structure_lock)); /* TODO: should be in src/libvlc.c */ @@ -162,7 +178,6 @@ void *vlc_custom_create (vlc_object_t *parent, size_t length, var_AddCallback (obj, "vars", DumpCommand, NULL); vlc_restorecancel (canc); } - obj->p_parent = NULL; memset (obj + 1, 0, length - sizeof (*obj)); return obj; @@ -540,27 +555,15 @@ void vlc_object_release( vlc_object_t *p_this ) void vlc_object_attach( vlc_object_t *p_this, vlc_object_t *p_parent ) { if( !p_this ) return; + if( likely(p_this->p_parent == p_parent) ) + return; - vlc_object_internals_t *pap = vlc_internals (p_parent); - vlc_object_internals_t *priv = vlc_internals (p_this); - - priv->prev = NULL; - vlc_object_hold (p_parent); - libvlc_lock (p_this->p_libvlc); - - /* Attach the parent to its child */ - assert (p_this->p_parent == NULL); - p_this->p_parent = p_parent; - - /* Attach the child to its parent */ - priv->next = pap->first; - if (priv->next != NULL) - priv->next->prev = priv; - pap->first = priv; - libvlc_unlock (p_this->p_libvlc); + msg_Err( p_this, "object hierarchy bug:" ); + msg_Err( p_this->p_parent, "created by this object but..." ); + msg_Err( p_parent, "...attached to this object" ); + abort(); } - #undef vlc_list_children /** * Gets the list of children of an objects, and increment their reference -- 2.39.5