+ vlc_object_internals_t *p_priv = vlc_internals( p_this );
+ int i_new;
+
+ vlc_mutex_lock( &p_priv->var_lock );
+
+ /* FIXME: if the variable already exists, we don't duplicate it. But we
+ * duplicate the lookups. It's not that serious, but if anyone finds some
+ * time to rework Insert() so that only one lookup has to be done, feel
+ * free to do so. */
+ i_new = Lookup( p_priv->pp_vars, p_priv->i_vars, psz_name );
+
+ if( i_new >= 0 )
+ {
+ /* If the types differ, variable creation failed. */
+ if( (i_type & VLC_VAR_CLASS) != (p_priv->pp_vars[i_new]->i_type & VLC_VAR_CLASS) )
+ {
+ msg_Err( p_this, "Variable '%s' (0x%04x) already exist but with a different type (0x%04x)",
+ psz_name, p_priv->pp_vars[i_new]->i_type, i_type );
+ vlc_mutex_unlock( &p_priv->var_lock );
+ return VLC_EBADVAR;
+ }
+
+ p_priv->pp_vars[i_new]->i_usage++;
+ p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_ISCOMMAND );
+ p_priv->pp_vars[i_new]->i_type |= ( i_type & VLC_VAR_HASCHOICE );
+ vlc_mutex_unlock( &p_priv->var_lock );
+
+ /* We did not need to create a new variable, free everything... */
+ Destroy( p_var );
+ return VLC_SUCCESS;
+ }
+
+ i_new = Insert( p_priv->pp_vars, p_priv->i_vars, psz_name );
+
+ if( (p_priv->i_vars & 15) == 0 )
+ p_priv->pp_vars = xrealloc( p_priv->pp_vars,
+ (p_priv->i_vars+16) * sizeof(variable_t *) );
+
+ memmove( p_priv->pp_vars + i_new + 1,
+ p_priv->pp_vars + i_new,
+ (p_priv->i_vars - i_new) * sizeof(variable_t *) );
+
+ p_priv->i_vars++;
+
+ p_priv->pp_vars[i_new] = p_var;