From 461ee89b6199ac93d5262eaae4afb69113c74d44 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 19 Jun 2024 18:13:44 +0200 Subject: [PATCH] Cleanup: Sync 'alignment' checks of guarded allocator with lockfree one. Sync a bit better the checks on the alignment value between `MEM_lockfree_mallocN_aligned` and `MEM_guarded_mallocN_aligned`. The only significant change, in `MEM_guarded_mallocN_aligned`, is the usage of `ALIGNED_MALLOC_MINIMUM_ALIGNMENT` instead of 'magic value' `8`. This should not have any effect on 64bits platforms, but on 32bits ones the minimum alignment would be reduced from `8` to `4` now. NOTE: we could also consider making these checks part of a utils function, instead of duplicating them in the codebase. --- .../intern/mallocn_guarded_impl.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/intern/guardedalloc/intern/mallocn_guarded_impl.cc b/intern/guardedalloc/intern/mallocn_guarded_impl.cc index 50185d54423..6cb93544772 100644 --- a/intern/guardedalloc/intern/mallocn_guarded_impl.cc +++ b/intern/guardedalloc/intern/mallocn_guarded_impl.cc @@ -511,12 +511,18 @@ void *MEM_guarded_malloc_arrayN(size_t len, size_t size, const char *str) void *MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str) { - /* We only support alignment to a power of two. */ + /* 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)); - /* Use a minimal alignment of 8. Otherwise MEM_guarded_freeN thinks it is an illegal pointer. */ - if (alignment < 8) { - alignment = 8; + /* Some OS specific aligned allocators require a certain minimal alignment. */ + /* And #MEM_guarded_freeN also checks that it is freeing a pointer aligned with `sizeof(void *)`. + */ + if (alignment < ALIGNED_MALLOC_MINIMUM_ALIGNMENT) { + alignment = ALIGNED_MALLOC_MINIMUM_ALIGNMENT; } /* It's possible that MemHead's size is not properly aligned, @@ -527,11 +533,6 @@ void *MEM_guarded_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); - #ifdef WITH_MEM_VALGRIND const size_t len_unaligned = len; #endif