]> git.sesse.net Git - vlc/commitdiff
Second attempt to use a dynamic array shortcuts (this save some memory).
authorRémi Duraffort <ivoire@videolan.org>
Thu, 22 Apr 2010 18:41:10 +0000 (20:41 +0200)
committerRémi Duraffort <ivoire@videolan.org>
Sat, 24 Apr 2010 06:34:13 +0000 (08:34 +0200)
src/libvlc.c
src/modules/cache.c
src/modules/entry.c
src/modules/modules.c
src/modules/modules.h

index b11f40fc1ef69ddb86bcd93c275bc0f8468864d6..8690ee2a1c28d4e4cc9ef8e190b9a5ebb37968c7 100644 (file)
@@ -1423,15 +1423,15 @@ static void Usage( libvlc_int_t *p_this, char const *psz_search )
             ( b_strict ? strcmp( psz_search, p_parser->psz_object_name )
                        : !strstr( p_parser->psz_object_name, psz_search ) ) )
         {
-            char *const *pp_shortcut = p_parser->pp_shortcuts;
-            while( *pp_shortcut )
+            char *const *pp_shortcuts = p_parser->pp_shortcuts;
+            int i;
+            for( i = 0; i < p_parser->i_shortcuts; i++ )
             {
-                if( b_strict ? !strcmp( psz_search, *pp_shortcut )
-                             : !!strstr( *pp_shortcut, psz_search ) )
+                if( b_strict ? !strcmp( psz_search, pp_shortcuts[i] )
+                             : !!strstr( pp_shortcuts[i], psz_search ) )
                     break;
-                pp_shortcut ++;
             }
-            if( !*pp_shortcut )
+            if( i == p_parser->i_shortcuts )
                 continue;
         }
 
@@ -1861,19 +1861,18 @@ static void ListModules( libvlc_int_t *p_this, bool b_verbose )
 
         if( b_verbose )
         {
-            char *const *pp_shortcut = p_parser->pp_shortcuts;
-            while( *pp_shortcut )
+            char *const *pp_shortcuts = p_parser->pp_shortcuts;
+            for( int i = 0; i < p_parser->i_shortcuts; i++ )
             {
-                if( strcmp( *pp_shortcut, p_parser->psz_object_name ) )
+                if( strcmp( pp_shortcuts[i], p_parser->psz_object_name ) )
                 {
                     if( b_color )
                         utf8_fprintf( stdout, CYAN"   s %s\n"GRAY,
-                                      *pp_shortcut );
+                                      pp_shortcuts[i] );
                     else
                         utf8_fprintf( stdout, "   s %s\n",
-                                      *pp_shortcut );
+                                      pp_shortcuts[i] );
                 }
-                pp_shortcut++;
             }
             if( p_parser->psz_capability )
             {
index c6e314ff9ea22913f9a85a9181f2127d1c35217b..f55d43175237eccdd8c771760160f1840a26960d 100644 (file)
@@ -253,10 +253,20 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
         LOAD_STRING( pp_cache[i]->p_module->psz_shortname );
         LOAD_STRING( pp_cache[i]->p_module->psz_longname );
         LOAD_STRING( pp_cache[i]->p_module->psz_help );
-        for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
+
+        LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
+        if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
+            goto error;
+        else if( pp_cache[i]->p_module->i_shortcuts == 0 )
+            pp_cache[i]->p_module->pp_shortcuts = NULL;
+        else
         {
-            LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
+            pp_cache[i]->p_module->pp_shortcuts =
+                    xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts );
+            for( j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ )
+                LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
         }
+
         LOAD_STRING( pp_cache[i]->p_module->psz_capability );
         LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score );
         LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
@@ -277,14 +287,24 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
         {
             module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module );
             free( p_module->psz_object_name );
+            free( p_module->pp_shortcuts );
             LOAD_STRING( p_module->psz_object_name );
             LOAD_STRING( p_module->psz_shortname );
             LOAD_STRING( p_module->psz_longname );
             LOAD_STRING( p_module->psz_help );
-            for( j = 0; j < MODULE_SHORTCUT_MAX; j++ )
+
+            LOAD_IMMEDIATE( p_module->i_shortcuts );
+            if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX )
+                goto error;
+            else if( p_module->i_shortcuts == 0 )
+                p_module->pp_shortcuts = NULL;
+            else
             {
-                LOAD_STRING( p_module->pp_shortcuts[j] ); // FIX
+                p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts );
+                for( j = 0; j < p_module->i_shortcuts; j++ )
+                    LOAD_STRING( p_module->pp_shortcuts[j] );
             }
+
             LOAD_STRING( p_module->psz_capability );
             LOAD_IMMEDIATE( p_module->i_score );
             LOAD_IMMEDIATE( p_module->b_unloadable );
@@ -545,10 +565,10 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
         SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
         SAVE_STRING( pp_cache[i]->p_module->psz_longname );
         SAVE_STRING( pp_cache[i]->p_module->psz_help );
-        for (unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++)
-        {
-            SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); // FIX
-        }
+        SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts );
+        for (unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++)
+            SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] );
+
         SAVE_STRING( pp_cache[i]->p_module->psz_capability );
         SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score );
         SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable );
@@ -590,8 +610,9 @@ static int CacheSaveSubmodule( FILE *file, const module_t *p_module )
     SAVE_STRING( p_module->psz_shortname );
     SAVE_STRING( p_module->psz_longname );
     SAVE_STRING( p_module->psz_help );
-    for( unsigned j = 0; j < MODULE_SHORTCUT_MAX; j++ )
-         SAVE_STRING( p_module->pp_shortcuts[j] ); // FIXME
+    SAVE_IMMEDIATE( p_module->i_shortcuts );
+    for( unsigned j = 0; j < p_module->i_shortcuts; j++ )
+         SAVE_STRING( p_module->pp_shortcuts[j] );
 
     SAVE_STRING( p_module->psz_capability );
     SAVE_IMMEDIATE( p_module->i_score );
index 0834ff9adf146f6e62be51a76c73af4f0cef1b24..e0b48d293faed0f5915388378fe527537b6f05ac 100644 (file)
@@ -37,6 +37,7 @@ static void vlc_module_destruct (gc_object_t *obj)
 {
     module_t *module = vlc_priv (obj, module_t);
 
+    free (module->pp_shortcuts);
     free (module->psz_object_name);
     free (module);
 }
@@ -59,8 +60,8 @@ module_t *vlc_module_create (vlc_object_t *obj)
     module->psz_shortname = NULL;
     module->psz_longname = (char*)default_name;
     module->psz_help = NULL;
-    for (unsigned i = 0; i < MODULE_SHORTCUT_MAX; i++)
-        module->pp_shortcuts[i] = NULL;
+    module->pp_shortcuts = NULL;
+    module->i_shortcuts = 0;
     module->psz_capability = (char*)"";
     module->i_score = 1;
     module->b_unloadable = true;
@@ -85,6 +86,7 @@ module_t *vlc_module_create (vlc_object_t *obj)
 static void vlc_submodule_destruct (gc_object_t *obj)
 {
     module_t *module = vlc_priv (obj, module_t);
+    free (module->pp_shortcuts);
     free (module->psz_object_name);
     free (module);
 }
@@ -105,9 +107,9 @@ module_t *vlc_submodule_create (module_t *module)
     module->submodule_count++;
 
     /* Muahahaha! Heritage! Polymorphism! Ugliness!! */
+    submodule->pp_shortcuts = malloc( sizeof( char ** ) );
     submodule->pp_shortcuts[0] = module->pp_shortcuts[0]; /* object name */
-    for (unsigned i = 1; i < MODULE_SHORTCUT_MAX; i++)
-        submodule->pp_shortcuts[i] = NULL;
+    submodule->i_shortcuts = 1;
 
     submodule->psz_object_name = strdup( module->psz_object_name );
     submodule->psz_shortname = module->psz_shortname;
@@ -179,12 +181,9 @@ int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
 
         case VLC_MODULE_SHORTCUT:
         {
-            unsigned i;
-            for (i = 0; module->pp_shortcuts[i] != NULL; i++);
-                if (i >= (MODULE_SHORTCUT_MAX - 1))
-                    break;
-
-            module->pp_shortcuts[i] = va_arg (ap, char *);
+            const char *psz_new = va_arg (ap, char*);
+            module->pp_shortcuts = realloc (module->pp_shortcuts, sizeof( char ** ) * (module->i_shortcuts + 1));
+            module->pp_shortcuts[module->i_shortcuts++] = psz_new;
             break;
         }
 
@@ -213,7 +212,10 @@ int vlc_plugin_set (module_t *module, module_config_t *item, int propid, ...)
             const char *value = va_arg (ap, const char *);
             free( module->psz_object_name );
             module->psz_object_name = strdup( value );
+            module->pp_shortcuts = malloc( sizeof( char ** ) );
             module->pp_shortcuts[0] = (char*)value; /* dooh! */
+            module->i_shortcuts = 1;
+
             if (module->psz_longname == default_name)
                 module->psz_longname = (char*)value; /* dooh! */
             break;
index e4929523a4077508ecb0d73d88bc8da5d51d4a4c..6020b14683fc893b6248c873a19e2a0e34d43612 100644 (file)
@@ -481,7 +481,7 @@ module_t * module_need( vlc_object_t *p_this, const char *psz_capability,
 
             for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
             {
-                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
+                for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
                 {
                     char *c;
                     if( ( c = strchr( name, '@' ) )
@@ -691,9 +691,7 @@ module_t *module_find_by_shortcut (const char *psz_shortcut)
 
     for (size_t i = 0; (module = list[i]) != NULL; i++)
     {
-        for (size_t j = 0;
-             (module->pp_shortcuts[j] != NULL) && (j < MODULE_SHORTCUT_MAX);
-             j++)
+        for (size_t j = 0; j < module->i_shortcuts; j++)
         {
             if (!strcmp (module->pp_shortcuts[j], psz_shortcut))
             {
@@ -1054,12 +1052,9 @@ static module_t * AllocatePlugin( vlc_object_t * p_this, const char *psz_file )
  *****************************************************************************/
 static void DupModule( module_t *p_module )
 {
-    char **pp_shortcut;
-
-    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
-    {
-        *pp_shortcut = strdup( *pp_shortcut );
-    }
+    char **pp_shortcuts = p_module->pp_shortcuts;
+    for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
+        pp_shortcuts[i] = strdup( p_module->pp_shortcuts[i] );
 
     /* We strdup() these entries so that they are still valid when the
      * module is unloaded. */
@@ -1082,15 +1077,13 @@ static void DupModule( module_t *p_module )
  *****************************************************************************/
 static void UndupModule( module_t *p_module )
 {
-    char **pp_shortcut;
+    char **pp_shortcuts = p_module->pp_shortcuts;
 
     for (module_t *subm = p_module->submodule; subm; subm = subm->next)
         UndupModule (subm);
 
-    for( pp_shortcut = p_module->pp_shortcuts ; *pp_shortcut ; pp_shortcut++ )
-    {
-        free( *pp_shortcut );
-    }
+    for( unsigned i = 0; i < p_module->i_shortcuts; i++ )
+        free( pp_shortcuts[i] );
 
     free( p_module->psz_capability );
     FREENULL( p_module->psz_shortname );
index 039cefa301f595e47fc01a2c1618d2306938180c..a566019ffdbf698c3ef7469c670e7d1598c15b7c 100644 (file)
@@ -100,6 +100,10 @@ struct module_t
     module_t   *submodule;
     unsigned    submodule_count;
 
+    /** Shortcuts to the module */
+    unsigned    i_shortcuts;
+    char        **pp_shortcuts;
+
     /*
      * Variables set by the module to identify itself
      */
@@ -107,9 +111,6 @@ struct module_t
     char *psz_longname;                   /**< Module descriptive name */
     char *psz_help;        /**< Long help string for "special" modules */
 
-    /** Shortcuts to the module */
-    char *pp_shortcuts[ MODULE_SHORTCUT_MAX ];
-
     char    *psz_capability;                                 /**< Capability */
     int      i_score;                          /**< Score for the capability */