From: RĂ©mi Denis-Courmont Date: Sun, 20 Sep 2009 09:38:28 +0000 (+0300) Subject: Allow setting the name of an object at any time... X-Git-Tag: 1.1.0-ff~3306 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=8ad732cd471bb22c992bb1cbe4f44bdfb6e233ca;p=vlc Allow setting the name of an object at any time... ...in a thread-safe manner. However, this is really only usable for informational purpose (debugging the object tree). vlc_object_find_name() remains broken. --- diff --git a/include/vlc_objects.h b/include/vlc_objects.h index a5a692fc9b..8ffe9ae7c1 100644 --- a/include/vlc_objects.h +++ b/include/vlc_objects.h @@ -83,7 +83,7 @@ VLC_EXPORT( void *, __vlc_object_hold, ( vlc_object_t * ) ); VLC_EXPORT( void, __vlc_object_release, ( vlc_object_t * ) ); VLC_EXPORT( vlc_list_t *, __vlc_list_children, ( vlc_object_t * ) ); VLC_EXPORT( void, vlc_list_release, ( vlc_list_t * ) ); -VLC_EXPORT( const char *, vlc_object_get_name, ( const vlc_object_t * ) ) LIBVLC_USED; +VLC_EXPORT( char *, vlc_object_get_name, ( const vlc_object_t * ) ) LIBVLC_USED; #define vlc_object_get_name(o) vlc_object_get_name(VLC_OBJECT(o)) /*}@*/ diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c index b05f93d10f..1d67a0f73d 100644 --- a/modules/gui/ncurses.c +++ b/modules/gui/ncurses.c @@ -1453,10 +1453,13 @@ static void MainBoxWrite( intf_thread_t *p_intf, int l, int x, const char *p_fmt static void DumpObject( intf_thread_t *p_intf, int *l, vlc_object_t *p_obj, int i_level ) { - const char *psz_name = vlc_object_get_name( p_obj ); + char *psz_name = vlc_object_get_name( p_obj ); if( psz_name ) + { MainBoxWrite( p_intf, (*l)++, 1 + 2 * i_level, "%s \"%s\" (%p)", p_obj->psz_object_type, psz_name, p_obj ); + free( psz_name ); + } else MainBoxWrite( p_intf, (*l)++, 1 + 2 * i_level, "%s (%o)", p_obj->psz_object_type, p_obj ); diff --git a/modules/gui/qt4/dialogs/messages.cpp b/modules/gui/qt4/dialogs/messages.cpp index 622cc2fd3a..4c5954c34c 100644 --- a/modules/gui/qt4/dialogs/messages.cpp +++ b/modules/gui/qt4/dialogs/messages.cpp @@ -278,11 +278,14 @@ void MessagesDialog::buildTree( QTreeWidgetItem *parentItem, else item = new QTreeWidgetItem( modulesTree ); - const char *name = vlc_object_get_name( p_obj ); + char *name = vlc_object_get_name( p_obj ); if( name != NULL ) + { item->setText( 0, qfu( p_obj->psz_object_type ) + " \"" + qfu( name ) + "\" (" + QString::number((uintptr_t)p_obj) + ")" ); + free( name ); + } else item->setText( 0, qfu( p_obj->psz_object_type ) + " (" + QString::number((uintptr_t)p_obj) + ")" ); diff --git a/src/misc/objects.c b/src/misc/objects.c index cb7428743f..36f8537d24 100644 --- a/src/misc/objects.c +++ b/src/misc/objects.c @@ -241,26 +241,35 @@ void __vlc_object_set_destructor( vlc_object_t *p_this, vlc_spin_unlock( &p_priv->ref_spin ); } +static vlc_mutex_t name_lock = VLC_STATIC_MUTEX; + #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); + char *newname = name ? strdup (name) : NULL; + char *oldname; - /* Object must be named before it is attached (or never) */ - assert(obj->p_parent == NULL); - assert(priv->i_children == 0); + vlc_mutex_lock (&name_lock); + oldname = priv->psz_name; + priv->psz_name = newname; + vlc_mutex_unlock (&name_lock); - free(priv->psz_name); - priv->psz_name = name ? strdup(name) : NULL; + free (oldname); return (priv->psz_name || !name) ? VLC_SUCCESS : VLC_ENOMEM; } #undef vlc_object_get_name -const char *vlc_object_get_name(const vlc_object_t *obj) +char *vlc_object_get_name(const vlc_object_t *obj) { vlc_object_internals_t *priv = vlc_internals(obj); + char *name; + + vlc_mutex_lock (&name_lock); + name = priv->psz_name ? strdup (priv->psz_name) : NULL; + vlc_mutex_unlock (&name_lock); - return priv->psz_name; + return name; } /** @@ -478,9 +487,13 @@ void * __vlc_object_find( vlc_object_t *p_this, int i_type, int i_mode ) static int objnamecmp(const vlc_object_t *obj, const char *name) { - if (!vlc_object_get_name(obj)) + char *objname = vlc_object_get_name(obj); + if (objname == NULL) return INT_MIN; - return strcmp( vlc_object_get_name(obj), name ); + + int ret = strcmp (objname, name); + free (objname); + return ret; } #undef vlc_object_find_name @@ -607,9 +620,8 @@ void __vlc_object_release( vlc_object_t *p_this ) { /* We are leaking this object */ fprintf( stderr, - "ERROR: leaking object (%p, type:%s, name:%s)\n", - leaked, leaked->psz_object_type, - vlc_object_get_name(leaked) ); + "ERROR: leaking object (%p, type:%s)\n", + leaked, leaked->psz_object_type ); /* Dump object to ease debugging */ vlc_object_dump( leaked ); fflush(stderr); @@ -1090,10 +1102,11 @@ static void PrintObject( vlc_object_t *p_this, const char *psz_prefix ) int canc = vlc_savecancel (); memset( &psz_name, 0, sizeof(psz_name) ); - if( vlc_object_get_name(p_this) ) + char *name = vlc_object_get_name(p_this); + if( name ) { - snprintf( psz_name, 49, " \"%s\"", - vlc_object_get_name(p_this) ); + snprintf( psz_name, 49, " \"%s\"", name ); + free( name ); if( psz_name[48] ) psz_name[48] = '\"'; }