public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix realloc prototype (BZ #4792)
@ 2007-07-16  9:58 Jakub Jelinek
  2007-07-16 10:30 ` Wolfram Gloger
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2007-07-16  9:58 UTC (permalink / raw)
  To: Ulrich Drepper; +Cc: Glibc hackers

Hi!

info gcc says on the malloc attribute:
`malloc'
     The `malloc' attribute is used to tell the compiler that a function
     may be treated as if any non-`NULL' pointer it returns cannot
     alias any other pointer valid when the function returns.  This
     will often improve optimization.  Standard functions with this
     property include `malloc' and `calloc'.  `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-`NULL' value.
and internally while GCC uses malloc attribute on malloc etc., it doesn't
use it for realloc.
http://gcc.gnu.org/ml/gcc-patches/2004-01/msg00189.html

2007-07-16  Jakub Jelinek  <jakub@redhat.com>

	[BZ #4792]
	* stdlib/stdlib.h (realloc): Remove __attribute_malloc__.
	* malloc/malloc.h (realloc): Likewise.

--- libc/stdlib/stdlib.h.jj	2007-07-03 12:37:00.000000000 +0200
+++ libc/stdlib/stdlib.h	2007-07-16 10:31:04.000000000 +0200
@@ -597,8 +597,11 @@ __END_NAMESPACE_STD
 __BEGIN_NAMESPACE_STD
 /* Re-allocate the previously allocated block
    in PTR, making the new block SIZE bytes long.  */
+/* __attribute_malloc__ is not used, because if realloc returns
+   the same pointer that was passed to it, aliasing needs to be allowed
+   between objects pointed by the old and new pointers.  */
 extern void *realloc (void *__ptr, size_t __size)
-     __THROW __attribute_malloc__ __attribute_warn_unused_result__;
+     __THROW __attribute_warn_unused_result__;
 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
 extern void free (void *__ptr) __THROW;
 __END_NAMESPACE_STD
--- libc/malloc/malloc.h.jj	2005-03-08 01:44:45.000000000 +0100
+++ libc/malloc/malloc.h	2007-07-16 10:32:25.000000000 +0200
@@ -1,5 +1,6 @@
 /* Prototypes and definition for malloc implementation.
-   Copyright (C) 1996,97,99,2000,2002-2004,2005 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1999, 2000, 2002-2004, 2005, 2007
+   Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -54,8 +55,11 @@ extern void *calloc __MALLOC_P ((size_t 
 
 /* Re-allocate the previously allocated block in __ptr, making the new
    block SIZE bytes long.  */
+/* __attribute_malloc__ is not used, because if realloc returns
+   the same pointer that was passed to it, aliasing needs to be allowed
+   between objects pointed by the old and new pointers.  */
 extern void *realloc __MALLOC_P ((void *__ptr, size_t __size))
-       __attribute_malloc__ __attribute_warn_unused_result__;
+       __attribute_warn_unused_result__;
 
 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
 extern void free __MALLOC_P ((void *__ptr));


	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix realloc prototype (BZ #4792)
  2007-07-16  9:58 [PATCH] Fix realloc prototype (BZ #4792) Jakub Jelinek
@ 2007-07-16 10:30 ` Wolfram Gloger
  2007-07-16 11:15   ` Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Wolfram Gloger @ 2007-07-16 10:30 UTC (permalink / raw)
  To: jakub; +Cc: libc-hacker

Hi,

> info gcc says on the malloc attribute:
> `malloc'
>      The `malloc' attribute is used to tell the compiler that a function
>      may be treated as if any non-`NULL' pointer it returns cannot
>      alias any other pointer valid when the function returns.  This
>      will often improve optimization.  Standard functions with this
>      property include `malloc' and `calloc'.  `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-`NULL' value.

That text is IMHO completely correct and adequate.  But note that the
last sentence after "as long as" refers to a case that is _undefined_
by the C standard for the realloc function.  If realloc returns
non-NULL, the "old pointer" must not be used any more, it is just as
if free() had been called on it.  In other words, our "realloc" in
libc is not just "realloc-like" but it is _the_ C realloc.

> 2007-07-16  Jakub Jelinek  <jakub@redhat.com>
> 
> 	[BZ #4792]
> 	* stdlib/stdlib.h (realloc): Remove __attribute_malloc__.
> 	* malloc/malloc.h (realloc): Likewise.

Based on the argument above, I disagree with this change.

Within gcc, I guess you are referring to:

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

But maybe that is an internal "realloc-like" function were comparison
with the old pointer is intended and useful?  For libc, did you have a
particular issue/bug in mind beyond general principles?

Regards,
Wolfram.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix realloc prototype (BZ #4792)
  2007-07-16 10:30 ` Wolfram Gloger
@ 2007-07-16 11:15   ` Jakub Jelinek
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2007-07-16 11:15 UTC (permalink / raw)
  To: Wolfram Gloger; +Cc: libc-hacker

On Mon, Jul 16, 2007 at 10:29:47AM -0000, Wolfram Gloger wrote:
> > 2007-07-16  Jakub Jelinek  <jakub@redhat.com>
> > 
> > 	[BZ #4792]
> > 	* stdlib/stdlib.h (realloc): Remove __attribute_malloc__.
> > 	* malloc/malloc.h (realloc): Likewise.
> 
> Based on the argument above, I disagree with this change.

Except that this is highly controversial in GCC apparently, various
GCC developers have different opinions on this and that also exhibits
in the optimizations GCC will do with malloc attribute.

> Within gcc, I guess you are referring to:
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32748

I'm not actually refering to that, but e.g. to the "add alloc_size attribute"
gcc-patches thread back this May, or to the longish 2004 thread.
If GCC has realloc attribute, then glibc should use it, until then
it is certainly safer not to use malloc attribute.

	Jakub

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-07-16 11:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-16  9:58 [PATCH] Fix realloc prototype (BZ #4792) Jakub Jelinek
2007-07-16 10:30 ` Wolfram Gloger
2007-07-16 11:15   ` Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).