]> git.sesse.net Git - vlc/commitdiff
Remove one level of indirection in plugins cache, simplify
authorRémi Denis-Courmont <remi@remlab.net>
Sat, 13 Aug 2011 18:13:10 +0000 (21:13 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Sat, 13 Aug 2011 19:22:42 +0000 (22:22 +0300)
src/modules/cache.c
src/modules/modules.c
src/modules/modules.h

index 76b1b9b8a910d5d2a92a18a032a8067435ee5828..be47d9806396e8a9dd9e5cdab887bd1830ebe78f 100644 (file)
@@ -88,7 +88,7 @@ void CacheDelete( vlc_object_t *obj, const char *dir )
  * actually load the dynamically loadable module.
  * 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;
     FILE *file;
@@ -169,7 +169,7 @@ size_t CacheLoad( vlc_object_t *p_this, const char *dir, module_cache_t ***r )
         return 0;
     }
 
-    module_cache_t **pp_cache = NULL;
+    module_cache_t *cache = NULL;
 
 #define LOAD_IMMEDIATE(a) \
     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 )
         LOAD_IMMEDIATE(st.st_mtime);
         LOAD_IMMEDIATE(st.st_size);
 
-        CacheAdd (&pp_cache, &count, path, &st, module);
+        CacheAdd (&cache, &count, path, &st, module);
         free (path);
         /* TODO: deal with errors */
     }
     fclose( file );
 
-    *r = pp_cache;
+    *r = cache;
     return i_cache;
 
 error:
@@ -399,13 +399,13 @@ static int CacheLoadConfig( module_t *p_module, FILE *file )
     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.
  */
 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;
 
@@ -446,17 +446,14 @@ out:
     free (tmpname);
 
     for (size_t i = 0; i < n; i++)
-    {
-        free (entries[i]->path);
-        free (entries[i]);
-    }
+        free (entries[i].path);
     free (entries);
 }
 
 static int CacheSaveConfig (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)
 {
     uint32_t i_file_size = 0;
@@ -496,37 +493,38 @@ static int CacheSaveBank (FILE *file, module_cache_t *const *pp_cache,
 
     for (unsigned i = 0; i < i_cache; i++)
     {
+        module_t *module = cache[i].p_module;
         uint32_t i_submodule;
 
         /* Save additional infos */
-        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_longname );
-        SAVE_STRING( pp_cache[i]->p_module->psz_help );
-        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 );
+        SAVE_STRING(module->psz_object_name);
+        SAVE_STRING(module->psz_shortname);
+        SAVE_STRING(module->psz_longname);
+        SAVE_STRING(module->psz_help);
+        SAVE_IMMEDIATE(module->i_shortcuts);
+        for (unsigned j = 0; j < module->i_shortcuts; j++)
+            SAVE_STRING(module->pp_shortcuts[j]);
+
+        SAVE_STRING(module->psz_capability);
+        SAVE_IMMEDIATE(module->i_score);
+        SAVE_IMMEDIATE(module->b_unloadable);
 
         /* Config stuff */
-        if (CacheSaveConfig (file, pp_cache[i]->p_module))
+        if (CacheSaveConfig (file, module))
             goto error;
 
-        SAVE_STRING( pp_cache[i]->p_module->psz_filename );
-        SAVE_STRING( pp_cache[i]->p_module->domain );
+        SAVE_STRING(module->psz_filename);
+        SAVE_STRING(module->domain);
 
-        i_submodule = pp_cache[i]->p_module->submodule_count;
+        i_submodule = module->submodule_count;
         SAVE_IMMEDIATE( i_submodule );
-        if( CacheSaveSubmodule( file, pp_cache[i]->p_module->submodule ) )
+        if (CacheSaveSubmodule (file, module->submodule))
             goto error;
 
         /* Save common info */
-        SAVE_STRING(pp_cache[i]->path);
-        SAVE_IMMEDIATE(pp_cache[i]->mtime);
-        SAVE_IMMEDIATE(pp_cache[i]->size);
+        SAVE_STRING(cache[i].path);
+        SAVE_IMMEDIATE(cache[i].mtime);
+        SAVE_IMMEDIATE(cache[i].size);
     }
 
     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 )
 /**
  * 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)
 {
     while (count > 0)
     {
-        module_cache_t *entry = *(entries++);
-
-        if (!strcmp (entry->path, path)
-         && entry->mtime == st->st_mtime
-         && entry->size == st->st_size)
+        if (!strcmp (cache->path, path)
+         && cache->mtime == st->st_mtime
+         && cache->size == st->st_size)
        {
-            module_t *module = entry->p_module;
-            entry->p_module = NULL; /* Return NULL next time */
+            module_t *module = cache->p_module;
+            cache->p_module = NULL; /* Return NULL next time */
             return module;
        }
+       cache++;
        count--;
     }
 
@@ -666,25 +663,24 @@ module_t *CacheFind (module_cache_t *const *entries, size_t count,
 }
 
 /** 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)
 {
-    module_cache_t **entries;
+    module_cache_t *cache = *cachep;
+    const size_t count = *countp;
 
-    entries = realloc (*cache, (*count + 1) * sizeof (*entries));
-    if (unlikely(entries == NULL))
+    cache = realloc (cache, (count + 1) * sizeof (*cache));
+    if (unlikely(cache == NULL))
         return -1;
-    *cache = entries;
+    *cachep = cache;
 
-    entries[*count] = malloc (sizeof (**entries));
-    if (unlikely(entries[*count] == NULL))
-        return -1;
+    cache += count;
     /* 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)++;
+    cache->path = strdup (path);
+    cache->mtime = st->st_mtime;
+    cache->size = st->st_size;
+    cache->p_module = module;
+    *countp = count + 1;
     return 0;
 }
 
index 74f419eb99d3fc57d55126dc47c27ba6c73f3647..29ea6b345c2c887e9a71a7eb5afe3f296e2f6bc3 100644 (file)
@@ -64,10 +64,10 @@ typedef struct
 
     /* Plugins cache */
     size_t         i_cache;
-    module_cache_t **pp_cache;
+    module_cache_t *cache;
 
     int            i_loaded_cache;
-    module_cache_t **pp_loaded_cache;
+    module_cache_t *loaded_cache;
 
     module_t       *head;
 } module_bank_t;
@@ -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,
                                 const char *path, cache_mode_t mode )
 {
-    module_cache_t **cache = NULL;
+    module_cache_t *cache = NULL;
     size_t count = 0;
 
     switch( mode )
@@ -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 );
 
     /* TODO: pass as argument, remove this hack */
-    p_bank->pp_cache = NULL;
+    p_bank->cache = NULL;
     p_bank->i_cache = 0;
-    p_bank->pp_loaded_cache = cache;
+    p_bank->loaded_cache = cache;
     p_bank->i_loaded_cache = count;
     /* Don't go deeper than 5 subdirectories */
     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,
     {
         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] );
-                }
+            {
+                if (cache[i].p_module != NULL)
+                   DeleteModule (p_bank, cache[i].p_module);
+                free (cache[i].path);
+            }
             free( cache );
         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:
             break;
     }
@@ -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 ); */
     /* Check our plugins cache first then load plugin if needed */
     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);
     if( p_module == NULL )
         p_module = AllocatePlugin( p_this, path, true );
@@ -1010,7 +1008,7 @@ static int AllocatePluginFile( vlc_object_t * p_this, module_bank_t *p_bank,
         return 0;
 
     /* 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 */
     return  0;
 }
index 82598cfaa0d0c3ed20596473052e693eba304afc..9a8adb1df0c942c3b0d0b7d81eb6e9045886c6c1 100644 (file)
@@ -129,11 +129,11 @@ void module_Unload (module_handle_t);
 /* Plugins cache */
 void   CacheMerge (vlc_object_t *, module_t *, module_t *);
 void   CacheDelete(vlc_object_t *, const char *);
-size_t CacheLoad  (vlc_object_t *, const char *, module_cache_t ***);
-void   CacheSave  (vlc_object_t *, const char *, 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 *,
+size_t CacheLoad  (vlc_object_t *, const char *, module_cache_t **);
+int CacheAdd (module_cache_t **, size_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 */