mirror of
https://code.videolan.org/videolan/dav1d
synced 2026-06-11 04:03:05 +00:00
Support using C11 aligned_alloc for dav1d_alloc_aligned
This commit is contained in:
committed by
Ronald S. Bultje
co-authored by
Ronald S. Bultje
parent
7629402bbd
commit
d268788467
@@ -157,6 +157,12 @@ else
|
||||
if cc.has_function('posix_memalign', prefix : '#include <stdlib.h>', args : test_args)
|
||||
cdata.set('HAVE_POSIX_MEMALIGN', 1)
|
||||
endif
|
||||
if cc.has_function('memalign', prefix : '#include <malloc.h>', args : test_args)
|
||||
cdata.set('HAVE_MEMALIGN', 1)
|
||||
endif
|
||||
if cc.has_function('aligned_alloc', prefix : '#include <stdlib.h>', args : test_args)
|
||||
cdata.set('HAVE_ALIGNED_ALLOC', 1)
|
||||
endif
|
||||
endif
|
||||
|
||||
# check for fseeko on android. It is not always available if _FILE_OFFSET_BITS is defined to 64
|
||||
|
||||
@@ -109,16 +109,7 @@ void *dav1d_malloc(const enum AllocationType type, const size_t sz) {
|
||||
void *dav1d_alloc_aligned(const enum AllocationType type,
|
||||
const size_t sz, const size_t align)
|
||||
{
|
||||
assert(!(align & (align - 1)));
|
||||
void *ptr;
|
||||
#ifdef _WIN32
|
||||
ptr = _aligned_malloc(sz + align, align);
|
||||
#elif defined(HAVE_POSIX_MEMALIGN)
|
||||
if (posix_memalign(&ptr, align, sz + align)) return NULL;
|
||||
#else
|
||||
ptr = memalign(align, sz + align);
|
||||
#endif
|
||||
|
||||
void *const ptr = dav1d_alloc_aligned_internal(align, sz + align);
|
||||
return track_alloc(type, ptr, sz, align);
|
||||
}
|
||||
|
||||
@@ -140,12 +131,7 @@ void dav1d_free(void *ptr) {
|
||||
|
||||
void dav1d_free_aligned(void *ptr) {
|
||||
if (ptr) {
|
||||
ptr = track_free(ptr);
|
||||
#ifdef _WIN32
|
||||
_aligned_free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
dav1d_free_aligned_internal(track_free(ptr));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(_WIN32) || !defined(HAVE_POSIX_MEMALIGN)
|
||||
#if defined(_WIN32) || defined(HAVE_MEMALIGN)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
@@ -79,6 +79,39 @@ typedef struct Dav1dMemPool {
|
||||
#endif
|
||||
} Dav1dMemPool;
|
||||
|
||||
// TODO: Move this to a common location?
|
||||
#define ROUND_UP(x,a) (((x)+((a)-1)) & ~((a)-1))
|
||||
|
||||
/*
|
||||
* Allocate align-byte aligned memory. The return value can be released
|
||||
* by calling the dav1d_free_aligned() function.
|
||||
*/
|
||||
static inline void *dav1d_alloc_aligned_internal(const size_t sz, const size_t align) {
|
||||
assert(!(align & (align - 1)));
|
||||
#ifdef _WIN32
|
||||
return _aligned_malloc(sz, align);
|
||||
#elif defined(HAVE_POSIX_MEMALIGN)
|
||||
void *ptr;
|
||||
if (posix_memalign(&ptr, align, sz)) return NULL;
|
||||
return ptr;
|
||||
#elif defined(HAVE_MEMALIGN)
|
||||
return memalign(align, sz);
|
||||
#elif defined(HAVE_ALIGNED_ALLOC)
|
||||
// The C11 standard specifies that the size parameter
|
||||
// must be an integral multiple of alignment.
|
||||
return aligned_alloc(align, ROUND_UP(sz, align));
|
||||
#else
|
||||
#error No aligned allocation functions are available
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline void dav1d_free_aligned_internal(void *ptr) {
|
||||
#ifdef _WIN32
|
||||
_aligned_free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if TRACK_HEAP_ALLOCATIONS
|
||||
void *dav1d_malloc(enum AllocationType type, size_t sz);
|
||||
@@ -91,34 +124,9 @@ void dav1d_log_alloc_stats(Dav1dContext *c);
|
||||
#define dav1d_mem_pool_init(type, pool) dav1d_mem_pool_init(pool)
|
||||
#define dav1d_malloc(type, sz) malloc(sz)
|
||||
#define dav1d_realloc(type, ptr, sz) realloc(ptr, sz)
|
||||
#define dav1d_alloc_aligned(type, sz, align) dav1d_alloc_aligned_internal(sz, align)
|
||||
#define dav1d_free(ptr) free(ptr)
|
||||
|
||||
/*
|
||||
* Allocate align-byte aligned memory. The return value can be released
|
||||
* by calling the dav1d_free_aligned() function.
|
||||
*/
|
||||
static inline void *dav1d_alloc_aligned(const size_t sz, const size_t align) {
|
||||
assert(!(align & (align - 1)));
|
||||
#ifdef _WIN32
|
||||
return _aligned_malloc(sz, align);
|
||||
#elif defined(HAVE_POSIX_MEMALIGN)
|
||||
void *ptr;
|
||||
if (posix_memalign(&ptr, align, sz)) return NULL;
|
||||
return ptr;
|
||||
#else
|
||||
return memalign(align, sz);
|
||||
#endif
|
||||
}
|
||||
#define dav1d_alloc_aligned(type, sz, align) dav1d_alloc_aligned(sz, align)
|
||||
|
||||
static inline void dav1d_free_aligned(void *ptr) {
|
||||
#ifdef _WIN32
|
||||
_aligned_free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
#define dav1d_free_aligned(ptr) dav1d_free_aligned_internal(ptr)
|
||||
#endif /* TRACK_HEAP_ALLOCATIONS */
|
||||
|
||||
void dav1d_mem_pool_push(Dav1dMemPool *pool, Dav1dMemPoolBuffer *buf);
|
||||
|
||||
Reference in New Issue
Block a user