Commit 639eb0c5 authored by Adrien Maglo's avatar Adrien Maglo Committed by Rémi Duraffort

Redefine vlc_dictionary_clear() and vlc_dictionary_remove_value_for_key()...

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: default avatarRémi Duraffort <ivoire@videolan.org>
parent b9dc29f1
...@@ -432,7 +432,9 @@ static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size ) ...@@ -432,7 +432,9 @@ static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
p_dict->i_size = 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; int i;
struct vlc_dictionary_entry_t * p_current, * p_next; struct vlc_dictionary_entry_t * p_current, * p_next;
...@@ -444,6 +446,8 @@ static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict ) ...@@ -444,6 +446,8 @@ static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict )
while( p_current ) while( p_current )
{ {
p_next = p_current->p_next; 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->psz_key );
free( p_current ); free( p_current );
p_current = p_next; p_current = p_next;
...@@ -553,7 +557,7 @@ __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, ...@@ -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->i_size = new_dict.i_size;
p_dict->p_entries = new_dict.p_entries; 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 ...@@ -567,7 +571,9 @@ vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p
} }
static inline void 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 ) if( !p_dict->p_entries )
return; return;
...@@ -584,6 +590,8 @@ vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char ...@@ -584,6 +590,8 @@ vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char
do { do {
if( !strcmp( psz_key, p_entry->psz_key ) ) if( !strcmp( psz_key, p_entry->psz_key ) )
{ {
if( pf_free != NULL )
( * pf_free )( p_entry->p_value, p_obj );
if( !p_prev ) if( !p_prev )
p_dict->p_entries[i_pos] = p_entry->p_next; p_dict->p_entries[i_pos] = p_entry->p_next;
else else
......
...@@ -93,6 +93,14 @@ struct vlc_meta_t ...@@ -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_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 ) #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 ) 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] ); free( p_meta->ppsz_meta[meta_type] );
...@@ -119,7 +127,7 @@ static inline void vlc_meta_Delete( vlc_meta_t *m ) ...@@ -119,7 +127,7 @@ static inline void vlc_meta_Delete( vlc_meta_t *m )
int i; int i;
for( i = 0; i < VLC_META_TYPE_COUNT ; i++ ) for( i = 0; i < VLC_META_TYPE_COUNT ; i++ )
free( m->ppsz_meta[i] ); free( m->ppsz_meta[i] );
vlc_dictionary_clear( &m->extra_tags ); vlc_dictionary_clear( &m->extra_tags, &vlc_meta_FreeExtraKey, NULL );
free( m ); free( m );
} }
...@@ -129,7 +137,8 @@ static inline void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const ...@@ -129,7 +137,8 @@ static inline void vlc_meta_AddExtra( vlc_meta_t *m, const char *psz_name, const
if( psz_oldvalue != kVLCDictionaryNotFound ) if( psz_oldvalue != kVLCDictionaryNotFound )
{ {
free( psz_oldvalue ); 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) ); 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 ) ...@@ -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++ ) for( i = 0; ppsz_all_keys[i]; i++ )
{ {
/* Always try to remove the previous value */ /* 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] ); 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 ); vlc_dictionary_insert( &dst->extra_tags, ppsz_all_keys[i], p_value );
free( ppsz_all_keys[i] ); free( ppsz_all_keys[i] );
......
...@@ -232,7 +232,7 @@ static void browse_callback( ...@@ -232,7 +232,7 @@ static void browse_callback(
services_discovery_RemoveItem( p_sd, p_item ); services_discovery_RemoveItem( p_sd, p_item );
vlc_dictionary_remove_value_for_key( vlc_dictionary_remove_value_for_key(
&p_sys->services_name_to_input_item, &p_sys->services_name_to_input_item,
name ); name, NULL, NULL );
} }
} }
} }
...@@ -294,7 +294,7 @@ error: ...@@ -294,7 +294,7 @@ error:
if( p_sys->poll != NULL ) if( p_sys->poll != NULL )
avahi_threaded_poll_free( p_sys->poll ); 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 ); free( p_sys );
return VLC_EGENERIC; return VLC_EGENERIC;
...@@ -312,6 +312,6 @@ static void Close( vlc_object_t *p_this ) ...@@ -312,6 +312,6 @@ static void Close( vlc_object_t *p_this )
avahi_client_free( p_sys->client ); avahi_client_free( p_sys->client );
avahi_threaded_poll_free( p_sys->poll ); 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 ); free( p_sys );
} }
...@@ -235,7 +235,7 @@ libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis ) ...@@ -235,7 +235,7 @@ libvlc_media_discoverer_release( libvlc_media_discoverer_t * p_mdis )
} }
free( all_keys ); free( all_keys );
vlc_dictionary_clear( &p_mdis->catname_to_submedialist ); vlc_dictionary_clear( &p_mdis->catname_to_submedialist, NULL, NULL );
free( p_mdis ); free( p_mdis );
} }
......
...@@ -186,7 +186,7 @@ void msg_Destroy (libvlc_int_t *p_libvlc) ...@@ -186,7 +186,7 @@ void msg_Destroy (libvlc_int_t *p_libvlc)
CloseHandle( QUEUE.logfile ); CloseHandle( QUEUE.logfile );
#endif #endif
vlc_dictionary_clear( &priv->msg_enabled_objects ); vlc_dictionary_clear( &priv->msg_enabled_objects, NULL, NULL );
/* Destroy lock */ /* Destroy lock */
vlc_mutex_destroy( &QUEUE.lock ); vlc_mutex_destroy( &QUEUE.lock );
......
...@@ -85,11 +85,11 @@ int main (void) ...@@ -85,11 +85,11 @@ int main (void)
test_dictionary_validity( &dict, our_keys, size ); 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 ); 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 ); assert( vlc_dictionary_keys_count( &dict ) == 0 );
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment