From 0b574db7fddd22289bedd007e6b382f92f96ea31 Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Duraffort?= Date: Thu, 22 Apr 2010 20:41:10 +0200 Subject: [PATCH] Second attempt to use a dynamic array shortcuts (this save some memory). --- src/libvlc.c | 23 +++++++++++------------ src/modules/cache.c | 41 +++++++++++++++++++++++++++++++---------- src/modules/entry.c | 22 ++++++++++++---------- src/modules/modules.c | 23 ++++++++--------------- src/modules/modules.h | 7 ++++--- 5 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/libvlc.c b/src/libvlc.c index b11f40fc1e..8690ee2a1c 100644 --- a/src/libvlc.c +++ b/src/libvlc.c @@ -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 ) { diff --git a/src/modules/cache.c b/src/modules/cache.c index c6e314ff9e..f55d431752 100644 --- a/src/modules/cache.c +++ b/src/modules/cache.c @@ -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 ); diff --git a/src/modules/entry.c b/src/modules/entry.c index 0834ff9adf..e0b48d293f 100644 --- a/src/modules/entry.c +++ b/src/modules/entry.c @@ -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; diff --git a/src/modules/modules.c b/src/modules/modules.c index e4929523a4..6020b14683 100644 --- a/src/modules/modules.c +++ b/src/modules/modules.c @@ -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 ); diff --git a/src/modules/modules.h b/src/modules/modules.h index 039cefa301..a566019ffd 100644 --- a/src/modules/modules.h +++ b/src/modules/modules.h @@ -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 */ -- 2.39.2