[vlc-devel] [PATCH 1/2] input/mrl_helpers: remove usage of vlc_array_{new, destroy} vlc_array_new

Filip Roséen filip at atch.se
Tue Feb 21 20:10:11 CET 2017


---
 src/input/input.c       | 14 +++++++-------
 src/input/mrl_helpers.h | 24 +++++++++++++-----------
 src/test/mrl_helpers.c  |  9 ++++-----
 3 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/src/input/input.c b/src/input/input.c
index 5a28c8a066..cfd0e50475 100644
--- a/src/input/input.c
+++ b/src/input/input.c
@@ -2261,7 +2261,7 @@ static int
 InputStreamHandleAnchor( input_source_t *source, stream_t **stream,
                          char const *anchor )
 {
-    vlc_array_t* identifiers = NULL;
+    vlc_array_t identifiers;
     char const* extra;
 
     if( mrl_FragmentSplit( &identifiers, &extra, anchor ) )
@@ -2274,9 +2274,9 @@ InputStreamHandleAnchor( input_source_t *source, stream_t **stream,
         &source->i_title_start, &source->i_title_end,
         &source->i_seekpoint_start, &source->i_seekpoint_end );
 
-    while( vlc_array_count( identifiers ) )
+    while( vlc_array_count( &identifiers ) )
     {
-        char* id = vlc_array_item_at_index( identifiers, 0 );
+        char* id = vlc_array_item_at_index( &identifiers, 0 );
 
         if( vlc_stream_extractor_Attach( stream, id, NULL ) )
         {
@@ -2286,16 +2286,16 @@ InputStreamHandleAnchor( input_source_t *source, stream_t **stream,
         else
             msg_Dbg( source, "successfully located entity '%s' within stream", id );
 
-        vlc_array_remove( identifiers, 0 );
+        vlc_array_remove( &identifiers, 0 );
         free( id );
     }
 
-    int remaining = vlc_array_count( identifiers );
+    int remaining = vlc_array_count( &identifiers );
 
     for( int i = 0; i < remaining; ++i )
-        free( vlc_array_item_at_index( identifiers, i ) );
+        free( vlc_array_item_at_index( &identifiers, i ) );
 
-    vlc_array_destroy( identifiers );
+    vlc_array_clear( &identifiers );
 
     if( remaining == 0 )
     {
diff --git a/src/input/mrl_helpers.h b/src/input/mrl_helpers.h
index f8875846cc..09916c6070 100644
--- a/src/input/mrl_helpers.h
+++ b/src/input/mrl_helpers.h
@@ -101,21 +101,24 @@ mrl_EscapeFragmentIdentifier( char** out, char const* payload )
  * See the \link mrl MRL-specification\endlink for detailed
  * information regarding how `payload` will be interpreted.
  *
- * \param[out] out_items `*out_items` contains the individual entries on success
+ * \warning On success, the caller has ownership of the contents of *out_items
+ *          which means that it is responsible for freeing the individual
+ *          elements, as well as cleaning the array itself.
+ *
+ * \param[out] out_items storage for a vlc_array_t that will contain the
+ *                       parsed identifiers on success.
  * \param[out] out_extra `*out_extra` will point to any remaining data (if any)
  * \param[in] payload the data to parse
  * \return VLC_SUCCESS on success, an error-code on failure
  **/
 static inline int
-mrl_FragmentSplit( vlc_array_t** out_items,
+mrl_FragmentSplit( vlc_array_t* out_items,
                    char const** out_extra,
                    char const* payload )
 {
-    vlc_array_t* items = vlc_array_new();
     char const* extra = NULL;
 
-    if( unlikely( !items ) )
-        return VLC_ENOMEM;
+    vlc_array_init( out_items );
 
     while( strncmp( payload, "!/", 2 ) == 0 )
     {
@@ -127,7 +130,7 @@ mrl_FragmentSplit( vlc_array_t** out_items,
         if( unlikely( !decoded ) || !vlc_uri_decode( decoded ) )
             goto error;
 
-        vlc_array_append( items, decoded );
+        vlc_array_append( out_items, decoded );
         payload += len;
     }
 
@@ -136,20 +139,19 @@ mrl_FragmentSplit( vlc_array_t** out_items,
         if( *payload == '!' )
             goto error;
 
-        if( *payload == '?' && vlc_array_count( items ) )
+        if( *payload == '?' && vlc_array_count( out_items ) )
             ++payload;
 
         extra = payload;
     }
 
-    *out_items = items;
     *out_extra = extra;
     return VLC_SUCCESS;
 
 error:
-    for( size_t i = 0; i < vlc_array_count( items ); ++i )
-        free( vlc_array_item_at_index( items, i ) );
-    vlc_array_destroy( items );
+    for( size_t i = 0; i < vlc_array_count( out_items ); ++i )
+        free( vlc_array_item_at_index( out_items, i ) );
+    vlc_array_clear( out_items );
     return VLC_EGENERIC;;
 }
 
diff --git a/src/test/mrl_helpers.c b/src/test/mrl_helpers.c
index 093db94bcb..00b29a4035 100644
--- a/src/test/mrl_helpers.c
+++ b/src/test/mrl_helpers.c
@@ -70,13 +70,12 @@ int main (void)
 {
     for (size_t i = 0; i < ARRAY_SIZE(testcase); ++i)
     {
-        vlc_array_t *out;
+        vlc_array_t out;
         const char *extra = NULL;
         int ret = mrl_FragmentSplit(&out, &extra, testcase[i].payload);
         if (testcase[i].success)
         {
             assert(ret == VLC_SUCCESS);
-            assert(out != NULL);
             if (extra != NULL)
                 assert(strcmp(extra, testcase[i].extra) == 0);
             else
@@ -85,8 +84,8 @@ int main (void)
             const char *p = testcase[i].payload + 2;
             for (int j = 0; testcase[i].results[j] != NULL; ++j)
             {
-                assert(j < vlc_array_count(out) && j < MAX_RESULT);
-                char *res = vlc_array_item_at_index(out, j);
+                assert(j < vlc_array_count(&out) && j < MAX_RESULT);
+                char *res = vlc_array_item_at_index(&out, j);
 
                 assert(strcmp(testcase[i].results[j], res) == 0);
 
@@ -99,7 +98,7 @@ int main (void)
                 free(res_escaped);
                 free(res);
             }
-            vlc_array_destroy(out);
+            vlc_array_clear(&out);
         }
         else
         {
-- 
2.11.1



More information about the vlc-devel mailing list