Patch to special_function_p

Jason Merrill jason@cygnus.com
Fri Mar 3 01:18:00 GMT 2000


The C++ standard doesn't appear to provide any guarantees about whether or
not operator new returns the pointer to a unique object; in practice the
default operator new will do so (since it just calls malloc), but it can be
overridden by the user to something that doesn't, and we can't optimize
based on something that is probably, but not necessarily, true.

Furthermore, operator new (size_t, void *) always returns its argument,
which is almost certainly going to alias something else, so the blanket
treatment of all op news as malloc-like is very wrong.

Thoughts?

Fixes g++.other/anon3.C when built with -O2.  

2000-03-03  Jason Merrill  <jason@casey.cygnus.com>

	* calls.c (special_function_p): operator new may not be malloc-like.

Index: calls.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/calls.c,v
retrieving revision 1.88
diff -c -p -r1.88 calls.c
*** calls.c	2000/03/02 23:50:11	1.88
--- calls.c	2000/03/03 09:04:33
*************** special_function_p (fndecl, returns_twic
*** 634,649 ****
        /* Do not add any more malloc-like functions to this list,
           instead mark them as malloc functions using the malloc attribute.
           Note, realloc is not suitable for attribute malloc since
!          it may return the same address across multiple calls. */
        else if (! strcmp (tname, "malloc")
  	       || ! strcmp (tname, "calloc")
! 	       || ! strcmp (tname, "strdup")
! 	       /* Note use of NAME rather than TNAME here.  These functions
! 		  are only reserved when preceded with __.  */
! 	       || ! strcmp (name, "__vn")	/* mangled __builtin_vec_new */
! 	       || ! strcmp (name, "__nw")	/* mangled __builtin_new */
! 	       || ! strcmp (name, "__builtin_new")
! 	       || ! strcmp (name, "__builtin_vec_new"))
  	*is_malloc = 1;
      }
  }
--- 634,646 ----
        /* Do not add any more malloc-like functions to this list,
           instead mark them as malloc functions using the malloc attribute.
           Note, realloc is not suitable for attribute malloc since
!          it may return the same address across multiple calls.
!          C++ operator new is not suitable because it is not required
!          to return a unique pointer; indeed, the standard placement new
! 	 just returns its argument. */
        else if (! strcmp (tname, "malloc")
  	       || ! strcmp (tname, "calloc")
! 	       || ! strcmp (tname, "strdup"))
  	*is_malloc = 1;
      }
  }


More information about the Gcc-patches mailing list