* variables.h: variables handling
*****************************************************************************
* Copyright (C) 2002 VideoLAN
- * $Id: variables.h,v 1.2 2002/10/14 16:46:55 sam Exp $
+ * $Id: variables.h,v 1.3 2002/10/14 19:04:51 sam Exp $
*
* Authors: Samuel Hocevar <sam@zoy.org>
*
#define VLC_VAR_TIME 0x0500
#define VLC_VAR_ADDRESS 0x0600
#define VLC_VAR_COMMAND 0x0700
+#define VLC_VAR_MUTEX 0x0800
/*****************************************************************************
* vlc_value_t is the common union for variable values; variable_t is the
vlc_value_t val;
/* Lots of other things that can be added */
+ int i_usage;
vlc_bool_t b_set;
vlc_bool_t b_active;
};
* variables.c: routines for object variables handling
*****************************************************************************
* Copyright (C) 2002 VideoLAN
- * $Id: variables.c,v 1.2 2002/10/14 16:46:56 sam Exp $
+ * $Id: variables.c,v 1.3 2002/10/14 19:04:51 sam Exp $
*
* Authors: Samuel Hocevar <sam@zoy.org>
*
int __var_Create( vlc_object_t *p_this, const char *psz_name, int i_type )
{
int i_new;
+ variable_t *p_var;
vlc_mutex_lock( &p_this->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_this->p_vars, p_this->i_vars, psz_name );
+
+ if( i_new >= 0 )
+ {
+ /* If the types differ, variable creation failed. */
+ if( i_type != p_this->p_vars[i_new].i_type )
+ {
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_EBADVAR;
+ }
+
+ p_this->p_vars[i_new].i_usage++;
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_SUCCESS;
+ }
+
+ i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
+
if( (p_this->i_vars & 15) == 15 )
{
p_this->p_vars = realloc( p_this->p_vars,
(p_this->i_vars+17) * sizeof(variable_t) );
}
- i_new = Insert( p_this->p_vars, p_this->i_vars, psz_name );
-
memmove( p_this->p_vars + i_new + 1,
p_this->p_vars + i_new,
(p_this->i_vars - i_new) * sizeof(variable_t) );
- p_this->p_vars[i_new].i_hash = HashString( psz_name );
- p_this->p_vars[i_new].psz_name = strdup( psz_name );
+ p_var = &p_this->p_vars[i_new];
- p_this->p_vars[i_new].i_type = i_type;
- memset( &p_this->p_vars[i_new].val, 0, sizeof(vlc_value_t) );
+ p_var->i_hash = HashString( psz_name );
+ p_var->psz_name = strdup( psz_name );
- p_this->p_vars[i_new].b_set = VLC_FALSE;
- p_this->p_vars[i_new].b_active = VLC_TRUE;
+ p_var->i_type = i_type;
+ memset( &p_var->val, 0, sizeof(vlc_value_t) );
+
+ p_var->i_usage = 1;
+ p_var->b_set = VLC_FALSE;
+ p_var->b_active = VLC_TRUE;
p_this->i_vars++;
int __var_Destroy( vlc_object_t *p_this, const char *psz_name )
{
int i_del;
+ variable_t *p_var;
vlc_mutex_lock( &p_this->var_lock );
return VLC_ENOVAR;
}
+ p_var = &p_this->p_vars[i_del];
+
+ if( p_var->i_usage > 1 )
+ {
+ p_var->i_usage--;
+ vlc_mutex_unlock( &p_this->var_lock );
+ return VLC_SUCCESS;
+ }
+
/* Free value if needed */
- switch( p_this->p_vars[i_del].i_type )
+ switch( p_var->i_type )
{
case VLC_VAR_STRING:
case VLC_VAR_MODULE:
case VLC_VAR_FILE:
- if( p_this->p_vars[i_del].b_set
- && p_this->p_vars[i_del].val.psz_string )
+ if( p_var->b_set && p_var->val.psz_string )
{
- free( p_this->p_vars[i_del].val.psz_string );
+ free( p_var->val.psz_string );
}
break;
}
- free( p_this->p_vars[i_del].psz_name );
+ free( p_var->psz_name );
memmove( p_this->p_vars + i_del,
p_this->p_vars + i_del + 1,