X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fmisc%2Fobjects.c;h=63f3c2b3157e2ce13f3a8eab4562e5f8b060ae60;hb=c22195b458f8bf44b4dbd5f424031e0add08e5d0;hp=13f1a849056f6efb56319936444ec5cf17f48b5f;hpb=bde4f0ba22eb98928ed68b236d5cd80efbe0651d;p=vlc diff --git a/src/misc/objects.c b/src/misc/objects.c index 13f1a84905..63f3c2b315 100644 --- a/src/misc/objects.c +++ b/src/misc/objects.c @@ -54,11 +54,11 @@ # include /* ENOSYS */ #endif +#include #include #if defined (HAVE_SYS_EVENTFD_H) # include -# define HAVE_EVENTFD 1 #endif @@ -122,7 +122,7 @@ void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size, p_priv->i_object_type = i_type; p_new->psz_object_type = psz_type; - p_new->psz_object_name = NULL; + p_priv->psz_name = NULL; p_new->b_die = false; p_new->b_error = false; @@ -160,8 +160,6 @@ void *__vlc_custom_create( vlc_object_t *p_this, size_t i_size, p_priv->pp_children = NULL; p_priv->i_children = 0; - p_new->p_private = NULL; - /* Initialize mutexes and condvars */ vlc_mutex_init( &p_priv->var_lock ); vlc_cond_init( &p_priv->var_wait ); @@ -205,18 +203,10 @@ void * __vlc_object_create( vlc_object_t *p_this, int i_type ) switch( i_type ) { - case VLC_OBJECT_INTF: - i_size = sizeof(intf_thread_t); - psz_type = "interface"; - break; case VLC_OBJECT_DECODER: i_size = sizeof(decoder_t); psz_type = "decoder"; break; - case VLC_OBJECT_PACKETIZER: - i_size = sizeof(decoder_t); - psz_type = "packetizer"; - break; case VLC_OBJECT_AOUT: i_size = sizeof(aout_instance_t); psz_type = "audio output"; @@ -245,7 +235,24 @@ void __vlc_object_set_destructor( vlc_object_t *p_this, vlc_destructor_t pf_destructor ) { vlc_object_internals_t *p_priv = vlc_internals(p_this ); + + vlc_spin_lock( &p_priv->ref_spin ); p_priv->pf_destructor = pf_destructor; + vlc_spin_unlock( &p_priv->ref_spin ); +} + +#undef vlc_object_set_name +int vlc_object_set_name(vlc_object_t *obj, const char *name) +{ + vlc_object_internals_t *priv = vlc_internals(obj); + + /* Object must be named before it is attached (or never) */ + assert(obj->p_parent == NULL); + assert(priv->i_children == 0); + + free(priv->psz_name); + priv->psz_name = name ? strdup(name) : NULL; + return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM; } /** @@ -288,7 +295,7 @@ static void vlc_object_destroy( vlc_object_t *p_this ) free( p_this->psz_header ); - FREENULL( p_this->psz_object_name ); + free( p_priv->psz_name ); vlc_spin_destroy( &p_priv->ref_spin ); if( p_priv->pipes[1] != -1 && p_priv->pipes[1] != p_priv->pipes[0] ) @@ -460,6 +467,14 @@ void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode ) return p_found; } + +static int objnamecmp(const vlc_object_t *obj, const char *name) +{ + if (!vlc_object_get_name(obj)) + return INT_MIN; + return strcmp( vlc_object_get_name(obj), name ); +} + #undef vlc_object_find_name /** * Finds a named object and increment its reference count. @@ -482,10 +497,11 @@ vlc_object_t *vlc_object_find_name( vlc_object_t *p_this, { vlc_object_t *p_found; + /* Reading psz_object_name from a separate inhibits thread-safety. + * Use a libvlc address variable instead for that sort of things! */ + msg_Warn( p_this, "%s(%s) is not safe!", __func__, psz_name ); /* If have the requested name ourselves, don't look further */ - if( !(i_mode & FIND_STRICT) - && p_this->psz_object_name - && !strcmp( p_this->psz_object_name, psz_name ) ) + if( !(i_mode & FIND_STRICT) && !objnamecmp(p_this, psz_name) ) { vlc_object_hold( p_this ); return p_this; @@ -585,7 +601,7 @@ void __vlc_object_release( vlc_object_t *p_this ) fprintf( stderr, "ERROR: leaking object (%p, type:%s, name:%s)\n", leaked, leaked->psz_object_type, - leaked->psz_object_name ); + vlc_object_get_name(leaked) ); /* Dump object to ease debugging */ vlc_object_dump( leaked ); fflush(stderr); @@ -1018,8 +1034,7 @@ static vlc_object_t * FindObjectName( vlc_object_t *p_this, p_tmp = p_this->p_parent; if( p_tmp ) { - if( p_tmp->psz_object_name - && !strcmp( p_tmp->psz_object_name, psz_name ) ) + if( !objnamecmp(p_tmp, psz_name) ) { vlc_object_hold( p_tmp ); return p_tmp; @@ -1035,8 +1050,7 @@ static vlc_object_t * FindObjectName( vlc_object_t *p_this, for( i = vlc_internals( p_this )->i_children; i--; ) { p_tmp = vlc_internals( p_this )->pp_children[i]; - if( p_tmp->psz_object_name - && !strcmp( p_tmp->psz_object_name, psz_name ) ) + if( !objnamecmp(p_tmp, psz_name ) ) { vlc_object_hold( p_tmp ); return p_tmp; @@ -1068,9 +1082,10 @@ static void PrintObject( vlc_object_t *p_this, const char *psz_prefix ) int canc = vlc_savecancel (); memset( &psz_name, 0, sizeof(psz_name) ); - if( p_this->psz_object_name ) + if( vlc_object_get_name(p_this) ) { - snprintf( psz_name, 49, " \"%s\"", p_this->psz_object_name ); + snprintf( psz_name, 49, " \"%s\"", + vlc_object_get_name(p_this) ); if( psz_name[48] ) psz_name[48] = '\"'; }