This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Adjust 'malloc' attribute documentation to match implementation


On Monday 9 January 2012 10:05:08 Richard Guenther wrote:
> Since GCC 4.4 applying the malloc attribute to realloc-like
> functions does not work under the documented constraints because
> the contents of the memory pointed to are not properly transfered
> from the realloc argument (or treated as pointing to anything,
> like 4.3 behaved).
> 
> The following adjusts documentation to reflect implementation
> reality (we do have an implementation detail that treats the
> memory blob returned for non-builtins as pointing to any global
> variable, but that is neither documented nor do I plan to do
> so - I presume it is to allow allocation + initialization
> routines to be marked with malloc, but even that area looks
> susceptible to misinterpretation to me).
> 
> Any comments?

The new text says the memory must be undefined, but gives calloc as an
example for which the memory is defined to be zero. Also, GCC has
built-ins for strdup and strndup with the malloc attribute and GLIBC
further adds it to wcsdup (wchar_t version of strdup) and tempnam. In
all of these cases the memory is defined.

Isn't the reason the attribute doesn't apply to realloc simply because
the returned pointer may alias the one given as argument, rather than
having defined memory content?

> 2012-01-09  Richard Guenther  <rguenther@suse.de>
> 
>         * doc/extend.texi (malloc attribute): Adjust according to
>         implementation.
> 
> Index: gcc/doc/extend.texi
> ===================================================================
> --- gcc/doc/extend.texi (revision 183001)
> +++ gcc/doc/extend.texi (working copy)
> @@ -2771,13 +2771,12 @@ efficient @code{jal} instruction.
>  @cindex @code{malloc} attribute
>  The @code{malloc} attribute is used to tell the compiler that a function
>  may be treated as if any non-@code{NULL} pointer it returns cannot
> -alias any other pointer valid when the function returns.
> +alias any other pointer valid when the function returns and that the memory
> +has undefined content.
>  This will often improve optimization.
>  Standard functions with this property include @code{malloc} and
> -@code{calloc}.  @code{realloc}-like functions have this property as
> -long as the old pointer is never referred to (including comparing it
> -to the new pointer) after the function returns a non-@code{NULL}
> -value.
> +@code{calloc}.  @code{realloc}-like functions do not have this
> +property as the memory pointed to does not have undefined content.
>  
>  @item mips16/nomips16
>  @cindex @code{mips16} attribute


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]