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: [LIBIBERTY]: Expose tail calls in xmemdup and xstrdup


 > Note that this explicit declaration is only done in .c files that are
 > careful to *not* include libiberty.h nor any header file which declare
 > memcpy.  Since xmemdup.c includes libiberty.h--and should, because
 > that is where xmemdup and xcalloc are declared--you should not use
 > this technique.
 > 
 > Instead, just include <string.h> as is already done.  It will declare
 > memcpy on pretty much all systems, and I don't think we need to worry
 > about the ones on which it doesn't.  In fact, I don't see that you
 > should change the set of header files at all.
 > 
 > If you really want to make sure that memcpy is declared, then you
 > should try including both <string.h> and <strings.h> as in, e.g,
 > concat.c, and only declare memcpy in the case where neither
 > HAVE_STRING_H nor HAVE_STRINGS_H is defined.  In such a case, I think
 > it is best to declare it without a prototype for extra protection
 > against declaration conflicts.
 > 
 > Ian


I don't necessarily agree that libiberty.h can prototype memcpy and
therefore cause conflicts.  It only includes stddef.h (which you said
is okay in another message) and stdarg.h (which only defines the va_
macros).

However I don't feel strongly about it.  Here's a patch which instead
includes strings.h and I added a cast to memcpy just in case.

Okay for mainline?


2005-03-24  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* xmemdup.c, xstrdup.c: Expose the tail call.
	
diff -rup orig/egcc-CVS20050323/libiberty/xmemdup.c egcc-CVS20050323/libiberty/xmemdup.c
--- orig/egcc-CVS20050323/libiberty/xmemdup.c	2001-09-26 14:16:17.000000000 -0400
+++ egcc-CVS20050323/libiberty/xmemdup.c	2005-03-24 13:17:30.232492448 -0500
@@ -24,6 +24,10 @@ allocated, the remaining memory is zeroe
 #include <sys/types.h> /* For size_t. */
 #ifdef HAVE_STRING_H
 #include <string.h>
+#else
+# if HAVE_STRINGS_H
+#  include <strings.h>
+# endif
 #endif
 
 PTR
@@ -33,6 +37,5 @@ xmemdup (input, copy_size, alloc_size)
   size_t alloc_size;
 {
   PTR output = xcalloc (1, alloc_size);
-  memcpy (output, input, copy_size);
-  return output;
+  return (PTR) memcpy (output, input, copy_size);
 }
diff -rup orig/egcc-CVS20050323/libiberty/xstrdup.c egcc-CVS20050323/libiberty/xstrdup.c
--- orig/egcc-CVS20050323/libiberty/xstrdup.c	2001-09-26 14:16:17.000000000 -0400
+++ egcc-CVS20050323/libiberty/xstrdup.c	2005-03-24 13:17:24.080427704 -0500
@@ -19,6 +19,10 @@ obtain memory.
 #endif
 #ifdef HAVE_STRING_H
 #include <string.h>
+#else
+# if HAVE_STRINGS_H
+#  include <strings.h>
+# endif
 #endif
 #include "ansidecl.h"
 #include "libiberty.h"
@@ -29,6 +33,5 @@ xstrdup (s)
 {
   register size_t len = strlen (s) + 1;
   register char *ret = xmalloc (len);
-  memcpy (ret, s, len);
-  return ret;
+  return (char *) memcpy (ret, s, len);
 }


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