Commit 8f683236 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Improve vlc_memalign()

 - use native MingW back-end on Windows, instead of kludges,
 - remove "base" pointer parameter,
 - inline.
parent cece0cc7
......@@ -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.])
......
......@@ -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 *) );
......
......@@ -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;
}
......
......@@ -25,7 +25,6 @@
#define _VLC_AVCODEC_COPY_H 1
typedef struct {
void *base;
uint8_t *buffer;
size_t size;
} copy_cache_t;
......
......@@ -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));
}
......
......@@ -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
......
......@@ -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
}
......@@ -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 );
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment