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

Remove one level of indirection in plugins cache, simplify

parent d14f6b76
...@@ -88,7 +88,7 @@ void CacheDelete( vlc_object_t *obj, const char *dir ) ...@@ -88,7 +88,7 @@ void CacheDelete( vlc_object_t *obj, const char *dir )
* actually load the dynamically loadable module. * actually load the dynamically loadable module.
* This allows us to only fully load plugins when they are actually used. * This allows us to only fully load plugins when they are actually used.
*/ */
size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r ) size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t **r )
{ {
char *psz_filename; char *psz_filename;
FILE *file; FILE *file;
...@@ -169,7 +169,7 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r ) ...@@ -169,7 +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 = NULL; module_cache_t *cache = NULL;
#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
...@@ -273,13 +273,13 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r ) ...@@ -273,13 +273,13 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
LOAD_IMMEDIATE(st.st_mtime); LOAD_IMMEDIATE(st.st_mtime);
LOAD_IMMEDIATE(st.st_size); LOAD_IMMEDIATE(st.st_size);
CacheAdd (&pp_cache, &count, path, &st, module); CacheAdd (&cache, &count, path, &st, module);
free (path); free (path);
/* TODO: deal with errors */ /* TODO: deal with errors */
} }
fclose( file ); fclose( file );
*r = pp_cache; *r = cache;
return i_cache; return i_cache;
error: error:
...@@ -399,13 +399,13 @@ static int CacheLoadConfig( module_t *p_module, FILE *file ) ...@@ -399,13 +399,13 @@ static int CacheLoadConfig( module_t *p_module, FILE *file )
return VLC_EGENERIC; return VLC_EGENERIC;
} }
static int CacheSaveBank( FILE *file, module_cache_t *const *, size_t ); static int CacheSaveBank( FILE *file, const module_cache_t *, size_t );
/** /**
* Saves a module cache to disk, and release cache data from memory. * Saves a module cache to disk, and release cache data from memory.
*/ */
void CacheSave (vlc_object_t *p_this, const char *dir, void CacheSave (vlc_object_t *p_this, const char *dir,
module_cache_t **entries, size_t n) module_cache_t *entries, size_t n)
{ {
char *filename = NULL, *tmpname = NULL; char *filename = NULL, *tmpname = NULL;
...@@ -446,17 +446,14 @@ out: ...@@ -446,17 +446,14 @@ out:
free (tmpname); free (tmpname);
for (size_t i = 0; i < n; i++) for (size_t i = 0; i < n; i++)
{ free (entries[i].path);
free (entries[i]->path);
free (entries[i]);
}
free (entries); free (entries);
} }
static int CacheSaveConfig (FILE *, const module_t *); static int CacheSaveConfig (FILE *, const module_t *);
static int CacheSaveSubmodule (FILE *, const module_t *); static int CacheSaveSubmodule (FILE *, const module_t *);
static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache, static int CacheSaveBank (FILE *file, const module_cache_t *cache,
size_t i_cache) size_t i_cache)
{ {
uint32_t i_file_size = 0; uint32_t i_file_size = 0;
...@@ -496,37 +493,38 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache, ...@@ -496,37 +493,38 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
for (unsigned i = 0; i < i_cache; i++) for (unsigned i = 0; i < i_cache; i++)
{ {
module_t *module = cache[i].p_module;
uint32_t i_submodule; uint32_t i_submodule;
/* Save additional infos */ /* Save additional infos */
SAVE_STRING( pp_cache[i]->p_module->psz_object_name ); SAVE_STRING(module->psz_object_name);
SAVE_STRING( pp_cache[i]->p_module->psz_shortname ); SAVE_STRING(module->psz_shortname);
SAVE_STRING( pp_cache[i]->p_module->psz_longname ); SAVE_STRING(module->psz_longname);
SAVE_STRING( pp_cache[i]->p_module->psz_help ); SAVE_STRING(module->psz_help);
SAVE_IMMEDIATE( pp_cache[i]->p_module->i_shortcuts ); SAVE_IMMEDIATE(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++)
SAVE_STRING( pp_cache[i]->p_module->pp_shortcuts[j] ); SAVE_STRING(module->pp_shortcuts[j]);
SAVE_STRING( pp_cache[i]->p_module->psz_capability ); SAVE_STRING(module->psz_capability);
SAVE_IMMEDIATE( pp_cache[i]->p_module->i_score ); SAVE_IMMEDIATE(module->i_score);
SAVE_IMMEDIATE( pp_cache[i]->p_module->b_unloadable ); SAVE_IMMEDIATE(module->b_unloadable);
/* Config stuff */ /* Config stuff */
if (CacheSaveConfig (file, pp_cache[i]->p_module)) if (CacheSaveConfig (file, module))
goto error; goto error;
SAVE_STRING( pp_cache[i]->p_module->psz_filename ); SAVE_STRING(module->psz_filename);
SAVE_STRING( pp_cache[i]->p_module->domain ); SAVE_STRING(module->domain);
i_submodule = pp_cache[i]->p_module->submodule_count; i_submodule = module->submodule_count;
SAVE_IMMEDIATE( i_submodule ); SAVE_IMMEDIATE( i_submodule );
if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) ) if (CacheSaveSubmodule (file, module->submodule))
goto error; goto error;
/* Save common info */ /* Save common info */
SAVE_STRING(pp_cache[i]->path); SAVE_STRING(cache[i].path);
SAVE_IMMEDIATE(pp_cache[i]->mtime); SAVE_IMMEDIATE(cache[i].mtime);
SAVE_IMMEDIATE(pp_cache[i]->size); SAVE_IMMEDIATE(cache[i].size);
} }
if (fflush (file)) /* flush libc buffers */ if (fflush (file)) /* flush libc buffers */
...@@ -644,21 +642,20 @@ void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module ) ...@@ -644,21 +642,20 @@ void CacheMerge( vlc_object_t *p_this, module_t *p_cache, module_t *p_module )
/** /**
* Looks up a plugin file in a table of cached plugins. * Looks up a plugin file in a table of cached plugins.
*/ */
module_t *CacheFind (module_cache_t *const *entries, size_t count, module_t *CacheFind (module_cache_t *cache, size_t count,
const char *path, const struct stat *st) const char *path, const struct stat *st)
{ {
while (count > 0) while (count > 0)
{ {
module_cache_t *entry = *(entries++); if (!strcmp (cache->path, path)
&& cache->mtime == st->st_mtime
if (!strcmp (entry->path, path) && cache->size == st->st_size)
&& entry->mtime == st->st_mtime
&& entry->size == st->st_size)
{ {
module_t *module = entry->p_module; module_t *module = cache->p_module;
entry->p_module = NULL; /* Return NULL next time */ cache->p_module = NULL; /* Return NULL next time */
return module; return module;
} }
cache++;
count--; count--;
} }
...@@ -666,25 +663,24 @@ module_t *CacheFind (module_cache_t *const *entries, size_t count, ...@@ -666,25 +663,24 @@ module_t *CacheFind (module_cache_t *const *entries, size_t count,
} }
/** Adds entry to the cache */ /** Adds entry to the cache */
int CacheAdd (module_cache_t ***cache, size_t *count, int CacheAdd (module_cache_t **cachep, size_t *countp,
const char *path, const struct stat *st, module_t *module) const char *path, const struct stat *st, module_t *module)
{ {
module_cache_t **entries; module_cache_t *cache = *cachep;
const size_t count = *countp;
entries = realloc (*cache, (*count + 1) * sizeof (*entries)); cache = realloc (cache, (count + 1) * sizeof (*cache));
if (unlikely(entries == NULL)) if (unlikely(cache == NULL))
return -1; return -1;
*cache = entries; *cachep = cache;
entries[*count] = malloc (sizeof (**entries)); cache += count;
if (unlikely(entries[*count] == NULL))
return -1;
/* NOTE: strdup() could be avoided, but it would be a bit ugly */ /* NOTE: strdup() could be avoided, but it would be a bit ugly */
entries[*count]->path = strdup (path); cache->path = strdup (path);
entries[*count]->mtime = st->st_mtime; cache->mtime = st->st_mtime;
entries[*count]->size = st->st_size; cache->size = st->st_size;
entries[*count]->p_module = module; cache->p_module = module;
(*count)++; *countp = count + 1;
return 0; return 0;
} }
......
...@@ -64,10 +64,10 @@ typedef struct ...@@ -64,10 +64,10 @@ typedef struct
/* Plugins cache */ /* Plugins cache */
size_t i_cache; size_t i_cache;
module_cache_t **pp_cache; module_cache_t *cache;
int i_loaded_cache; int i_loaded_cache;
module_cache_t **pp_loaded_cache; module_cache_t *loaded_cache;
module_t *head; module_t *head;
} module_bank_t; } module_bank_t;
...@@ -859,7 +859,7 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) ...@@ -859,7 +859,7 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank )
static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank, static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank,
const char *path, cache_mode_t mode ) const char *path, cache_mode_t mode )
{ {
module_cache_t **cache = NULL; module_cache_t *cache = NULL;
size_t count = 0; size_t count = 0;
switch( mode ) switch( mode )
...@@ -877,9 +877,9 @@ static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank, ...@@ -877,9 +877,9 @@ static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank,
msg_Dbg( p_this, "recursively browsing `%s'", path ); msg_Dbg( p_this, "recursively browsing `%s'", path );
/* TODO: pass as argument, remove this hack */ /* TODO: pass as argument, remove this hack */
p_bank->pp_cache = NULL; p_bank->cache = NULL;
p_bank->i_cache = 0; p_bank->i_cache = 0;
p_bank->pp_loaded_cache = cache; p_bank->loaded_cache = cache;
p_bank->i_loaded_cache = count; p_bank->i_loaded_cache = count;
/* Don't go deeper than 5 subdirectories */ /* Don't go deeper than 5 subdirectories */
AllocatePluginDir( p_this, p_bank, path, 5, mode ); AllocatePluginDir( p_this, p_bank, path, 5, mode );
...@@ -888,16 +888,14 @@ static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank, ...@@ -888,16 +888,14 @@ static void AllocatePluginPath( vlc_object_t *p_this, module_bank_t *p_bank,
{ {
case CACHE_USE: case CACHE_USE:
for( size_t i = 0; i < count; i++ ) for( size_t i = 0; i < count; i++ )
if( likely(cache[i] != NULL) ) {
{ if (cache[i].p_module != NULL)
if( cache[i]->p_module != NULL ) DeleteModule (p_bank, cache[i].p_module);
DeleteModule( p_bank, cache[i]->p_module ); free (cache[i].path);
free( cache[i]->path ); }
free( cache[i] );
}
free( cache ); free( cache );
case CACHE_RESET: case CACHE_RESET:
CacheSave (p_this, path, p_bank->pp_cache, p_bank->i_cache); CacheSave (p_this, path, p_bank->cache, p_bank->i_cache);
case CACHE_IGNORE: case CACHE_IGNORE:
break; break;
} }
...@@ -972,7 +970,7 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank, ...@@ -972,7 +970,7 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
p_module->psz_object_name, p_module->psz_longname ); */ p_module->psz_object_name, p_module->psz_longname ); */
/* Check our plugins cache first then load plugin if needed */ /* Check our plugins cache first then load plugin if needed */
if( mode == CACHE_USE ) if( mode == CACHE_USE )
p_module = CacheFind (p_bank->pp_loaded_cache, p_bank->i_loaded_cache, p_module = CacheFind (p_bank->loaded_cache, p_bank->i_loaded_cache,
path, st); path, st);
if( p_module == NULL ) if( p_module == NULL )
p_module = AllocatePlugin( p_this, path, true ); p_module = AllocatePlugin( p_this, path, true );
...@@ -1010,7 +1008,7 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank, ...@@ -1010,7 +1008,7 @@ 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 */
CacheAdd (&p_bank->pp_cache, &p_bank->i_cache, path, st, p_module); CacheAdd (&p_bank->cache, &p_bank->i_cache, path, st, p_module);
/* TODO: deal with errors */ /* TODO: deal with errors */
return 0; return 0;
} }
......
...@@ -129,11 +129,11 @@ void module_Unload (module_handle_t); ...@@ -129,11 +129,11 @@ void module_Unload (module_handle_t);
/* Plugins cache */ /* Plugins cache */
void CacheMerge (vlc_object_t *, module_t *, module_t *); void CacheMerge (vlc_object_t *, module_t *, module_t *);
void CacheDelete(vlc_object_t *, const char *); void CacheDelete(vlc_object_t *, const char *);
size_t CacheLoad (vlc_object_t *, const char *, module_cache_t ***); size_t CacheLoad (vlc_object_t *, const char *, module_cache_t **);
void CacheSave (vlc_object_t *, const char *, module_cache_t **, size_t); int CacheAdd (module_cache_t **, size_t *,
module_t *CacheFind (module_cache_t *const *, size_t,
const char *, const struct stat *);
int CacheAdd (module_cache_t ***, size_t *,
const char *, const struct stat *, module_t *); const char *, const struct stat *, module_t *);
void CacheSave (vlc_object_t *, const char *, module_cache_t *, size_t);
module_t *CacheFind (module_cache_t *, size_t,
const char *, const struct stat *);
#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