Add alloc_align attribute to memalign et al

System Internals / glibc - Jonathan Wakely [redhat.com] - 20 October 2021 23:19 UTC

GCC 4.9.0 added the alloc_align attribute to say that a function argument specifies the alignment of the returned pointer. Clang supports the attribute too. Using the attribute can allow a compiler to generate better code if it knows the returned pointer has a minimum alignment. See https://gcc.gnu.org/PR60092 for more details.

GCC implicitly knows the semantics of aligned_alloc and posix_memalign, but not the obsolete memalign. As a result, GCC generates worse code when memalign is used, compared to aligned_alloc. Clang knows about aligned_alloc and memalign, but not posix_memalign.

This change adds a new __attribute_alloc_align__ macro to and then uses it on memalign (where it helps GCC) and aligned_alloc (where GCC and Clang already know the semantics, but it doesn't hurt) and xposix_memalign. It can't be used on posix_memalign because that doesn't return a pointer (the allocated pointer is returned via a void** parameter instead).

Unlike the alloc_size attribute, alloc_align only allows a single argument. That means the new __attribute_alloc_align__ macro doesn't really need to be used with double parentheses to protect a comma between its arguments. For consistency with __attribute_alloc_size__ this patch defines it the same way, so that double parentheses are required.

8a9a593115 Add alloc_align attribute to memalign et al
malloc/malloc.h | 5 +++--
misc/sys/cdefs.h | 10 ++++++++++
stdlib/stdlib.h | 4 +++-
support/support.h | 5 +++--
4 files changed, 19 insertions(+), 5 deletions(-)

Upstream: sourceware.org


  • Share