Commit d14f6b76 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Factor code to add an entry to the plugins cache in memory

parent 3fa02df3
...@@ -169,9 +169,7 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r ) ...@@ -169,9 +169,7 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
return 0; return 0;
} }
module_cache_t **pp_cache = malloc( i_cache * sizeof(*pp_cache) ); module_cache_t **pp_cache = NULL;
if( pp_cache == NULL )
i_cache = 0; /* don't load anything */
#define LOAD_IMMEDIATE(a) \ #define LOAD_IMMEDIATE(a) \
if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error if( fread( (void *)&a, sizeof(char), sizeof(a), file ) != sizeof(a) ) goto error
...@@ -195,90 +193,96 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r ) ...@@ -195,90 +193,96 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
} \ } \
} }
for( size_t i = 0; i < i_cache; i++ ) for (size_t count = 0; count < i_cache;)
{ {
module_t *module;
uint16_t i_size; uint16_t i_size;
int i_submodules; int i_submodules;
pp_cache[i] = xmalloc( sizeof(module_cache_t) ); module = vlc_module_create();
/* Load common info */
LOAD_STRING( pp_cache[i]->path );
LOAD_IMMEDIATE( pp_cache[i]->mtime );
LOAD_IMMEDIATE( pp_cache[i]->size );
pp_cache[i]->p_module = vlc_module_create();
/* Load additional infos */ /* Load additional infos */
free( pp_cache[i]->p_module->psz_object_name ); free (module->psz_object_name);
LOAD_STRING( pp_cache[i]->p_module->psz_object_name ); LOAD_STRING(module->psz_object_name);
LOAD_STRING( pp_cache[i]->p_module->psz_shortname ); LOAD_STRING(module->psz_shortname);
LOAD_STRING( pp_cache[i]->p_module->psz_longname ); LOAD_STRING(module->psz_longname);
LOAD_STRING( pp_cache[i]->p_module->psz_help ); LOAD_STRING(module->psz_help);
LOAD_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts ); LOAD_IMMEDIATE(module->i_shortcuts);
if( pp_cache[i]->p_module->i_shortcuts > MODULE_SHORTCUT_MAX ) if (module->i_shortcuts > MODULE_SHORTCUT_MAX)
goto error; goto error;
else if( pp_cache[i]->p_module->i_shortcuts == 0 ) else if (module->i_shortcuts == 0)
pp_cache[i]->p_module->pp_shortcuts = NULL; module->pp_shortcuts = NULL;
else else
{ {
pp_cache[i]->p_module->pp_shortcuts = module->pp_shortcuts =
xmalloc( sizeof( char ** ) * pp_cache[i]->p_module->i_shortcuts ); xmalloc (sizeof (char **) * module->i_shortcuts);
for( unsigned j = 0; j < pp_cache[i]->p_module->i_shortcuts; j++ ) for (unsigned j = 0; j < module->i_shortcuts; j++)
LOAD_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); LOAD_STRING(module->pp_shortcuts[j]);
} }
LOAD_STRING( pp_cache[i]->p_module->psz_capability ); LOAD_STRING(module->psz_capability);
LOAD_IMMEDIATE( pp_cache[i]->p_module->i_score ); LOAD_IMMEDIATE(module->i_score);
LOAD_IMMEDIATE( pp_cache[i]->p_module->b_unloadable ); LOAD_IMMEDIATE(module->b_unloadable);
/* Config stuff */ /* Config stuff */
if( CacheLoadConfig( pp_cache[i]->p_module, file ) != VLC_SUCCESS ) if (CacheLoadConfig (module, file) != VLC_SUCCESS)
goto error; goto error;
LOAD_STRING( pp_cache[i]->p_module->psz_filename ); LOAD_STRING(module->psz_filename);
LOAD_STRING( pp_cache[i]->p_module->domain ); LOAD_STRING(module->domain);
if( pp_cache[i]->p_module->domain != NULL ) if (module->domain != NULL)
vlc_bindtextdomain( pp_cache[i]->p_module->domain ); vlc_bindtextdomain (module->domain);
LOAD_IMMEDIATE( i_submodules ); LOAD_IMMEDIATE( i_submodules );
while( i_submodules-- ) while( i_submodules-- )
{ {
module_t *p_module = vlc_submodule_create( pp_cache[i]->p_module ); module_t *submodule = vlc_submodule_create (module);
free( p_module->psz_object_name ); free (submodule->psz_object_name);
free( p_module->pp_shortcuts ); free (submodule->pp_shortcuts);
LOAD_STRING( p_module->psz_object_name ); LOAD_STRING(submodule->psz_object_name);
LOAD_STRING( p_module->psz_shortname ); LOAD_STRING(submodule->psz_shortname);
LOAD_STRING( p_module->psz_longname ); LOAD_STRING(submodule->psz_longname);
LOAD_STRING( p_module->psz_help ); LOAD_STRING(submodule->psz_help);
LOAD_IMMEDIATE( p_module->i_shortcuts ); LOAD_IMMEDIATE(submodule->i_shortcuts);
if( p_module->i_shortcuts > MODULE_SHORTCUT_MAX ) if (submodule->i_shortcuts > MODULE_SHORTCUT_MAX)
goto error; goto error;
else if( p_module->i_shortcuts == 0 ) else if (submodule->i_shortcuts == 0)
p_module->pp_shortcuts = NULL; submodule->pp_shortcuts = NULL;
else else
{ {
p_module->pp_shortcuts = xmalloc( sizeof( char ** ) * p_module->i_shortcuts ); submodule->pp_shortcuts =
for( unsigned j = 0; j < p_module->i_shortcuts; j++ ) xmalloc (sizeof (char **) * submodule->i_shortcuts);
LOAD_STRING( p_module->pp_shortcuts[j] ); for (unsigned j = 0; j < submodule->i_shortcuts; j++)
LOAD_STRING(submodule->pp_shortcuts[j]);
} }
LOAD_STRING( p_module->psz_capability ); LOAD_STRING(submodule->psz_capability);
LOAD_IMMEDIATE( p_module->i_score ); LOAD_IMMEDIATE(submodule->i_score);
LOAD_IMMEDIATE( p_module->b_unloadable ); LOAD_IMMEDIATE(submodule->b_unloadable);
LOAD_STRING( p_module->domain ); LOAD_STRING(submodule->domain);
} }
char *path;
struct stat st;
/* Load common info */
LOAD_STRING(path);
LOAD_IMMEDIATE(st.st_mtime);
LOAD_IMMEDIATE(st.st_size);
CacheAdd (&pp_cache, &count, path, &st, module);
free (path);
/* TODO: deal with errors */
} }
fclose( file ); fclose( file );
*r = pp_cache; *r = pp_cache;
return i_cache; return i_cache;
error: error:
msg_Warn( p_this, "plugins cache not loaded (corrupted)" ); msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
/* TODO: cleanup */ /* TODO: cleanup */
...@@ -494,11 +498,6 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache, ...@@ -494,11 +498,6 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
{ {
uint32_t i_submodule; uint32_t i_submodule;
/* Save common info */
SAVE_STRING( pp_cache[i]->path );
SAVE_IMMEDIATE( pp_cache[i]->mtime );
SAVE_IMMEDIATE( pp_cache[i]->size );
/* Save additional infos */ /* Save additional infos */
SAVE_STRING( pp_cache[i]->p_module->psz_object_name ); SAVE_STRING( pp_cache[i]->p_module->psz_object_name );
SAVE_STRING( pp_cache[i]->p_module->psz_shortname ); SAVE_STRING( pp_cache[i]->p_module->psz_shortname );
...@@ -523,6 +522,11 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache, ...@@ -523,6 +522,11 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
SAVE_IMMEDIATE( i_submodule ); SAVE_IMMEDIATE( i_submodule );
if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) ) if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
goto error; goto error;
/* Save common info */
SAVE_STRING(pp_cache[i]->path);
SAVE_IMMEDIATE(pp_cache[i]->mtime);
SAVE_IMMEDIATE(pp_cache[i]->size);
} }
if (fflush (file)) /* flush libc buffers */ if (fflush (file)) /* flush libc buffers */
...@@ -661,4 +665,27 @@ module_t *CacheFind (module_cache_t *const *entries, size_t count, ...@@ -661,4 +665,27 @@ module_t *CacheFind (module_cache_t *const *entries, size_t count,
return NULL; return NULL;
} }
/** Adds entry to the cache */
int CacheAdd (module_cache_t ***cache, size_t *count,
const char *path, const struct stat *st, module_t *module)
{
module_cache_t **entries;
entries = realloc (*cache, (*count + 1) * sizeof (*entries));
if (unlikely(entries == NULL))
return -1;
*cache = entries;
entries[*count] = malloc (sizeof (**entries));
if (unlikely(entries[*count] == NULL))
return -1;
/* NOTE: strdup() could be avoided, but it would be a bit ugly */
entries[*count]->path = strdup (path);
entries[*count]->mtime = st->st_mtime;
entries[*count]->size = st->st_size;
entries[*count]->p_module = module;
(*count)++;
return 0;
}
#endif /* HAVE_DYNAMIC_PLUGINS */ #endif /* HAVE_DYNAMIC_PLUGINS */
...@@ -63,7 +63,7 @@ typedef struct ...@@ -63,7 +63,7 @@ typedef struct
unsigned i_usage; unsigned i_usage;
/* Plugins cache */ /* Plugins cache */
int i_cache; size_t i_cache;
module_cache_t **pp_cache; module_cache_t **pp_cache;
int i_loaded_cache; int i_loaded_cache;
...@@ -1010,20 +1010,8 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank, ...@@ -1010,20 +1010,8 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
return 0; return 0;
/* Add entry to cache */ /* Add entry to cache */
module_cache_t **pp_cache = p_bank->pp_cache; CacheAdd (&p_bank->pp_cache, &p_bank->i_cache, path, st, p_module);
/* TODO: deal with errors */
pp_cache = realloc_or_free( pp_cache, (p_bank->i_cache + 1) * sizeof(void *) );
if( pp_cache == NULL )
return -1;
pp_cache[p_bank->i_cache] = malloc( sizeof(module_cache_t) );
if( pp_cache[p_bank->i_cache] == NULL )
return -1;
pp_cache[p_bank->i_cache]->path = strdup( path );
pp_cache[p_bank->i_cache]->mtime = st->st_mtime;
pp_cache[p_bank->i_cache]->size = st->st_size;
pp_cache[p_bank->i_cache]->p_module = p_module;
p_bank->pp_cache = pp_cache;
p_bank->i_cache++;
return 0; return 0;
} }
......
...@@ -133,5 +133,7 @@ size_t CacheLoad (vlc_object_t *, const char *, module_cache_t ***); ...@@ -133,5 +133,7 @@ size_t CacheLoad (vlc_object_t *, const char *, module_cache_t ***);
void CacheSave (vlc_object_t *, const char *, module_cache_t **, size_t); void CacheSave (vlc_object_t *, const char *, module_cache_t **, size_t);
module_t *CacheFind (module_cache_t *const *, size_t, module_t *CacheFind (module_cache_t *const *, size_t,
const char *, const struct stat *); const char *, const struct stat *);
int CacheAdd (module_cache_t ***, size_t *,
const char *, const struct stat *, module_t *);
#endif /* !LIBVLC_MODULES_H */ #endif /* !LIBVLC_MODULES_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment