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.
This commit is contained in:
Bastien Montagne
2024-06-19 18:13:44 +02:00
committed by Bastien Montagne
parent de986e68d4
commit 461ee89b61

View File

@@ -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