[vlc-devel] [PATCH] vlc_arrays: refactor foreach loop for arrays

Romain Vimont rom1v at videolabs.io
Tue Aug 28 09:12:50 CEST 2018


Replace:

    FOREACH_ARRAY(item, array)
       // ...
    FOREACH_END()

by:

    ARRAY_FOREACH(item, array)
    {
        // ...
    }

The new implementation uses a variable name which depends on "item" to
avoid variable shadowing warnings for nested loops (even if this
specific shadowing is harmless). See previous discussion:
<https://mailman.videolan.org/pipermail/vlc-devel/2018-June/119489.html>

As a consequence, it prevents to declare the variable directly.
Concretely, it is not possible to write:

    ARRAY_FOREACH(void *item, array)

Instead, use:

    void *item;
    ARRAY_FOREACH(item, array);
---
 include/vlc_arrays.h                          |  14 +-
 modules/control/hotkeys.c                     |   6 +-
 .../gui/macosx/VLCExtensionsDialogProvider.m  |   3 +-
 modules/gui/macosx/VLCExtensionsManager.m     |  12 +-
 modules/gui/qt/dialogs/extensions.cpp         |   3 +-
 modules/gui/qt/dialogs/plugins.cpp            |   9 +-
 modules/gui/qt/extensions_manager.cpp         |  12 +-
 modules/lua/extension.c                       |   3 +-
 modules/lua/libs/dialog.c                     |   6 +-
 modules/misc/addons/fsstorage.c               | 178 +++++++++---------
 modules/misc/addons/vorepository.c            |   6 +-
 src/misc/addons.c                             |  35 ++--
 src/misc/events.c                             |  18 +-
 src/test/arrays.c                             |   5 +-
 14 files changed, 155 insertions(+), 155 deletions(-)

diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h
index bc2ae2ae6d..0d6c2dd371 100644
--- a/include/vlc_arrays.h
+++ b/include/vlc_arrays.h
@@ -243,13 +243,13 @@ static inline void *realloc_or_free( void *p, size_t sz )
 #define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
     BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
 
-#define FOREACH_ARRAY( item, array ) { \
-    int fe_idx; \
-    for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
-    { \
-        item = (array).p_elems[fe_idx];
-
-#define FOREACH_END() } }
+/* append ##item to index variable name to avoid variable shadowing warnings for
+ * nested loops */
+#define ARRAY_FOREACH(item, array) \
+    for (int array_index_##item = 0; \
+         array_index_##item < (array).i_size && \
+            ((item) = (array).p_elems[array_index_##item], 1); \
+         ++array_index_##item)
 
 
 /************************************************************************
diff --git a/modules/control/hotkeys.c b/modules/control/hotkeys.c
index a4078bf198..9fe8d98b02 100644
--- a/modules/control/hotkeys.c
+++ b/modules/control/hotkeys.c
@@ -1470,7 +1470,9 @@ static void PlayBookmark( intf_thread_t *p_intf, int i_num )
     char *psz_bookmark = var_CreateGetString( p_intf, psz_bookmark_name );
 
     PL_LOCK;
-    FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
+    playlist_item_t *p_item;
+    ARRAY_FOREACH( p_item, p_playlist->items )
+    {
         char *psz_uri = input_item_GetURI( p_item->p_input );
         if( !strcmp( psz_bookmark, psz_uri ) )
         {
@@ -1480,7 +1482,7 @@ static void PlayBookmark( intf_thread_t *p_intf, int i_num )
         }
         else
             free( psz_uri );
-    FOREACH_END();
+    }
     PL_UNLOCK;
 
     free( psz_bookmark );
diff --git a/modules/gui/macosx/VLCExtensionsDialogProvider.m b/modules/gui/macosx/VLCExtensionsDialogProvider.m
index dfc47637a6..8c685d8c17 100644
--- a/modules/gui/macosx/VLCExtensionsDialogProvider.m
+++ b/modules/gui/macosx/VLCExtensionsDialogProvider.m
@@ -377,7 +377,7 @@ static void extensionDialogCallback(extension_dialog_t *p_ext_dialog,
     extension_widget_t *widget;
     VLCDialogWindow *dialogWindow = (__bridge VLCDialogWindow *)(dialog->p_sys_intf);
 
-    FOREACH_ARRAY(widget, dialog->widgets) {
+    ARRAY_FOREACH(widget, dialog->widgets) {
         if (!widget)
             continue; /* Some widgets may be NULL at this point */
 
@@ -432,7 +432,6 @@ static void extensionDialogCallback(extension_dialog_t *p_ext_dialog,
             }
         }
     }
-    FOREACH_END()
 }
 
 /** Create a dialog
diff --git a/modules/gui/macosx/VLCExtensionsManager.m b/modules/gui/macosx/VLCExtensionsManager.m
index 3f1d64147e..2de351e393 100644
--- a/modules/gui/macosx/VLCExtensionsManager.m
+++ b/modules/gui/macosx/VLCExtensionsManager.m
@@ -80,7 +80,7 @@
     extension_t *p_ext = NULL;
     int i_ext = 0;
 
-    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
+    ARRAY_FOREACH(p_ext, p_extensions_manager->extensions) {
         bool b_Active = extension_IsActivated(p_extensions_manager, p_ext);
 
         NSString *titleString = toNSStr(p_ext->psz_title);
@@ -149,7 +149,6 @@
         }
         i_ext++;
     }
-    FOREACH_END()
 
     vlc_mutex_unlock(&p_extensions_manager->lock);
 }
@@ -248,11 +247,10 @@
     vlc_mutex_lock(&p_extensions_manager->lock);
 
     extension_t *p_ext;
-    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
+    ARRAY_FOREACH(p_ext, p_extensions_manager->extensions) {
         if (extension_IsActivated(p_extensions_manager, p_ext))
             extension_SetInput(p_extensions_manager, p_ext, p_input);
     }
-    FOREACH_END()
 
     vlc_mutex_unlock(&p_extensions_manager->lock);
 }
@@ -265,11 +263,10 @@
     vlc_mutex_lock(&p_extensions_manager->lock);
 
     extension_t *p_ext;
-    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
+    ARRAY_FOREACH(p_ext, p_extensions_manager->extensions) {
         if (extension_IsActivated(p_extensions_manager, p_ext))
             extension_PlayingChanged(p_extensions_manager, p_ext, state);
     }
-    FOREACH_END()
 
     vlc_mutex_unlock(&p_extensions_manager->lock);
 }
@@ -281,11 +278,10 @@
         return;
     vlc_mutex_lock(&p_extensions_manager->lock);
     extension_t *p_ext;
-    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
+    ARRAY_FOREACH(p_ext, p_extensions_manager->extensions) {
         if (extension_IsActivated(p_extensions_manager, p_ext))
             extension_MetaChanged(p_extensions_manager, p_ext);
     }
-    FOREACH_END()
     vlc_mutex_unlock(&p_extensions_manager->lock);
 }
 
diff --git a/modules/gui/qt/dialogs/extensions.cpp b/modules/gui/qt/dialogs/extensions.cpp
index 7b8a732e6c..020f626689 100644
--- a/modules/gui/qt/dialogs/extensions.cpp
+++ b/modules/gui/qt/dialogs/extensions.cpp
@@ -479,7 +479,7 @@ void ExtensionDialog::UpdateWidgets()
 {
     assert( p_dialog );
     extension_widget_t *p_widget;
-    FOREACH_ARRAY( p_widget, p_dialog->widgets )
+    ARRAY_FOREACH( p_widget, p_dialog->widgets )
     {
         if( !p_widget ) continue; /* Some widgets may be NULL at this point */
         QWidget *widget;
@@ -539,7 +539,6 @@ void ExtensionDialog::UpdateWidgets()
             this->resize( sizeHint() );
         }
     }
-    FOREACH_END()
 }
 
 QWidget* ExtensionDialog::UpdateWidget( extension_widget_t *p_widget )
diff --git a/modules/gui/qt/dialogs/plugins.cpp b/modules/gui/qt/dialogs/plugins.cpp
index 93c92b9fa6..f0147f0dcd 100644
--- a/modules/gui/qt/dialogs/plugins.cpp
+++ b/modules/gui/qt/dialogs/plugins.cpp
@@ -706,12 +706,11 @@ void ExtensionListModel::updateList()
 
     vlc_mutex_lock( &p_mgr->lock );
     extension_t *p_ext;
-    FOREACH_ARRAY( p_ext, p_mgr->extensions )
+    ARRAY_FOREACH( p_ext, p_mgr->extensions )
     {
         ext = new ExtensionCopy( p_ext );
         extensions.append( ext );
     }
-    FOREACH_END()
     vlc_mutex_unlock( &p_mgr->lock );
     vlc_object_release( p_mgr );
 
@@ -850,9 +849,9 @@ QVariant AddonsListModel::Addon::data( int role ) const
     case FilenameRole:
     {
         QList<QString> list;
-        FOREACH_ARRAY( addon_file_t *p_file, p_entry->files )
-        list << qfu( p_file->psz_filename );
-        FOREACH_END();
+        addon_file_t *p_file;
+        ARRAY_FOREACH( p_file, p_entry->files )
+            list << qfu( p_file->psz_filename );
         returnval = QVariant( list );
         break;
     }
diff --git a/modules/gui/qt/extensions_manager.cpp b/modules/gui/qt/extensions_manager.cpp
index aecff87787..dadd760312 100644
--- a/modules/gui/qt/extensions_manager.cpp
+++ b/modules/gui/qt/extensions_manager.cpp
@@ -147,7 +147,7 @@ void ExtensionsManager::menu( QMenu *current )
     QAction *action;
     extension_t *p_ext = NULL;
     int i_ext = 0;
-    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
+    ARRAY_FOREACH( p_ext, p_extensions_manager->extensions )
     {
         bool b_Active = extension_IsActivated( p_extensions_manager, p_ext );
 
@@ -213,7 +213,6 @@ void ExtensionsManager::menu( QMenu *current )
         }
         i_ext++;
     }
-    FOREACH_END()
 
     vlc_mutex_unlock( &p_extensions_manager->lock );
 }
@@ -273,14 +272,13 @@ void ExtensionsManager::inputChanged( )
     vlc_mutex_lock( &p_extensions_manager->lock );
 
     extension_t *p_ext;
-    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
+    ARRAY_FOREACH( p_ext, p_extensions_manager->extensions )
     {
         if( extension_IsActivated( p_extensions_manager, p_ext ) )
         {
             extension_SetInput( p_extensions_manager, p_ext, p_input );
         }
     }
-    FOREACH_END()
 
     vlc_mutex_unlock( &p_extensions_manager->lock );
 }
@@ -293,14 +291,13 @@ void ExtensionsManager::playingChanged( int state )
     vlc_mutex_lock( &p_extensions_manager->lock );
 
     extension_t *p_ext;
-    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
+    ARRAY_FOREACH( p_ext, p_extensions_manager->extensions )
     {
         if( extension_IsActivated( p_extensions_manager, p_ext ) )
         {
             extension_PlayingChanged( p_extensions_manager, p_ext, state );
         }
     }
-    FOREACH_END()
 
     vlc_mutex_unlock( &p_extensions_manager->lock );
 }
@@ -312,13 +309,12 @@ void ExtensionsManager::metaChanged( input_item_t* )
         return ;
     vlc_mutex_lock( &p_extensions_manager->lock );
     extension_t *p_ext;
-    FOREACH_ARRAY( p_ext, p_extensions_manager->extensions )
+    ARRAY_FOREACH( p_ext, p_extensions_manager->extensions )
     {
         if( extension_IsActivated( p_extensions_manager, p_ext ) )
         {
             extension_MetaChanged( p_extensions_manager, p_ext );
         }
     }
-    FOREACH_END()
     vlc_mutex_unlock( &p_extensions_manager->lock );
 }
diff --git a/modules/lua/extension.c b/modules/lua/extension.c
index 39a72496eb..f0b3f3eae1 100644
--- a/modules/lua/extension.c
+++ b/modules/lua/extension.c
@@ -137,7 +137,7 @@ void Close_Extension( vlc_object_t *p_this )
     extension_t *p_ext = NULL;
 
     /* Free extensions' memory */
-    FOREACH_ARRAY( p_ext, p_mgr->extensions )
+    ARRAY_FOREACH( p_ext, p_mgr->extensions )
     {
         if( !p_ext )
             break;
@@ -186,7 +186,6 @@ void Close_Extension( vlc_object_t *p_this )
         free( p_ext->p_sys );
         free( p_ext );
     }
-    FOREACH_END()
 
     vlc_mutex_destroy( &p_mgr->lock );
 
diff --git a/modules/lua/libs/dialog.c b/modules/lua/libs/dialog.c
index 488b032226..8a74b18262 100644
--- a/modules/lua/libs/dialog.c
+++ b/modules/lua/libs/dialog.c
@@ -268,7 +268,7 @@ static int vlclua_dialog_delete( lua_State *L )
 
     /* Destroy widgets */
     extension_widget_t *p_widget;
-    FOREACH_ARRAY( p_widget, p_dlg->widgets )
+    ARRAY_FOREACH( p_widget, p_dlg->widgets )
     {
         if( !p_widget )
             continue;
@@ -284,7 +284,6 @@ static int vlclua_dialog_delete( lua_State *L )
         }
         free( p_widget );
     }
-    FOREACH_END()
 
     ARRAY_RESET( p_dlg->widgets );
 
@@ -1058,7 +1057,7 @@ static int DeleteWidget( extension_dialog_t *p_dialog,
     int pos = -1;
     bool found = false;
     extension_widget_t *p_iter;
-    FOREACH_ARRAY( p_iter, p_dialog->widgets )
+    ARRAY_FOREACH( p_iter, p_dialog->widgets )
     {
         pos++;
         if( p_iter == p_widget )
@@ -1067,7 +1066,6 @@ static int DeleteWidget( extension_dialog_t *p_dialog,
             break;
         }
     }
-    FOREACH_END()
 
     if( !found )
         return VLC_EGENERIC;
diff --git a/modules/misc/addons/fsstorage.c b/modules/misc/addons/fsstorage.c
index 77ba5db6cf..6d264294ba 100644
--- a/modules/misc/addons/fsstorage.c
+++ b/modules/misc/addons/fsstorage.c
@@ -276,15 +276,19 @@ static bool FileBelongsToManagedAddon( addons_finder_t *p_finder,
                                        const addon_type_t e_type,
                                        const char *psz_file )
 {
-    FOREACH_ARRAY( const addon_entry_t *p_entry, p_finder->entries )
+    const addon_entry_t *p_entry;
+    ARRAY_FOREACH( p_entry, p_finder->entries )
+    {
         if ( ( p_entry->e_flags & ADDON_MANAGEABLE ) == 0 )
             continue;
-        FOREACH_ARRAY( const addon_file_t *p_file, p_entry->files )
+        const addon_file_t *p_file;
+        ARRAY_FOREACH( p_file, p_entry->files )
+        {
             if ( p_file->e_filetype == e_type
                  && !strcmp( p_file->psz_filename, psz_file ) )
                 return true;
-        FOREACH_END();
-    FOREACH_END();
+        }
+    }
     return false;
 }
 
@@ -440,56 +444,55 @@ static int InstallAllFiles( addons_storage_t *p_this, const addon_entry_t *p_ent
     if ( p_entry->files.i_size < 1 )
         return VLC_EGENERIC;
 
-    FOREACH_ARRAY( p_file, p_entry->files )
-
-    switch( p_file->e_filetype )
+    ARRAY_FOREACH( p_file, p_entry->files )
     {
-        case ADDON_EXTENSION:
-        case ADDON_PLAYLIST_PARSER:
-        case ADDON_SERVICE_DISCOVERY:
-        case ADDON_INTERFACE:
-        case ADDON_META:
-        case ADDON_SKIN2:
+        switch( p_file->e_filetype )
         {
-            if ( strstr( p_file->psz_filename, ".." ) )
-                return VLC_EGENERIC;
-
-            char *psz_translated_filename = strdup( p_file->psz_filename );
-            if ( !psz_translated_filename )
-                return VLC_ENOMEM;
-            char *tmp = psz_translated_filename;
-            while (*tmp++) if ( *tmp == '/' ) *tmp = DIR_SEP_CHAR;
-
-            char *psz_dir = getAddonInstallDir( p_file->e_filetype );
-            if ( !psz_dir || asprintf( &psz_dest, "%s"DIR_SEP"%s", psz_dir,
-                           psz_translated_filename ) < 1 )
+            case ADDON_EXTENSION:
+            case ADDON_PLAYLIST_PARSER:
+            case ADDON_SERVICE_DISCOVERY:
+            case ADDON_INTERFACE:
+            case ADDON_META:
+            case ADDON_SKIN2:
             {
-                free( psz_dir );
+                if ( strstr( p_file->psz_filename, ".." ) )
+                    return VLC_EGENERIC;
+
+                char *psz_translated_filename = strdup( p_file->psz_filename );
+                if ( !psz_translated_filename )
+                    return VLC_ENOMEM;
+                char *tmp = psz_translated_filename;
+                while (*tmp++) if ( *tmp == '/' ) *tmp = DIR_SEP_CHAR;
+
+                char *psz_dir = getAddonInstallDir( p_file->e_filetype );
+                if ( !psz_dir || asprintf( &psz_dest, "%s"DIR_SEP"%s", psz_dir,
+                               psz_translated_filename ) < 1 )
+                {
+                    free( psz_dir );
+                    free( psz_translated_filename );
+                    return VLC_EGENERIC;
+                }
                 free( psz_translated_filename );
-                return VLC_EGENERIC;
-            }
-            free( psz_translated_filename );
-            free( psz_dir );
+                free( psz_dir );
+
+                if ( InstallFile( p_this, p_file->psz_download_uri, psz_dest ) != VLC_SUCCESS )
+                {
+                    free( psz_dest );
+                    return VLC_EGENERIC;
+                }
 
-            if ( InstallFile( p_this, p_file->psz_download_uri, psz_dest ) != VLC_SUCCESS )
-            {
                 free( psz_dest );
-                return VLC_EGENERIC;
+                break;
             }
-
-            free( psz_dest );
-            break;
+            /* Ignore all other unhandled files */
+            case ADDON_UNKNOWN:
+            case ADDON_PLUGIN:
+            case ADDON_OTHER:
+            default:
+                break;
         }
-        /* Ignore all other unhandled files */
-        case ADDON_UNKNOWN:
-        case ADDON_PLUGIN:
-        case ADDON_OTHER:
-        default:
-            break;
     }
 
-    FOREACH_END()
-
     return VLC_SUCCESS;
 }
 
@@ -638,12 +641,14 @@ static int WriteCatalog( addons_storage_t *p_storage,
         WRITE_WITH_ENTITIES( "\t\t\t\t<sourceurl>%s</sourceurl>\n", p_entry->psz_source_uri )
         fprintf( p_catalog, "\t\t\t</authorship>\n" );
 
-        FOREACH_ARRAY( addon_file_t *p_file, p_entry->files )
+        addon_file_t *p_file;
+        ARRAY_FOREACH( p_file, p_entry->files )
+        {
             psz_tempstring = vlc_xml_encode( p_file->psz_filename );
             fprintf( p_catalog, "\t\t\t<resource type=\"%s\">%s</resource>\n",
                      getTypePsz( p_file->e_filetype ), psz_tempstring );
             free( psz_tempstring );
-        FOREACH_END();
+        }
 
         fprintf( p_catalog, "\t\t</addon>\n" );
 
@@ -863,56 +868,59 @@ end:
 static int Remove( addons_storage_t *p_storage, addon_entry_t *p_entry )
 {
     vlc_mutex_lock( &p_entry->lock );
-    FOREACH_ARRAY( addon_file_t *p_file, p_entry->files )
-    switch( p_file->e_filetype )
-    {
-        case ADDON_EXTENSION:
-        case ADDON_PLAYLIST_PARSER:
-        case ADDON_SERVICE_DISCOVERY:
-        case ADDON_INTERFACE:
-        case ADDON_META:
-        case ADDON_SKIN2:
+    addon_file_t *p_file;
+    ARRAY_FOREACH( p_file, p_entry->files )
+    {
+        switch( p_file->e_filetype )
         {
-            char *psz_dest;
+            case ADDON_EXTENSION:
+            case ADDON_PLAYLIST_PARSER:
+            case ADDON_SERVICE_DISCOVERY:
+            case ADDON_INTERFACE:
+            case ADDON_META:
+            case ADDON_SKIN2:
+            {
+                char *psz_dest;
 
-            char *psz_translated_filename = strdup( p_file->psz_filename );
-            if ( !psz_translated_filename )
-                return VLC_ENOMEM;
-            char *tmp = psz_translated_filename;
-            while (*tmp++) if ( *tmp == '/' ) *tmp = DIR_SEP_CHAR;
+                char *psz_translated_filename = strdup( p_file->psz_filename );
+                if ( !psz_translated_filename )
+                    return VLC_ENOMEM;
+                char *tmp = psz_translated_filename;
+                while (*tmp++) if ( *tmp == '/' ) *tmp = DIR_SEP_CHAR;
 
-            char *psz_dir = getAddonInstallDir( p_file->e_filetype );
-            if ( !psz_dir || asprintf( &psz_dest, "%s"DIR_SEP"%s", psz_dir,
-                                       psz_translated_filename ) < 1 )
-            {
+                char *psz_dir = getAddonInstallDir( p_file->e_filetype );
+                if ( !psz_dir || asprintf( &psz_dest, "%s"DIR_SEP"%s", psz_dir,
+                                           psz_translated_filename ) < 1 )
+                {
+                    free( psz_dir );
+                    free( psz_translated_filename );
+                    return VLC_EGENERIC;
+                }
                 free( psz_dir );
                 free( psz_translated_filename );
-                return VLC_EGENERIC;
-            }
-            free( psz_dir );
-            free( psz_translated_filename );
 
-            vlc_unlink( psz_dest );
-            msg_Dbg( p_storage, "removing %s", psz_dest );
+                vlc_unlink( psz_dest );
+                msg_Dbg( p_storage, "removing %s", psz_dest );
 
-            free( psz_dest );
-            break;
+                free( psz_dest );
+                break;
+            }
+                /* Ignore all other unhandled files */
+            case ADDON_UNKNOWN:
+            case ADDON_PLUGIN:
+            case ADDON_OTHER:
+            default:
+                break;
         }
-            /* Ignore all other unhandled files */
-        case ADDON_UNKNOWN:
-        case ADDON_PLUGIN:
-        case ADDON_OTHER:
-        default:
-            break;
     }
-    FOREACH_END()
 
     /* Remove file info on success */
-    FOREACH_ARRAY( addon_file_t *p_file, p_entry->files )
-    free( p_file->psz_filename );
-    free( p_file->psz_download_uri );
-    free( p_file );
-    FOREACH_END()
+    ARRAY_FOREACH( p_file, p_entry->files )
+    {
+        free( p_file->psz_filename );
+        free( p_file->psz_download_uri );
+        free( p_file );
+    }
     ARRAY_RESET( p_entry->files );
 
     vlc_mutex_unlock( &p_entry->lock );
diff --git a/modules/misc/addons/vorepository.c b/modules/misc/addons/vorepository.c
index c80172dd7c..4d5d4857b8 100644
--- a/modules/misc/addons/vorepository.c
+++ b/modules/misc/addons/vorepository.c
@@ -485,10 +485,10 @@ static int FindDesignated( addons_finder_t *p_finder )
     if ( ParseCategoriesInfo( p_finder, p_stream ) )
     {
         /* Do archive uri fixup */
-        FOREACH_ARRAY( addon_entry_t *p_entry, p_finder->entries )
-        if ( likely( !p_entry->psz_archive_uri ) )
+        addon_entry_t *p_entry;
+        ARRAY_FOREACH( p_entry, p_finder->entries )
+            if ( likely( !p_entry->psz_archive_uri ) )
                 p_entry->psz_archive_uri = strdup( p_finder->psz_uri );
-        FOREACH_END()
     }
     else
     {
diff --git a/src/misc/addons.c b/src/misc/addons.c
index 56ea33166a..1ec38c642b 100644
--- a/src/misc/addons.c
+++ b/src/misc/addons.c
@@ -116,11 +116,12 @@ void addon_entry_Release( addon_entry_t * p_entry )
     free( p_entry->p_custom );
 
     addon_file_t *p_file;
-    FOREACH_ARRAY( p_file, p_entry->files )
-    free( p_file->psz_filename );
-    free( p_file->psz_download_uri );
-    free( p_file );
-    FOREACH_END()
+    ARRAY_FOREACH( p_file, p_entry->files )
+    {
+        free( p_file->psz_filename );
+        free( p_file->psz_download_uri );
+        free( p_file );
+    }
     ARRAY_RESET( p_entry->files );
 
     vlc_mutex_destroy( &p_entry->lock );
@@ -192,10 +193,11 @@ void addons_manager_Delete( addons_manager_t *p_manager )
         vlc_join( p_manager->p_priv->installer.thread, NULL );
     }
 
+    addon_entry_t *p_entry;
+
 #define FREE_QUEUE( name ) \
-    FOREACH_ARRAY( addon_entry_t *p_entry, p_manager->p_priv->name.entries )\
+    ARRAY_FOREACH( p_entry, p_manager->p_priv->name.entries )\
         addon_entry_Release( p_entry );\
-    FOREACH_END();\
     ARRAY_RESET( p_manager->p_priv->name.entries );\
     vlc_mutex_destroy( &p_manager->p_priv->name.lock );\
     vlc_cond_destroy( &p_manager->p_priv->name.waitcond );\
@@ -203,9 +205,10 @@ void addons_manager_Delete( addons_manager_t *p_manager )
 
     FREE_QUEUE( finder )
     FREE_QUEUE( installer )
-    FOREACH_ARRAY( char *psz_uri, p_manager->p_priv->finder.uris )
+
+    char *psz_uri;
+    ARRAY_FOREACH( psz_uri, p_manager->p_priv->finder.uris )
        free( psz_uri );
-    FOREACH_END();
     ARRAY_RESET( p_manager->p_priv->finder.uris );
 
     free( p_manager->p_priv );
@@ -247,14 +250,16 @@ static addon_entry_t * getHeldEntryByUUID( addons_manager_t *p_manager,
 {
     addon_entry_t *p_return = NULL;
     vlc_mutex_lock( &p_manager->p_priv->finder.lock );
-    FOREACH_ARRAY( addon_entry_t *p_entry, p_manager->p_priv->finder.entries )
-    if ( !memcmp( p_entry->uuid, uuid, sizeof( addon_uuid_t ) ) )
+    addon_entry_t *p_entry;
+    ARRAY_FOREACH( p_entry, p_manager->p_priv->finder.entries )
     {
-        p_return = p_entry;
-        addon_entry_Hold( p_return );
-        break;
+        if ( !memcmp( p_entry->uuid, uuid, sizeof( addon_uuid_t ) ) )
+        {
+            p_return = p_entry;
+            addon_entry_Hold( p_return );
+            break;
+        }
     }
-    FOREACH_END()
     vlc_mutex_unlock( &p_manager->p_priv->finder.lock );
     return p_return;
 }
diff --git a/src/misc/events.c b/src/misc/events.c
index 6bf55268ed..a2273d654f 100644
--- a/src/misc/events.c
+++ b/src/misc/events.c
@@ -87,9 +87,9 @@ void vlc_event_manager_fini( vlc_event_manager_t * p_em )
     {
         struct vlc_event_listeners_group_t *slot = p_em->events + i;
 
-        FOREACH_ARRAY( listener, slot->listeners )
+        ARRAY_FOREACH( listener, slot->listeners )
             free( listener );
-        FOREACH_END()
+
         ARRAY_RESET( slot->listeners );
     }
 }
@@ -108,9 +108,8 @@ void vlc_event_send( vlc_event_manager_t * p_em,
 
     vlc_mutex_lock( &p_em->lock ) ;
 
-    FOREACH_ARRAY( listener, slot->listeners )
+    ARRAY_FOREACH( listener, slot->listeners )
         listener->pf_callback( p_event, listener->p_user_data );
-    FOREACH_END()
 
     vlc_mutex_unlock( &p_em->lock );
 }
@@ -150,23 +149,22 @@ void vlc_event_detach( vlc_event_manager_t *p_em,
                        void *p_user_data )
 {
     vlc_event_listeners_group_t *slot = &p_em->events[event_type];
-    struct vlc_event_listener_t * listener;
 
     vlc_mutex_lock( &p_em->lock );
 
-    FOREACH_ARRAY( listener, slot->listeners )
+    for (int i = 0; i < slot->listeners.i_size; ++i)
+    {
+        struct vlc_event_listener_t *listener = slot->listeners.p_elems[i];
         if( listener->pf_callback == pf_callback &&
             listener->p_user_data == p_user_data )
         {
             /* that's our listener */
-            ARRAY_REMOVE( slot->listeners,
-                          fe_idx /* This comes from the macro (and that's why
-                                    I hate macro) */ );
+            ARRAY_REMOVE( slot->listeners, i );
             vlc_mutex_unlock( &p_em->lock );
             free( listener );
             return;
         }
-    FOREACH_END()
+    }
 
     vlc_assert_unreachable();
 }
diff --git a/src/test/arrays.c b/src/test/arrays.c
index 82277bbbc1..f20c8f6edc 100644
--- a/src/test/arrays.c
+++ b/src/test/arrays.c
@@ -68,10 +68,11 @@ static void test_array_foreach(void)
 
     int count = 0;
     int item;
-    FOREACH_ARRAY(item, array)
+    ARRAY_FOREACH(item, array)
+    {
         assert(item == count);
         count++;
-    FOREACH_END()
+    }
     assert(count == 10);
 
     ARRAY_RESET(array);
-- 
2.18.0



More information about the vlc-devel mailing list