From: Adrien Maglo Date: Tue, 30 Sep 2008 07:10:25 +0000 (+0200) Subject: Redefine vlc_dictionary_clear() and vlc_dictionary_remove_value_for_key() Allow passi... X-Git-Tag: 1.0.0-pre1~2672 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=639eb0c5ab082e14c1fc9c32967b6d338032c2c9;p=vlc Redefine vlc_dictionary_clear() and vlc_dictionary_remove_value_for_key() Allow passing a pointer to a function and an opaque pointer in order to free the memory if the values of the dictionary contain allocated memory. Signed-off-by: RĂ©mi Duraffort --- diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h index 35cc0ca492..f3d1acc5cd 100644 --- a/include/vlc_arrays.h +++ b/include/vlc_arrays.h @@ -432,7 +432,9 @@ static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size ) p_dict->i_size = i_size; } -static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict ) +static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict, + void ( * pf_free )( void * p_data, void * p_obj ), + void * p_obj ) { int i; struct vlc_dictionary_entry_t * p_current, * p_next; @@ -444,6 +446,8 @@ static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict ) while( p_current ) { p_next = p_current->p_next; + if( pf_free != NULL ) + ( * pf_free )( p_current->p_value, p_obj ); free( p_current->psz_key ); free( p_current ); p_current = p_next; @@ -553,7 +557,7 @@ __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, } } - vlc_dictionary_clear( p_dict ); + vlc_dictionary_clear( p_dict, NULL, NULL ); p_dict->i_size = new_dict.i_size; p_dict->p_entries = new_dict.p_entries; } @@ -567,7 +571,9 @@ vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p } static inline void -vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key ) +vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key, + void ( * pf_free )( void * p_data, void * p_obj ), + void * p_obj ) { if( !p_dict->p_entries ) return; @@ -584,6 +590,8 @@ vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char do { if( !strcmp( psz_key, p_entry->psz_key ) ) { + if( pf_free != NULL ) + ( * pf_free )( p_entry->p_value, p_obj ); if( !p_prev ) p_dict->p_entries[i_pos] = p_entry->p_next; else diff --git a/include/vlc_meta.h b/include/vlc_meta.h index a2abee0bbd..300d1bb13f 100644 --- a/include/vlc_meta.h +++ b/include/vlc_meta.h @@ -93,6 +93,14 @@ struct vlc_meta_t #define vlc_meta_SetArtURL( meta, b ) vlc_meta_Set( meta, vlc_meta_ArtworkURL, b ) #define vlc_meta_SetTrackID( meta, b ) vlc_meta_Set( meta, vlc_meta_TrackID, b ) +/* Free a dictonary key allocated by strdup() in vlc_meta_AddExtra() */ +static void vlc_meta_FreeExtraKey( void * p_data, void * p_obj ) +{ + VLC_UNUSED( p_obj ); + free( p_data ); +} + + static inline void vlc_meta_Set( vlc_meta_t * p_meta, vlc_meta_type_t meta_type, const char * psz_val ) { free( p_meta->ppsz_meta[meta_type] ); @@ -119,7 +127,7 @@ static inline void vlc_meta_Delete( vlc_meta_t *m ) int i; for( i = 0; i < VLC_META_TYPE_COUNT ; i++ ) free( m->ppsz_meta[i] ); - vlc_dictionary_clear( &m->extra_tags ); + vlc_dictionary_clear( &m->extra_tags, &vlc_meta_FreeExtraKey, NULL ); free( m ); } @@ -129,7 +137,8 @@ static inline void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const if( psz_oldvalue != kVLCDictionaryNotFound ) { free( psz_oldvalue ); - vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name ); + vlc_dictionary_remove_value_for_key( &m->extra_tags, psz_name, + &vlc_meta_FreeExtraKey, NULL ); } vlc_dictionary_insert( &m->extra_tags, psz_name, strdup(psz_value) ); } @@ -155,7 +164,7 @@ static inline void vlc_meta_Merge( vlc_meta_t *dst, const vlc_meta_t *src ) for( i = 0; ppsz_all_keys[i]; i++ ) { /* Always try to remove the previous value */ - vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i] ); + vlc_dictionary_remove_value_for_key( &dst->extra_tags, ppsz_all_keys[i], NULL, NULL ); void * p_value = vlc_dictionary_value_for_key( &src->extra_tags, ppsz_all_keys[i] ); vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], p_value ); free( ppsz_all_keys[i] ); diff --git a/modules/services_discovery/bonjour.c b/modules/services_discovery/bonjour.c index fafff7bf03..817dc6ecf2 100644 --- a/modules/services_discovery/bonjour.c +++ b/modules/services_discovery/bonjour.c @@ -232,7 +232,7 @@ static void browse_callback( services_discovery_RemoveItem( p_sd, p_item ); vlc_dictionary_remove_value_for_key( &p_sys->services_name_to_input_item, - name ); + name, NULL, NULL ); } } } @@ -294,7 +294,7 @@ error: if( p_sys->poll != NULL ) avahi_threaded_poll_free( p_sys->poll ); - vlc_dictionary_clear( &p_sys->services_name_to_input_item ); + vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL ); free( p_sys ); return VLC_EGENERIC; @@ -312,6 +312,6 @@ static void Close( vlc_object_t *p_this ) avahi_client_free( p_sys->client ); avahi_threaded_poll_free( p_sys->poll ); - vlc_dictionary_clear( &p_sys->services_name_to_input_item ); + vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL ); free( p_sys ); } diff --git a/src/control/media_discoverer.c b/src/control/media_discoverer.c index a0875490a8..a00ba76764 100644 --- a/src/control/media_discoverer.c +++ b/src/control/media_discoverer.c @@ -235,7 +235,7 @@ libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis ) } free( all_keys ); - vlc_dictionary_clear( &p_mdis->catname_to_submedialist ); + vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL ); free( p_mdis ); } diff --git a/src/misc/messages.c b/src/misc/messages.c index 468e665cd6..37863148ec 100644 --- a/src/misc/messages.c +++ b/src/misc/messages.c @@ -186,7 +186,7 @@ void msg_Destroy (libvlc_int_t *p_libvlc) CloseHandle( QUEUE.logfile ); #endif - vlc_dictionary_clear( &priv->msg_enabled_objects ); + vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL ); /* Destroy lock */ vlc_mutex_destroy( &QUEUE.lock ); diff --git a/src/test/dictionary.c b/src/test/dictionary.c index b0d9644b7b..7c823749b1 100644 --- a/src/test/dictionary.c +++ b/src/test/dictionary.c @@ -85,11 +85,11 @@ int main (void) test_dictionary_validity( &dict, our_keys, size ); - vlc_dictionary_remove_value_for_key( &dict, our_keys[size-1] ); + vlc_dictionary_remove_value_for_key( &dict, our_keys[size-1], NULL, NULL ); test_dictionary_validity( &dict, our_keys, size-1 ); - - vlc_dictionary_clear( &dict ); + + vlc_dictionary_clear( &dict, NULL, NULL ); assert( vlc_dictionary_keys_count( &dict ) == 0 ); return 0;