diff options
Diffstat (limited to 'intern/guardedalloc')
-rw-r--r-- | intern/guardedalloc/intern/mallocn.c | 3 | ||||
-rw-r--r-- | intern/guardedalloc/intern/mallocn_intern.h | 2 | ||||
-rw-r--r-- | intern/guardedalloc/intern/mallocn_lockfree_impl.c | 23 |
3 files changed, 18 insertions, 10 deletions
diff --git a/intern/guardedalloc/intern/mallocn.c b/intern/guardedalloc/intern/mallocn.c index fa2d0d1e334..d24437c85f2 100644 --- a/intern/guardedalloc/intern/mallocn.c +++ b/intern/guardedalloc/intern/mallocn.c @@ -70,6 +70,9 @@ const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr; void *aligned_malloc(size_t size, size_t alignment) { + /* posix_memalign requires alignment to be a multiple of sizeof(void *). */ + assert(alignment >= ALIGNED_MALLOC_MINIMUM_ALIGNMENT); + #ifdef _WIN32 return _aligned_malloc(size, alignment); #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) diff --git a/intern/guardedalloc/intern/mallocn_intern.h b/intern/guardedalloc/intern/mallocn_intern.h index e6e090703d4..876607fdb77 100644 --- a/intern/guardedalloc/intern/mallocn_intern.h +++ b/intern/guardedalloc/intern/mallocn_intern.h @@ -107,6 +107,8 @@ size_t malloc_usable_size(void *ptr); #include "mallocn_inline.h" +#define ALIGNED_MALLOC_MINIMUM_ALIGNMENT sizeof(void *) + void *aligned_malloc(size_t size, size_t alignment); void aligned_free(void *ptr); diff --git a/intern/guardedalloc/intern/mallocn_lockfree_impl.c b/intern/guardedalloc/intern/mallocn_lockfree_impl.c index e8fd8de738b..87091bb9862 100644 --- a/intern/guardedalloc/intern/mallocn_lockfree_impl.c +++ b/intern/guardedalloc/intern/mallocn_lockfree_impl.c @@ -346,7 +346,17 @@ void *MEM_lockfree_malloc_arrayN(size_t len, size_t size, const char *str) void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str) { - MemHeadAligned *memh; + /* Huge alignment values doesn't make sense and they wouldn't fit into 'short' used in the + * MemHead. */ + assert(alignment < 1024); + + /* We only support alignments that are a power of two. */ + assert(IS_POW2(alignment)); + + /* Some OS specific aligned allocators require a certain minimal alignment. */ + if (alignment < ALIGNED_MALLOC_MINIMUM_ALIGNMENT) { + alignment = ALIGNED_MALLOC_MINIMUM_ALIGNMENT; + } /* It's possible that MemHead's size is not properly aligned, * do extra padding to deal with this. @@ -356,17 +366,10 @@ void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str */ size_t extra_padding = MEMHEAD_ALIGN_PADDING(alignment); - /* Huge alignment values doesn't make sense and they - * wouldn't fit into 'short' used in the MemHead. - */ - assert(alignment < 1024); - - /* We only support alignment to a power of two. */ - assert(IS_POW2(alignment)); - len = SIZET_ALIGN_4(len); - memh = (MemHeadAligned *)aligned_malloc(len + extra_padding + sizeof(MemHeadAligned), alignment); + MemHeadAligned *memh = (MemHeadAligned *)aligned_malloc( + len + extra_padding + sizeof(MemHeadAligned), alignment); if (LIKELY(memh)) { /* We keep padding in the beginning of MemHead, |