[vlc-devel] commit: variables: factorize the callback code, using a special function. ( Rémi Duraffort )

git version control git at videolan.org
Fri Jul 24 10:43:56 CEST 2009


vlc | branch: master | Rémi Duraffort <ivoire at videolan.org> | Fri Jul 24 10:39:05 2009 +0200| [afce97e9b349c18d49f85859053eafdf78b76c67] | committer: Rémi Duraffort 

variables: factorize the callback code, using a special function.
As a side effect, fix a potential memleak if the variable disappeares in
var_SetChecked.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=afce97e9b349c18d49f85859053eafdf78b76c67
---

 src/misc/variables.c |  108 ++++++++++++++++++++++----------------------------
 1 files changed, 48 insertions(+), 60 deletions(-)

diff --git a/src/misc/variables.c b/src/misc/variables.c
index 4d9ae4a..5b9ef21 100644
--- a/src/misc/variables.c
+++ b/src/misc/variables.c
@@ -162,6 +162,8 @@ static void     CheckValue  ( variable_t *, vlc_value_t * );
 
 static int      InheritValue( vlc_object_t *, const char *, vlc_value_t *,
                               int );
+static int      TriggerCallback( vlc_object_t *, variable_t *, const char *,
+                                 vlc_value_t, vlc_value_t );
 
 /**
  * Initialize a vlc variable
@@ -694,6 +696,7 @@ int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
                     int expected_type, vlc_value_t val )
 {
     int i_var;
+    int i_ret = VLC_SUCCESS;
     variable_t *p_var;
     vlc_value_t oldval;
     vlc_object_internals_t *p_priv = vlc_internals( p_this );
@@ -726,42 +729,14 @@ int var_SetChecked( vlc_object_t *p_this, const char *psz_name,
     /* Deal with callbacks. Tell we're in a callback, release the lock,
      * call stored functions, retake the lock. */
     if( p_var->i_entries )
-    {
-        int i_var;
-        int i_entries = p_var->i_entries;
-        callback_entry_t *p_entries = p_var->p_entries;
-
-        p_var->b_incallback = true;
-        vlc_mutex_unlock( &p_priv->var_lock );
-
-        /* The real calls */
-        for( ; i_entries-- ; )
-        {
-            p_entries[i_entries].pf_callback( p_this, psz_name, oldval, val,
-                                              p_entries[i_entries].p_data );
-        }
-
-        vlc_mutex_lock( &p_priv->var_lock );
-
-        i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
-        if( i_var < 0 )
-        {
-            msg_Err( p_this, "variable %s has disappeared", psz_name );
-            vlc_mutex_unlock( &p_priv->var_lock );
-            return VLC_ENOVAR;
-        }
-
-        p_var = &p_priv->p_vars[i_var];
-        p_var->b_incallback = false;
-        vlc_cond_broadcast( &p_priv->var_wait );
-    }
+        i_ret = TriggerCallback( p_this, p_var, psz_name, oldval, val );
 
     /* Free data if needed */
     p_var->ops->pf_free( &oldval );
 
     vlc_mutex_unlock( &p_priv->var_lock );
 
-    return VLC_SUCCESS;
+    return i_ret;
 }
 
 
@@ -922,6 +897,7 @@ int __var_DelCallback( vlc_object_t *p_this, const char *psz_name,
 int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
 {
     int i_var;
+    int i_ret = VLC_SUCCESS;
     variable_t *p_var;
     vlc_value_t oldval;
     vlc_object_internals_t *p_priv = vlc_internals( p_this );
@@ -943,38 +919,10 @@ int __var_TriggerCallback( vlc_object_t *p_this, const char *psz_name )
     /* Deal with callbacks. Tell we're in a callback, release the lock,
      * call stored functions, retake the lock. */
     if( p_var->i_entries )
-    {
-        int i_var;
-        int i_entries = p_var->i_entries;
-        callback_entry_t *p_entries = p_var->p_entries;
-
-        p_var->b_incallback = true;
-        vlc_mutex_unlock( &p_priv->var_lock );
-
-        /* The real calls */
-        for( ; i_entries-- ; )
-        {
-            p_entries[i_entries].pf_callback( p_this, psz_name, oldval, oldval,
-                                              p_entries[i_entries].p_data );
-        }
-
-        vlc_mutex_lock( &p_priv->var_lock );
-
-        i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
-        if( i_var < 0 )
-        {
-            msg_Err( p_this, "variable %s has disappeared", psz_name );
-            vlc_mutex_unlock( &p_priv->var_lock );
-            return VLC_ENOVAR;
-        }
-
-        p_var = &p_priv->p_vars[i_var];
-        p_var->b_incallback = false;
-        vlc_cond_broadcast( &p_priv->var_wait );
-    }
+        i_ret = TriggerCallback( p_this, p_var, psz_name, oldval, oldval );
 
     vlc_mutex_unlock( &p_priv->var_lock );
-    return VLC_SUCCESS;
+    return i_ret;
 }
 
 /** Parse a stringified option
@@ -1464,6 +1412,46 @@ static int InheritValue( vlc_object_t *p_this, const char *psz_name,
     return VLC_SUCCESS;
 }
 
+
+/**********************************************************************
+ * Trigger the callbacks
+ **********************************************************************/
+static int TriggerCallback( vlc_object_t *p_this, variable_t *p_var,
+                            const char *psz_name, vlc_value_t oldval,
+                            vlc_value_t newval )
+{
+    int i_var;
+    int i_entries = p_var->i_entries;
+    callback_entry_t *p_entries = p_var->p_entries;
+    vlc_object_internals_t *p_priv = vlc_internals( p_this );
+
+    p_var->b_incallback = true;
+    vlc_mutex_unlock( &p_priv->var_lock );
+
+    /* The real calls */
+    for( ; i_entries-- ; )
+    {
+        p_entries[i_entries].pf_callback( p_this, psz_name, oldval, newval,
+                                          p_entries[i_entries].p_data );
+    }
+
+    vlc_mutex_lock( &p_priv->var_lock );
+
+    i_var = Lookup( p_priv->p_vars, p_priv->i_vars, psz_name );
+    if( i_var < 0 )
+    {
+        msg_Err( p_this, "variable %s has disappeared", psz_name );
+        return VLC_ENOVAR;
+     }
+
+     p_var = &p_priv->p_vars[i_var];
+     p_var->b_incallback = false;
+     vlc_cond_broadcast( &p_priv->var_wait );
+
+     return VLC_SUCCESS;
+}
+
+
 /**********************************************************************
  * Execute a var command on an object identified by its name
  **********************************************************************/




More information about the vlc-devel mailing list