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 <ivoire@videolan.org>
This commit is contained in:
Adrien Maglo 2008-09-30 09:10:25 +02:00 committed by Rémi Duraffort
parent b9dc29f14c
commit 639eb0c5ab
6 changed files with 31 additions and 14 deletions

View File

@ -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

View File

@ -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] );

View File

@ -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 );
}

View File

@ -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 );
}

View File

@ -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 );

View File

@ -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;