From 7d600570ff883aabb9c8834e6fbdf49f36ebe58e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 7 Jul 2021 11:13:10 -0700 Subject: [PATCH 1/4] [ci skip] --- ChangeLog.md | 4 ++++ system/lib/dlmalloc.c | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 19fbcfdb0e108..76aface88b35e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -20,6 +20,10 @@ See docs/process.md for more on how version tagging works. 2.0.26 ------ +- The alignment of `long double`, which is a 128-bit floating-point value + implemented in software, is reduced from 16 to 8. The lower alignment allows + `max_align_t` to be correctly defined as 8, and avoids raising it to 16 which + would regress performance. (#10072) - The `alignMemory` function is now a library function and therefore not included by default. Debug builds will automatically abort if you try to use this function without including it. The normal library `__deps` diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index a500268f323f8..0eb6493963a5a 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -24,8 +24,19 @@ #define USE_SPIN_LOCKS 0 // Ensure we use pthread_mutex_t. #endif +#ifndef MALLOC_ALIGNMENT +#include +/* `malloc`ed pointers must be aligned at least as strictly as max_align_t. */ +#define MALLOC_ALIGNMENT (__alignof__(max_align_t)) +/* + Emscripten aligns even float128 to 64-bits, to save size and increase speed. + See https://github.com/emscripten-core/emscripten/issues/10072 +*/ +_Static_assert(MALLOC_ALIGNMENT == 8, "max_align_t must be 8"); #endif +#endif // __EMSCRIPTEN__ + #define __THROW #define __attribute_malloc__ From 797f670c6b1cd1795bc8c42758b638d1fa075a95 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Jul 2021 11:13:29 -0700 Subject: [PATCH 2/4] fix --- ChangeLog.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index a7d3c1ef53a60..65e0fb23350cc 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -22,8 +22,6 @@ See docs/process.md for more on how version tagging works. ------ - The alignment of `long double`, which is a 128-bit floating-point value implemented in software, is reduced from 16 to 8. The lower alignment allows - `max_align_t` to be correctly defined as 8, and avoids raising it to 16 which - would regress performance. (#10072) `max_align_t` to properly match the alignment we use for malloc, which is 8 (raising malloc's alignment to achieve correctness the other way would come with a performance regression). (#10072) From 9a760b08aa87b7c60d549344c81b6310446ff771 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Jul 2021 11:59:19 -0700 Subject: [PATCH 3/4] emmalloc too --- system/lib/emmalloc.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/lib/emmalloc.cpp b/system/lib/emmalloc.cpp index 187063ef96814..7cb9094f41592 100644 --- a/system/lib/emmalloc.cpp +++ b/system/lib/emmalloc.cpp @@ -39,6 +39,7 @@ * malloc. */ +#include #include #include #include @@ -62,6 +63,9 @@ extern "C" // than this will yield an allocation with this much alignment. #define MALLOC_ALIGNMENT 8 +_Static_assert(MALLOC_ALIGNMENT == __alignof__(max_align_t), "max_align_t must be correct"); + + #define EMMALLOC_EXPORT __attribute__((weak, __visibility__("default"))) #define MIN(x, y) ((x) < (y) ? (x) : (y)) From 1cf65286ea40df7d516f0db4207dfb8b469b3310 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 12 Jul 2021 12:29:42 -0700 Subject: [PATCH 4/4] feedback --- system/lib/emmalloc.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/system/lib/emmalloc.cpp b/system/lib/emmalloc.cpp index 7cb9094f41592..9b2727c9421e5 100644 --- a/system/lib/emmalloc.cpp +++ b/system/lib/emmalloc.cpp @@ -61,10 +61,8 @@ extern "C" // Configuration: specifies the minimum alignment that malloc()ed memory outputs. Allocation requests with smaller alignment // than this will yield an allocation with this much alignment. -#define MALLOC_ALIGNMENT 8 - -_Static_assert(MALLOC_ALIGNMENT == __alignof__(max_align_t), "max_align_t must be correct"); - +#define MALLOC_ALIGNMENT alignof(max_align_t) +_Static_assert(alignof(max_align_t) == 8, "max_align_t must be correct"); #define EMMALLOC_EXPORT __attribute__((weak, __visibility__("default")))