[vlc-commits] cache: align integer tables

Rémi Denis-Courmont git at videolan.org
Tue Oct 25 21:55:09 CEST 2016


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Oct 25 22:10:06 2016 +0300| [1a25b6290ea38a74c582acbb8a432dfc8a700a46] | committer: Rémi Denis-Courmont

cache: align integer tables

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

 src/modules/cache.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/src/modules/cache.c b/src/modules/cache.c
index cfa1cca..98a3667 100644
--- a/src/modules/cache.c
+++ b/src/modules/cache.c
@@ -28,6 +28,7 @@
 # include "config.h"
 #endif
 
+#include <stdalign.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -56,7 +57,7 @@
 #ifdef HAVE_DYNAMIC_PLUGINS
 /* Sub-version number
  * (only used to avoid breakage in dev version when cache structure changes) */
-#define CACHE_SUBVERSION_NUM 24
+#define CACHE_SUBVERSION_NUM 25
 
 /* Cache filename */
 #define CACHE_NAME "plugins.dat"
@@ -123,6 +124,25 @@ static int vlc_cache_load_string(const char **restrict p, block_t *file)
     return 0;
 }
 
+static int vlc_cache_load_align(size_t align, block_t *file)
+{
+    assert(align > 0);
+
+    size_t skip = (-(uintptr_t)file->p_buffer) % align;
+    if (skip == 0)
+        return 0;
+
+    assert(skip < align);
+
+    if (file->i_buffer < skip)
+        return -1;
+
+    file->p_buffer += skip;
+    file->i_buffer -= skip;
+    assert((((uintptr_t)file->p_buffer) % align) == 0);
+    return 0;
+}
+
 #define LOAD_IMMEDIATE(a) \
     if (vlc_cache_load_immediate(&(a), file, sizeof (a))) \
         goto error
@@ -137,6 +157,9 @@ static int vlc_cache_load_string(const char **restrict p, block_t *file)
 #define LOAD_STRING(a) \
     if (vlc_cache_load_string(&(a), file)) \
         goto error
+#define LOAD_ALIGNOF(t) \
+    if (vlc_cache_load_align(alignof(t), file)) \
+        goto error
 
 static int CacheLoadConfig(module_config_t *cfg, block_t *file)
 {
@@ -180,9 +203,13 @@ static int CacheLoadConfig(module_config_t *cfg, block_t *file)
         cfg->value = cfg->orig;
 
         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]);
     }
@@ -444,6 +471,22 @@ error:
     if (CacheSaveString (file, (a))) \
         goto error
 
+static int CacheSaveAlign(FILE *file, size_t align)
+{
+    assert(align > 0);
+
+    size_t skip = (-ftell(file)) % align;
+    if (skip == 0)
+        return 0;
+
+    assert(((ftell(file) + skip) % align) == 0);
+    return fseek(file, skip, SEEK_CUR);
+}
+
+#define SAVE_ALIGNOF(t) \
+    if (CacheSaveAlign(file, alignof (t))) \
+        goto error
+
 static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
 {
     SAVE_IMMEDIATE (cfg->i_type);
@@ -464,6 +507,7 @@ static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
         SAVE_STRING (cfg->orig.psz);
         if (cfg->list_count == 0)
             SAVE_IMMEDIATE (cfg->list.psz_cb); /* XXX: see CacheLoadConfig() */
+
         for (unsigned i = 0; i < cfg->list_count; i++)
             SAVE_STRING (cfg->list.psz[i]);
     }
@@ -472,8 +516,14 @@ static int CacheSaveConfig (FILE *file, const module_config_t *cfg)
         SAVE_IMMEDIATE (cfg->orig);
         SAVE_IMMEDIATE (cfg->min);
         SAVE_IMMEDIATE (cfg->max);
-        if (cfg->list_count == 0)
+
+        if (cfg->list_count > 0)
+        {
+            SAVE_ALIGNOF(*cfg->list.i);
+        }
+        else
             SAVE_IMMEDIATE (cfg->list.i_cb); /* XXX: see CacheLoadConfig() */
+
         for (unsigned i = 0; i < cfg->list_count; i++)
              SAVE_IMMEDIATE (cfg->list.i[i]);
     }



More information about the vlc-commits mailing list