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__ diff --git a/system/lib/emmalloc.cpp b/system/lib/emmalloc.cpp index 187063ef96814..9b2727c9421e5 100644 --- a/system/lib/emmalloc.cpp +++ b/system/lib/emmalloc.cpp @@ -39,6 +39,7 @@ * malloc. */ +#include #include #include #include @@ -60,7 +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 +#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")))