[vlc-commits] Improve vlc_memalign()

Rémi Denis-Courmont git at videolan.org
Mon Sep 5 22:44:11 CEST 2011


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Sep  5 23:42:24 2011 +0300| [8f683236f8aa59adfb7a5598247eb4af5098661c] | committer: Rémi Denis-Courmont

Improve vlc_memalign()

 - use native MingW back-end on Windows, instead of kludges,
 - remove "base" pointer parameter,
 - inline.

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

 configure.ac                   |    2 +-
 include/vlc_common.h           |   16 +++++++++++++++-
 modules/codec/avcodec/copy.c   |    8 +++-----
 modules/codec/avcodec/copy.h   |    1 -
 modules/video_filter/gradfun.c |    7 +++----
 src/libvlccore.sym             |    1 -
 src/misc/cpu.c                 |   28 ----------------------------
 src/misc/picture.c             |    5 +++--
 8 files changed, 25 insertions(+), 43 deletions(-)

diff --git a/configure.ac b/configure.ac
index ceafd03..240dcee 100644
--- a/configure.ac
+++ b/configure.ac
@@ -562,7 +562,7 @@ need_libc=false
 
 dnl Check for usual libc functions
 AC_CHECK_DECLS([nanosleep],,,[#include <time.h>])
-AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r if_nameindex if_nametoindex isatty lstat memalign mmap openat pread posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp uselocale])
+AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r if_nameindex if_nametoindex isatty lstat memalign mmap openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp uselocale])
 AC_REPLACE_FUNCS([asprintf atof atoll dirfd fdopendir flockfile fsync getdelim getpid gmtime_r lldiv localtime_r nrand48 rewind setenv strcasecmp strcasestr strdup strlcpy strncasecmp strndup strnlen strsep strtof strtok_r strtoll swab tdestroy vasprintf])
 AC_CHECK_FUNCS(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
diff --git a/include/vlc_common.h b/include/vlc_common.h
index 6cf89a7..08fd006 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -883,7 +883,21 @@ static inline void SetQWLE (void *p, uint64_t qw)
 
 VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
 
-VLC_API void * vlc_memalign( void **base, size_t alignment, size_t size ) VLC_USED;
+/* Aligned memory allocator */
+#ifdef WIN32
+# include <malloc.h>
+# define vlc_memalign(align, size) (__mingw_aligned_malloc(size, align))
+# define vlc_free(base)            (__mingw_aligned_free(base))
+#else
+static inline void *vlc_memalign(size_t align, size_t size)
+{
+    void *base;
+    if (unlikely(posix_memalign(&base, align, size)))
+        base = NULL;
+    return base;
+}
+# define vlc_free(base) free(base)
+#endif
 
 VLC_API void vlc_tdestroy( void *, void (*)(void *) );
 
diff --git a/modules/codec/avcodec/copy.c b/modules/codec/avcodec/copy.c
index a7ee432..3c27069 100644
--- a/modules/codec/avcodec/copy.c
+++ b/modules/codec/avcodec/copy.c
@@ -293,16 +293,14 @@ static void SplitPlanes(uint8_t *dstu, size_t dstu_pitch,
 int CopyInitCache(copy_cache_t *cache, unsigned width)
 {
     cache->size = __MAX((width + 0x0f) & ~ 0x0f, 4096);
-    cache->buffer = vlc_memalign(&cache->base, 16, cache->size);
-    if (!cache->base)
+    cache->buffer = vlc_memalign(16, cache->size);
+    if (!cache->buffer)
         return VLC_EGENERIC;
     return VLC_SUCCESS;
 }
 void CopyCleanCache(copy_cache_t *cache)
 {
-    free(cache->base);
-
-    cache->base   = NULL;
+    vlc_free(cache->buffer);
     cache->buffer = NULL;
     cache->size   = 0;
 }
diff --git a/modules/codec/avcodec/copy.h b/modules/codec/avcodec/copy.h
index 93d246a..f53fabb 100644
--- a/modules/codec/avcodec/copy.h
+++ b/modules/codec/avcodec/copy.h
@@ -25,7 +25,6 @@
 #define _VLC_AVCODEC_COPY_H 1
 
 typedef struct {
-    void    *base;
     uint8_t *buffer;
     size_t  size;
 } copy_cache_t;
diff --git a/modules/video_filter/gradfun.c b/modules/video_filter/gradfun.c
index 3bde5bb..765cf64 100644
--- a/modules/video_filter/gradfun.c
+++ b/modules/video_filter/gradfun.c
@@ -103,7 +103,6 @@ struct filter_sys_t {
     float            strength;
     int              radius;
     const vlc_chroma_description_t *chroma;
-    void             *base_buf;
     struct vf_priv_s cfg;
 };
 
@@ -128,7 +127,7 @@ static int Open(vlc_object_t *object)
     sys->radius   = var_CreateGetIntegerCommand(filter, CFG_PREFIX "radius");
     var_AddCallback(filter, CFG_PREFIX "strength", Callback, NULL);
     var_AddCallback(filter, CFG_PREFIX "radius",   Callback, NULL);
-    sys->base_buf = NULL;
+    sys->cfg.buf = NULL;
 
     struct vf_priv_s *cfg = &sys->cfg;
     cfg->thresh      = 0.0;
@@ -162,7 +161,7 @@ static void Close(vlc_object_t *object)
 
     var_DelCallback(filter, CFG_PREFIX "radius",   Callback, NULL);
     var_DelCallback(filter, CFG_PREFIX "strength", Callback, NULL);
-    free(sys->base_buf);
+    free(sys->cfg.buf);
     vlc_mutex_destroy(&sys->lock);
     free(sys);
 }
@@ -188,7 +187,7 @@ static picture_t *Filter(filter_t *filter, picture_t *src)
     cfg->thresh = (1 << 15) / strength;
     if (cfg->radius != radius) {
         cfg->radius = radius;
-        cfg->buf    = vlc_memalign(&sys->base_buf, 16,
+        cfg->buf    = vlc_memalign(16,
                                    (((fmt->i_width + 15) & ~15) * (cfg->radius + 1) / 2 + 32) * sizeof(*cfg->buf));
     }
 
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 7eb6e60..672bb53 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -558,7 +558,6 @@ vlc_join
 vlc_list_children
 vlc_list_release
 vlc_memcpy
-vlc_memalign
 vlc_meta_AddExtra
 vlc_meta_CopyExtraNames
 vlc_meta_Delete
diff --git a/src/misc/cpu.c b/src/misc/cpu.c
index 813f2fd..15de451 100644
--- a/src/misc/cpu.c
+++ b/src/misc/cpu.c
@@ -385,31 +385,3 @@ void *vlc_memcpy (void *tgt, const void *src, size_t n)
 {
     return pf_vlc_memcpy (tgt, src, n);
 }
-
-/**
- * Returned an aligned pointer on newly allocated memory.
- * \param alignment must be a power of 2 and a multiple of sizeof(void*)
- * \param size is the size of the usable memory returned.
- *
- * It must not be freed directly, *base must.
- */
-void *vlc_memalign(void **base, size_t alignment, size_t size)
-{
-    assert(alignment >= sizeof(void*));
-    for (size_t t = alignment; t > 1; t >>= 1)
-        assert((t&1) == 0);
-#if defined(HAVE_POSIX_MEMALIGN)
-    if (posix_memalign(base, alignment, size)) {
-        *base = NULL;
-        return NULL;
-    }
-    return *base;
-#elif defined(HAVE_MEMALIGN)
-    return *base = memalign(alignment, size);
-#else
-    unsigned char *p = *base = malloc(size + alignment - 1);
-    if (!p)
-        return NULL;
-    return (void*)((uintptr_t)(p + alignment - 1) & ~(alignment - 1));
-#endif
-}
diff --git a/src/misc/picture.c b/src/misc/picture.c
index b63f843..2c47255 100644
--- a/src/misc/picture.c
+++ b/src/misc/picture.c
@@ -70,12 +70,13 @@ static int vout_AllocatePicture( picture_t *p_pic,
         i_bytes += p->i_pitch * p->i_lines;
     }
 
-    uint8_t *p_data = vlc_memalign( &p_pic->p_data_orig, 16, i_bytes );
+    uint8_t *p_data = vlc_memalign( 16, i_bytes );
     if( !p_data )
     {
         p_pic->i_planes = 0;
         return VLC_EGENERIC;
     }
+    p_pic->p_data_orig = p_data; /* TODO: get rid of this */
 
     /* Fill the p_pixels field for each plane */
     p_pic->p[0].p_pixels = p_data;
@@ -267,7 +268,7 @@ void picture_Delete( picture_t *p_picture )
     assert( p_picture->p_release_sys == NULL );
 
     free( p_picture->p_q );
-    free( p_picture->p_data_orig );
+    vlc_free( p_picture->p_data_orig );
     free( p_picture->p_sys );
     free( p_picture );
 }



More information about the vlc-commits mailing list