[vlc-devel] commit: 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 . (Adrien Maglo )

git version control git at videolan.org
Sun Oct 5 21:53:10 CEST 2008


vlc | branch: master | Adrien Maglo <magsoft at videolan.org> | Tue Sep 30 09:10:25 2008 +0200| [639eb0c5ab082e14c1fc9c32967b6d338032c2c9] | committer: Rémi Duraffort 

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 at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=639eb0c5ab082e14c1fc9c32967b6d338032c2c9
---

 include/vlc_arrays.h                 |   14 +++++++++++---
 include/vlc_meta.h                   |   15 ++++++++++++---
 modules/services_discovery/bonjour.c |    6 +++---
 src/control/media_discoverer.c       |    2 +-
 src/misc/messages.c                  |    2 +-
 src/test/dictionary.c                |    6 +++---
 6 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
index 35cc0ca..f3d1acc 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 a2abee0..300d1bb 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 fafff7b..817dc6e 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 a087549..a00ba76 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 468e665..3786314 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 b0d9644..7c82374 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;




More information about the vlc-devel mailing list