[vlc-devel] [PATCH 17/40] playlist: allocate the root item statically

RĂ©mi Denis-Courmont remi at remlab.net
Sun May 14 17:45:47 CEST 2017


This allocates space for the root playlist item directly inside the
playlist structure. Creation of that item required a special case
anyway as it was the only item without a parent.

As a consequence, one unhandled error case is removed.
---
 include/vlc_playlist.h                          |  6 +++---
 modules/control/oldrc.c                         |  4 ++--
 modules/gui/macosx/VLCMainWindow.m              |  2 +-
 modules/gui/ncurses.c                           |  4 ++--
 modules/gui/qt/components/playlist/selector.cpp |  2 +-
 modules/gui/qt/dialogs/messages.cpp             |  2 +-
 modules/gui/skins2/commands/cmd_playtree.cpp    |  2 +-
 modules/gui/skins2/vars/playtree.cpp            |  4 ++--
 modules/lua/libs/playlist.c                     | 10 ++++-----
 src/playlist/engine.c                           | 28 +++++++++++++++++--------
 src/playlist/services_discovery.c               |  4 ++--
 src/playlist/tree.c                             |  3 +--
 12 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/include/vlc_playlist.h b/include/vlc_playlist.h
index b2adb3d2b8..ebd078404b 100644
--- a/include/vlc_playlist.h
+++ b/include/vlc_playlist.h
@@ -158,9 +158,9 @@ struct playlist_t
     int                   i_current_index; /**< Index in current array */
 
     /* Predefined items */
-    playlist_item_t *     p_root;
-    playlist_item_t *     p_playing;
-    playlist_item_t *     p_media_library;
+    playlist_item_t  root;
+    playlist_item_t *p_playing;
+    playlist_item_t *p_media_library;
 };
 
 /* A bit of macro magic to generate an enum out of the following list,
diff --git a/modules/control/oldrc.c b/modules/control/oldrc.c
index d33a74c62c..37eea02a0f 100644
--- a/modules/control/oldrc.c
+++ b/modules/control/oldrc.c
@@ -1323,14 +1323,14 @@ static int Playlist( vlc_object_t *p_this, char const *psz_cmd,
     else if( !strcmp( psz_cmd, "playlist" ) )
     {
         msg_rc( "+----[ Playlist ]" );
-        print_playlist( p_intf, p_playlist->p_root, 0 );
+        print_playlist( p_intf, &p_playlist->root, 0 );
         msg_rc( "+----[ End of playlist ]" );
     }
 
     else if( !strcmp( psz_cmd, "sort" ))
     {
         PL_LOCK;
-        playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root,
+        playlist_RecursiveNodeSort( p_playlist, &p_playlist->root,
                                     SORT_ARTIST, ORDER_NORMAL );
         PL_UNLOCK;
     }
diff --git a/modules/gui/macosx/VLCMainWindow.m b/modules/gui/macosx/VLCMainWindow.m
index c9622c0588..bb960e82b8 100644
--- a/modules/gui/macosx/VLCMainWindow.m
+++ b/modules/gui/macosx/VLCMainWindow.m
@@ -1145,7 +1145,7 @@ static const float f_min_window_height = 307.;
         }
     } else {
         PL_LOCK;
-        playlist_item_t *pl_item = playlist_ChildSearchName(p_playlist->p_root, [[item untranslatedTitle] UTF8String]);
+        playlist_item_t *pl_item = playlist_ChildSearchName(&p_playlist->root, [[item untranslatedTitle] UTF8String]);
         if (pl_item != NULL)
             [[[[VLCMain sharedInstance] playlist] model] changeRootItem:pl_item];
 
diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c
index dd45ee82ce..0d5370425d 100644
--- a/modules/gui/ncurses.c
+++ b/modules/gui/ncurses.c
@@ -416,7 +416,7 @@ static void PlaylistRebuild(intf_thread_t *intf)
     playlist_t *p_playlist = pl_Get(intf);
 
     PlaylistDestroy(sys);
-    PlaylistAddNode(sys, p_playlist->p_root, "");
+    PlaylistAddNode(sys, &p_playlist->root, "");
 }
 
 static int ItemChanged(vlc_object_t *p_this, const char *variable,
@@ -1341,7 +1341,7 @@ static bool HandlePlaylistKey(intf_thread_t *intf, int key)
     case 'o':
     case 'O':
         playlist_Lock(p_playlist);
-        playlist_RecursiveNodeSort(p_playlist, p_playlist->p_root,
+        playlist_RecursiveNodeSort(p_playlist, &p_playlist->root,
                                    SORT_TITLE_NODES_FIRST,
                                    (key == 'o')? ORDER_NORMAL : ORDER_REVERSE);
         sys->need_update = true;
diff --git a/modules/gui/qt/components/playlist/selector.cpp b/modules/gui/qt/components/playlist/selector.cpp
index c02a024b24..1d41f7f6af 100644
--- a/modules/gui/qt/components/playlist/selector.cpp
+++ b/modules/gui/qt/components/playlist/selector.cpp
@@ -390,7 +390,7 @@ void PLSelector::setSource( QTreeWidgetItem *item )
     {
         /* Find the right item for the SD */
         /* FIXME: searching by name - what could possibly go wrong? */
-        pl_item = playlist_ChildSearchName( THEPL->p_root,
+        pl_item = playlist_ChildSearchName( &(THEPL->root),
             vlc_gettext(qtu(item->data(0, LONGNAME_ROLE).toString())) );
 
         /* Podcasts */
diff --git a/modules/gui/qt/dialogs/messages.cpp b/modules/gui/qt/dialogs/messages.cpp
index b959622e04..d6a0ecc992 100644
--- a/modules/gui/qt/dialogs/messages.cpp
+++ b/modules/gui/qt/dialogs/messages.cpp
@@ -378,7 +378,7 @@ void MessagesDialog::updatePLTree()
 
     {
         vlc_playlist_locker pl_lock ( THEPL );
-        pldebugTree->addTopLevelItem( PLWalk( p_playlist->p_root ) );
+        pldebugTree->addTopLevelItem( PLWalk( &p_playlist->root ) );
     }
 
     pldebugTree->expandAll();
diff --git a/modules/gui/skins2/commands/cmd_playtree.cpp b/modules/gui/skins2/commands/cmd_playtree.cpp
index dbbfd2c975..626ba09b03 100644
--- a/modules/gui/skins2/commands/cmd_playtree.cpp
+++ b/modules/gui/skins2/commands/cmd_playtree.cpp
@@ -38,7 +38,7 @@ void CmdPlaytreeSort::execute()
     /// \todo Choose the correct view
     playlist_t *p_playlist = getPL();
     PL_LOCK;
-    playlist_RecursiveNodeSort( p_playlist, p_playlist->p_root,
+    playlist_RecursiveNodeSort( p_playlist, &p_playlist->root,
                                 SORT_TITLE, ORDER_NORMAL );
     PL_UNLOCK;
 
diff --git a/modules/gui/skins2/vars/playtree.cpp b/modules/gui/skins2/vars/playtree.cpp
index 90012db9d5..768dd24907 100644
--- a/modules/gui/skins2/vars/playtree.cpp
+++ b/modules/gui/skins2/vars/playtree.cpp
@@ -256,9 +256,9 @@ void Playtree::buildTree()
     clear();
     playlist_Lock( m_pPlaylist );
 
-    for( int i = 0; i < m_pPlaylist->p_root->i_children; i++ )
+    for( int i = 0; i < m_pPlaylist->root.i_children; i++ )
     {
-        buildNode( m_pPlaylist->p_root->pp_children[i], *this );
+        buildNode( m_pPlaylist->root.pp_children[i], *this );
     }
 
     playlist_Unlock( m_pPlaylist );
diff --git a/modules/lua/libs/playlist.c b/modules/lua/libs/playlist.c
index 989ea36dc8..f7770e8a38 100644
--- a/modules/lua/libs/playlist.c
+++ b/modules/lua/libs/playlist.c
@@ -296,11 +296,11 @@ static int vlclua_playlist_get( lua_State *L )
               || !strcasecmp( psz_what, "media library" ) )
             p_item = p_playlist->p_media_library;
         else if( !strcasecmp( psz_what, "root" ) )
-            p_item = p_playlist->p_root;
+            p_item = &p_playlist->root;
         else
         {
             /* currently, psz_what must be SD module's longname! */
-            p_item = playlist_ChildSearchName( p_playlist->p_root, psz_what );
+            p_item = playlist_ChildSearchName( &p_playlist->root, psz_what );
 
             if( !p_item )
             {
@@ -311,7 +311,7 @@ static int vlclua_playlist_get( lua_State *L )
     }
     else
     {
-        p_item = p_playlist->p_root;
+        p_item = &p_playlist->root;
     }
     push_playlist_item( L, p_item );
     PL_UNLOCK;
@@ -323,8 +323,8 @@ static int vlclua_playlist_search( lua_State *L )
     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
     const char *psz_string = luaL_optstring( L, 1, "" );
     PL_LOCK;
-    playlist_LiveSearchUpdate( p_playlist, p_playlist->p_root, psz_string, true );
-    push_playlist_item( L, p_playlist->p_root );
+    playlist_LiveSearchUpdate( p_playlist, &p_playlist->root, psz_string, true );
+    push_playlist_item( L, &p_playlist->root );
     PL_UNLOCK;
     return 1;
 }
diff --git a/src/playlist/engine.c b/src/playlist/engine.c
index a295b80cba..3fbf9dad41 100644
--- a/src/playlist/engine.c
+++ b/src/playlist/engine.c
@@ -233,24 +233,31 @@ playlist_t *playlist_Create( vlc_object_t *p_parent )
     pl_priv(p_playlist)->b_tree = var_InheritBool( p_parent, "playlist-tree" );
     pl_priv(p_playlist)->b_preparse = var_InheritBool( p_parent, "auto-preparse" );
 
+    p_playlist->root.p_input = NULL;
+    p_playlist->root.pp_children = NULL;
+    p_playlist->root.i_children = 0;
+    p_playlist->root.i_nb_played = 0;
+    p_playlist->root.i_id = 0;
+    p_playlist->root.i_flags = 0;
+
     /* Create the root, playing items and meida library nodes */
-    playlist_item_t *root, *playing, *ml;
+    playlist_item_t *playing, *ml;
 
     PL_LOCK;
-    root = playlist_NodeCreate( p_playlist, NULL, NULL, PLAYLIST_END, 0 );
-    playing = playlist_NodeCreate( p_playlist, _( "Playlist" ), root,
-                                   PLAYLIST_END, PLAYLIST_RO_FLAG | PLAYLIST_NO_INHERIT_FLAG );
+    playing = playlist_NodeCreate( p_playlist, _( "Playlist" ),
+                                   &p_playlist->root, PLAYLIST_END,
+                                   PLAYLIST_RO_FLAG|PLAYLIST_NO_INHERIT_FLAG );
     if( var_InheritBool( p_parent, "media-library") )
-        ml = playlist_NodeCreate( p_playlist, _( "Media Library" ), root,
-                                  PLAYLIST_END, PLAYLIST_RO_FLAG | PLAYLIST_NO_INHERIT_FLAG );
+        ml = playlist_NodeCreate( p_playlist, _( "Media Library" ),
+                                  &p_playlist->root, PLAYLIST_END,
+                                  PLAYLIST_RO_FLAG|PLAYLIST_NO_INHERIT_FLAG );
     else
         ml = NULL;
     PL_UNLOCK;
 
-    if( unlikely(root == NULL || playing == NULL) )
+    if( unlikely(playing == NULL) )
         abort();
 
-    p_playlist->p_root = root;
     p_playlist->p_playing = playing;
     p_playlist->p_media_library = ml;
 
@@ -333,7 +340,10 @@ void playlist_Destroy( playlist_t *p_playlist )
     ARRAY_RESET( p_playlist->current );
 
     /* Remove all remaining items */
-    playlist_NodeDelete( p_playlist, p_playlist->p_root, true );
+    if( p_playlist->p_media_library != NULL )
+        playlist_NodeDelete( p_playlist, p_playlist->p_media_library, true );
+    playlist_NodeDelete( p_playlist, p_playlist->p_playing, true );
+    assert( p_playlist->root.i_children <= 0 );
     PL_UNLOCK;
 
     vlc_cond_destroy( &p_sys->signal );
diff --git a/src/playlist/services_discovery.c b/src/playlist/services_discovery.c
index 1b4ed14c6c..0435a9fc0c 100644
--- a/src/playlist/services_discovery.c
+++ b/src/playlist/services_discovery.c
@@ -54,7 +54,7 @@ static void playlist_sd_item_added(services_discovery_t *sd,
 
     playlist_Lock(playlist);
     if (sds->node == NULL)
-        sds->node = playlist_NodeCreate(playlist, longname, playlist->p_root,
+        sds->node = playlist_NodeCreate(playlist, longname, &playlist->root,
                                         PLAYLIST_END,
                                         PLAYLIST_RO_FLAG|PLAYLIST_SKIP_FLAG);
 
@@ -137,7 +137,7 @@ int playlist_ServicesDiscoveryAdd(playlist_t *playlist, const char *chain)
      * has not discovered any item. */
     if (sds->node == NULL && sds->sd->description != NULL)
         sds->node = playlist_NodeCreate(playlist, sds->sd->description,
-                                        playlist->p_root, PLAYLIST_END,
+                                        &playlist->root, PLAYLIST_END,
                                         PLAYLIST_RO_FLAG|PLAYLIST_SKIP_FLAG);
 
     TAB_APPEND(pl_priv(playlist)->i_sds, pl_priv(playlist)->pp_sds, sds);
diff --git a/src/playlist/tree.c b/src/playlist/tree.c
index 10f3be994a..697e918d65 100644
--- a/src/playlist/tree.c
+++ b/src/playlist/tree.c
@@ -75,8 +75,7 @@ playlist_item_t * playlist_NodeCreate( playlist_t *p_playlist,
 
     if( p_item == NULL )  return NULL;
 
-    if( p_parent != NULL )
-        playlist_NodeInsert( p_parent, p_item, i_pos );
+    playlist_NodeInsert( p_parent, p_item, i_pos );
     playlist_SendAddNotify( p_playlist, p_item );
 
     p_item->i_flags |= i_flags;
-- 
2.11.0



More information about the vlc-devel mailing list