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

Keep only one cache file loaded at a time

Each cache file is now unloaded as soon as the cache directory scan is
complete. The data is not needed anymore past that point.
parent 468d5f4d
...@@ -79,28 +79,28 @@ void CacheDelete( vlc_object_t *obj, const char *dir ) ...@@ -79,28 +79,28 @@ void CacheDelete( vlc_object_t *obj, const char *dir )
free( path ); free( path );
} }
/***************************************************************************** /**
* LoadPluginsCache: loads the plugins cache file * Loads a plugins cache file.
***************************************************************************** *
* This function will load the plugin cache if present and valid. This cache * This function will load the plugin cache if present and valid. This cache
* will in turn be queried by AllocateAllPlugins() to see if it needs to * will in turn be queried by AllocateAllPlugins() to see if it needs to
* 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.
*****************************************************************************/ */
void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) 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;
int i_size, i_read; int i_size, i_read;
char p_cachestring[sizeof(CACHE_STRING)]; char p_cachestring[sizeof(CACHE_STRING)];
size_t i_cache; size_t i_cache;
module_cache_t **pp_cache = NULL;
int32_t i_file_size, i_marker; int32_t i_file_size, i_marker;
assert( dir != NULL ); assert( dir != NULL );
*r = NULL;
if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 ) if( asprintf( &psz_filename, "%s"DIR_SEP CACHE_NAME, dir ) == -1 )
return; return 0;
msg_Dbg( p_this, "loading plugins cache file %s", psz_filename ); msg_Dbg( p_this, "loading plugins cache file %s", psz_filename );
...@@ -110,7 +110,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -110,7 +110,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "cannot read %s (%m)", msg_Warn( p_this, "cannot read %s (%m)",
psz_filename ); psz_filename );
free( psz_filename ); free( psz_filename );
return; return 0;
} }
free( psz_filename ); free( psz_filename );
...@@ -121,7 +121,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -121,7 +121,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "This doesn't look like a valid plugins cache " msg_Warn( p_this, "This doesn't look like a valid plugins cache "
"(too short)" ); "(too short)" );
fclose( file ); fclose( file );
return; return 0;
} }
fseek( file, 0, SEEK_END ); fseek( file, 0, SEEK_END );
...@@ -130,7 +130,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -130,7 +130,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "This doesn't look like a valid plugins cache " msg_Warn( p_this, "This doesn't look like a valid plugins cache "
"(corrupted size)" ); "(corrupted size)" );
fclose( file ); fclose( file );
return; return 0;
} }
fseek( file, sizeof(i_file_size), SEEK_SET ); fseek( file, sizeof(i_file_size), SEEK_SET );
...@@ -142,7 +142,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -142,7 +142,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
{ {
msg_Warn( p_this, "This doesn't look like a valid plugins cache" ); msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
fclose( file ); fclose( file );
return; return 0;
} }
#ifdef DISTRO_VERSION #ifdef DISTRO_VERSION
...@@ -155,7 +155,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -155,7 +155,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
{ {
msg_Warn( p_this, "This doesn't look like a valid plugins cache" ); msg_Warn( p_this, "This doesn't look like a valid plugins cache" );
fclose( file ); fclose( file );
return; return 0;
} }
#endif #endif
...@@ -166,7 +166,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -166,7 +166,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "This doesn't look like a valid plugins cache " msg_Warn( p_this, "This doesn't look like a valid plugins cache "
"(corrupted header)" ); "(corrupted header)" );
fclose( file ); fclose( file );
return; return 0;
} }
/* Check header marker */ /* Check header marker */
...@@ -177,7 +177,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -177,7 +177,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "This doesn't look like a valid plugins cache " msg_Warn( p_this, "This doesn't look like a valid plugins cache "
"(corrupted header)" ); "(corrupted header)" );
fclose( file ); fclose( file );
return; return 0;
} }
if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) ) if (fread( &i_cache, 1, sizeof(i_cache), file ) != sizeof(i_cache) )
...@@ -185,22 +185,12 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -185,22 +185,12 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
msg_Warn( p_this, "This doesn't look like a valid plugins cache " msg_Warn( p_this, "This doesn't look like a valid plugins cache "
"(file too short)" ); "(file too short)" );
fclose( file ); fclose( file );
return; return 0;
} }
if( i_cache ) module_cache_t **pp_cache = malloc( i_cache * sizeof(*pp_cache) );
{ if( pp_cache == NULL )
size_t i_already = p_bank->i_loaded_cache; i_cache = 0; /* don't load anything */
pp_cache = realloc( p_bank->pp_loaded_cache,
(i_already + i_cache) * sizeof(*pp_cache) );
if( unlikely(pp_cache == NULL) )
i_cache = 0; /* don't load */
else
{
p_bank->pp_loaded_cache = pp_cache;
pp_cache += i_already;
}
}
#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
...@@ -301,10 +291,10 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -301,10 +291,10 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
LOAD_STRING( p_module->domain ); LOAD_STRING( p_module->domain );
} }
} }
p_bank->i_loaded_cache += i_cache;
fclose( file ); fclose( file );
return;
*r = pp_cache;
return i_cache;
error: error:
...@@ -312,7 +302,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir ) ...@@ -312,7 +302,7 @@ void CacheLoad( vlc_object_t *p_this, module_bank_t *p_bank, const char *dir )
/* TODO: cleanup */ /* TODO: cleanup */
fclose( file ); fclose( file );
return; return 0;
} }
......
...@@ -103,8 +103,8 @@ void module_InitBank( vlc_object_t *p_this ) ...@@ -103,8 +103,8 @@ void module_InitBank( vlc_object_t *p_this )
{ {
p_bank = calloc (1, sizeof(*p_bank)); p_bank = calloc (1, sizeof(*p_bank));
p_bank->i_usage = 1; p_bank->i_usage = 1;
p_bank->i_cache = p_bank->i_loaded_cache = 0; p_bank->i_cache = 0;
p_bank->pp_cache = p_bank->pp_loaded_cache = NULL; p_bank->pp_cache = NULL;
p_bank->head = NULL; p_bank->head = NULL;
/* Everything worked, attach the object */ /* Everything worked, attach the object */
...@@ -164,18 +164,6 @@ void module_EndBank( vlc_object_t *p_this, bool b_plugins ) ...@@ -164,18 +164,6 @@ void module_EndBank( vlc_object_t *p_this, bool b_plugins )
vlc_mutex_unlock( &module_lock ); vlc_mutex_unlock( &module_lock );
#ifdef HAVE_DYNAMIC_PLUGINS #ifdef HAVE_DYNAMIC_PLUGINS
while( p_bank->i_loaded_cache-- )
{
if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
{
if( unlikely( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module != NULL ) )
DeleteModule( p_bank,
p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module );
free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->path );
free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
}
}
free( p_bank->pp_loaded_cache );
while( p_bank->i_cache-- ) while( p_bank->i_cache-- )
{ {
free( p_bank->pp_cache[p_bank->i_cache]->path ); free( p_bank->pp_cache[p_bank->i_cache]->path );
...@@ -868,28 +856,48 @@ static void AllocateAllPlugins( vlc_object_t *p_this, module_bank_t *p_bank ) ...@@ -868,28 +856,48 @@ 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;
size_t count = 0;
size_t offset = p_module_bank->i_cache; size_t offset = p_module_bank->i_cache;
switch( mode ) switch( mode )
{ {
case CACHE_USE: case CACHE_USE:
CacheLoad( p_this, p_module_bank, path ); count = CacheLoad( p_this, path, &cache );
break; break;
case CACHE_RESET: case CACHE_RESET:
CacheDelete( p_this, path ); CacheDelete( p_this, path );
break; break;
default: case CACHE_IGNORE:
msg_Dbg( p_this, "ignoring plugins cache file" ); msg_Dbg( p_this, "ignoring plugins cache file" );
} }
msg_Dbg( p_this, "recursively browsing `%s'", path ); msg_Dbg( p_this, "recursively browsing `%s'", path );
/* TODO: pass as argument, remove this hack */
p_bank->pp_loaded_cache = cache;
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 );
if( mode != CACHE_IGNORE ) switch( mode )
CacheSave( p_this, path, p_module_bank->pp_cache + offset, {
p_module_bank->i_cache - offset ); case CACHE_USE:
for( size_t i = 0; i < count; i++ )
if( likely(cache[i] != NULL) )
{
if( cache[i]->p_module != NULL )
DeleteModule( p_bank, cache[i]->p_module );
free( cache[i]->path );
free( cache[i] );
}
free( cache );
case CACHE_RESET:
CacheSave( p_this, path, p_bank->pp_cache + offset,
p_bank->i_cache - offset );
case CACHE_IGNORE:
break;
}
} }
/***************************************************************************** /*****************************************************************************
......
...@@ -150,7 +150,7 @@ void module_Unload (module_handle_t); ...@@ -150,7 +150,7 @@ 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 *);
void CacheLoad (vlc_object_t *, module_bank_t *, const char *); size_t CacheLoad (vlc_object_t *, const char *, module_cache_t ***);
void CacheSave (vlc_object_t *, const char *, module_cache_t *const *, size_t); void CacheSave (vlc_object_t *, const char *, module_cache_t *const *, size_t);
module_t * CacheFind (module_bank_t *, const char *, time_t, off_t); module_t * CacheFind (module_bank_t *, const char *, time_t, off_t);
......
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