]> git.sesse.net Git - vlc/commitdiff
Improve vlc_memalign()
authorRémi Denis-Courmont <remi@remlab.net>
Mon, 5 Sep 2011 20:42:24 +0000 (23:42 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Mon, 5 Sep 2011 20:44:06 +0000 (23:44 +0300)
 - use native MingW back-end on Windows, instead of kludges,
 - remove "base" pointer parameter,
 - inline.

configure.ac
include/vlc_common.h
modules/codec/avcodec/copy.c
modules/codec/avcodec/copy.h
modules/video_filter/gradfun.c
src/libvlccore.sym
src/misc/cpu.c
src/misc/picture.c

index ceafd03ad22023dcd57d8203cc8c6074e200119d..240dceebb9e8cc7011ce5b9d2e5eb1bff8c2d8d3 100644 (file)
@@ -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.])
index 6cf89a7bacb02ae40109dba800c1a3a9bd02fd8e..08fd0063731d228a3923bc203bd8f1c1e26de9d9 100644 (file)
@@ -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 *) );
 
index a7ee432527dd8639b07d406f42cc65afa50e71fe..3c2706926ea64ebaace35d6b03afd76e70623f0b 100644 (file)
@@ -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;
 }
index 93d246af5a131ea004a20b0afac782d955ea74b5..f53fabb194b337106f05b200fff0faa8cb04c36f 100644 (file)
@@ -25,7 +25,6 @@
 #define _VLC_AVCODEC_COPY_H 1
 
 typedef struct {
-    void    *base;
     uint8_t *buffer;
     size_t  size;
 } copy_cache_t;
index 3bde5bb8622cb869ec9af5b04d597bb0f0cbc6f3..765cf643604c2aae5d0147339c7ade37e5329ae6 100644 (file)
@@ -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));
     }
 
index 7eb6e60126f307b9e55fcd2b1f57afd7dfac24ca..672bb53f2ae2f1c17e47f9e794251c93cb9c3557 100644 (file)
@@ -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
index 813f2fdd042f315f53d64f2f378b50001c0fb9f5..15de451c0d3d221c6d43f530b09d676ae8836ba6 100644 (file)
@@ -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
-}
index b63f843af09df15c6a5197d6c7713bfefee47417..2c47255bd515594c2ab26907f76526e7eabcfbcc 100644 (file)
@@ -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 );
 }