[vlc-commits] cache: do not copy integer tables

Rémi Denis-Courmont git at videolan.org
Tue Oct 25 22:26:22 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Oct 25 22:49:43 2016 +0300| [55835f108ebbd788bf410b16fafc37f5393f05c6] | committer: Rémi Denis-Courmont

cache: do not copy integer tables

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=55835f108ebbd788bf410b16fafc37f5393f05c6
---

 include/vlc_configuration.h |  2 +-
 src/config/core.c           |  6 ------
 src/modules/cache.c         | 36 +++++++++++++++++++++++++++++++++---
 src/modules/entry.c         |  8 +-------
 4 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/include/vlc_configuration.h b/include/vlc_configuration.h
index dcc24d8..84499c1 100644
--- a/include/vlc_configuration.h
+++ b/include/vlc_configuration.h
@@ -82,7 +82,7 @@ struct module_config_t
     union
     {
         const char **psz;          /* List of possible values for the option */
-        int   *i;
+        const int  *i;
         vlc_string_list_cb psz_cb;
         vlc_integer_list_cb i_cb;
     } list;
diff --git a/src/config/core.c b/src/config/core.c
index b0b177e..f6285bf 100644
--- a/src/config/core.c
+++ b/src/config/core.c
@@ -587,12 +587,6 @@ void config_Free (module_config_t *tab, size_t confsize)
     {
         module_config_t *p_item = &tab[j];
 
-        if (IsConfigIntegerType (p_item->i_type))
-        {
-            if (p_item->list_count)
-                free (p_item->list.i);
-        }
-        else
         if (IsConfigStringType (p_item->i_type))
         {
             free (p_item->value.psz);
diff --git a/src/modules/cache.c b/src/modules/cache.c
index 98a3667..c523566 100644
--- a/src/modules/cache.c
+++ b/src/modules/cache.c
@@ -100,6 +100,29 @@ static int vlc_cache_load_bool(bool *out, block_t *in)
     return 0;
 }
 
+static int vlc_cache_load_array(const void **p, size_t size, size_t n,
+                                block_t *file)
+{
+    if (n == 0)
+    {
+        *p = NULL;
+        return 0;
+    }
+
+    if (unlikely(size * n < size))
+        return -1;
+
+    size *= n;
+
+    if (file->i_buffer < size)
+        return -1;
+
+    *p = file->p_buffer;
+    file->p_buffer += size;
+    file->i_buffer -= size;
+    return 0;
+}
+
 static int vlc_cache_load_string(const char **restrict p, block_t *file)
 {
     uint16_t size;
@@ -154,6 +177,14 @@ static int vlc_cache_load_align(size_t align, block_t *file)
             goto error; \
         (a) = b; \
     } while (0)
+#define LOAD_ARRAY(a,n) \
+    do \
+    { \
+        const void *base; \
+        if (vlc_cache_load_array(&base, sizeof (*(a)), (n), file)) \
+            goto error; \
+        (a) = base; \
+    } while (0)
 #define LOAD_STRING(a) \
     if (vlc_cache_load_string(&(a), file)) \
         goto error
@@ -205,14 +236,13 @@ static int CacheLoadConfig(module_config_t *cfg, block_t *file)
         if (cfg->list_count)
         {
             LOAD_ALIGNOF(*cfg->list.i);
-            cfg->list.i = xmalloc (cfg->list_count * sizeof (int));
         }
         else /* TODO: fix config_GetPszChoices() instead of this hack: */
             LOAD_IMMEDIATE(cfg->list.i_cb);
 
-        for (unsigned i = 0; i < cfg->list_count; i++)
-             LOAD_IMMEDIATE (cfg->list.i[i]);
+        LOAD_ARRAY(cfg->list.i, cfg->list_count);
     }
+
     cfg->list_text = xmalloc (cfg->list_count * sizeof (char *));
     for (unsigned i = 0; i < cfg->list_count; i++)
     {
diff --git a/src/modules/entry.c b/src/modules/entry.c
index 23e4e6c..34bfd9b 100644
--- a/src/modules/entry.c
+++ b/src/modules/entry.c
@@ -365,13 +365,7 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
                 break; /* nothing to do */
             /* Copy values */
             if (IsConfigIntegerType (item->i_type))
-            {
-                const int *src = va_arg (ap, const int *);
-                int *dst = xmalloc (sizeof (int) * len);
-
-                memcpy (dst, src, sizeof (int) * len);
-                item->list.i = dst;
-            }
+                item->list.i = va_arg(ap, const int *);
             else
             if (IsConfigStringType (item->i_type))
             {



More information about the vlc-commits mailing list