http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60092

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #6)
> (In reply to Marc Glisse from comment #4)
> > Hack: when the return value of posix_memalign is ignored, if the platform
> > supports it, replace with a call to aligned_alloc (C11), which has an easier
> > interface.
> 
> The question is if posix_memalign is allowed to change errno.  If it is,
> then making glibc contains say something like:
> 
> extern int __REDIRECT_NTH (__posix_memalign_alias,
>                            (void ** __ptr, size_t __alignment, size_t
> __size),
>                            posix_memalign) __nonnull ((1)) __wur;
> extern void *__REDIRECT_NTH (__memalign_alias,
>                            (size_t __alignment, size_t __size),
>                            memalign)  __attribute__ ((__malloc__,
> __alloc_size__ (2)));
> 
> __extern_inline int
> posix_memalign (void **__ptr, size_t __alignment, size_t __size)
> {
>   if (__builtin_constant_p (__alignment))
>     {
>       if (__alignment == 0
>           || __alignment & (__alignment - 1)) != 0
>           || __alignment % sizeof (void *))
>         return EINVAL;
>       void *__res = __memalign_alias (__alignment, __size);
>       if (__res == NULL)
>         return ENOMEM;
>       *__ptr = __res;
>       return 0;
>     }
>   return __posix_memalign_alias (__ptr, __alignment, __size);
> }
> 
> But looking at glibc sources, even posix_memalign actually changes errno.
> Tbe problem with this inline version is that user aliasing bugs will trigger
> people more often, and that some hack will need to be find out for the
> EINVAL and ENOMEM values (because, stdlib.h is not supposed to include
> errno.h I guess, so it would need to be some __EINVAL/__ENOMEM value
> determined by configure or something).

According to the specification this is wrong.  Note that changing errno
is hindering optimization.  For example

int foo (int *p)
{
  *p = 1;
  malloc (4);
  return *p;
}

cannot CSE *p because p may point to errno.  (works for float *p and
works when using posix_memalign with my patch)

Reply via email to