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]

[LIBIBERTY]: Fix interfaces for bcmp, bcopy and bzero


The function definitions for libiberty's bcmp, bcopy and bzero are
incorrect.  They use "int" for the length instead of "size_t".  This
may have been papered over in 32-bit only days but not with lp64 and
it conflicts with several other places.  While this is in theory an
ABI change, I believe it is correct because it will break on lp64 as
it stands now:

1.  The open group specification uses size_t, see:
http://www.opengroup.org/onlinepubs/009695399/functions/bcmp.html
http://www.opengroup.org/onlinepubs/009695399/functions/bcopy.html
http://www.opengroup.org/onlinepubs/009695399/functions/bzero.html

2.  Using "int" conflicts with the builtin definitions in GCC, so user
    code will have the length arg promoted to size_t.

3.  Using "int" conflicts with the explicit prototypes for bcopy in
    libiberty's memmove.c and memcpy.c.


While poking around in there, I fixed some warnings and also decided
to have bcmp and bzero call the mem* analogues.  For bcmp it'll turn
the tail call into a jmp and on x86 it will optimize into the
appropriate inline instructions.


Note, the bcmp function docs say a non-zero result isn't meaningful,
but that doesn't preclude making it so by calling memcmp.  One simply
can't rely on it and the docs still make this explicit.

Tested by doing "make -k testlib.a", the b*.c files compiled cleanly.

Okay for mainline?

		Thanks,
		--Kaveh


2005-04-02  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* bcmp.c: Fix warnings and implement using memcmp.
	* bcopy.c: Fix warnings.
	* bzero.c: Fix warnings and implement using memset.

diff -rup orig/egcc-CVS20050401/libiberty/bcmp.c egcc-CVS20050401/libiberty/bcmp.c
--- orig/egcc-CVS20050401/libiberty/bcmp.c	2005-03-27 01:10:51.000000000 -0500
+++ egcc-CVS20050401/libiberty/bcmp.c	2005-04-02 11:46:25.981132080 -0500
@@ -15,20 +15,13 @@ result mean @var{x} sorts before @var{y}
 
 */
 
+#include <stddef.h>
+
+extern int memcmp(const void *, const void *, size_t);
 
 int
-bcmp (char *from, char *to, int count)
+bcmp (const void *s1, const void *s2, size_t count)
 {
-  int rtnval = 0;
-
-  while (count-- > 0)
-    {
-      if (*from++ != *to++)
-	{
-	  rtnval = 1;
-	  break;
-	}
-    }
-  return (rtnval);
+  return memcmp (s1, s2, count);
 }
 
diff -rup orig/egcc-CVS20050401/libiberty/bcopy.c egcc-CVS20050401/libiberty/bcopy.c
--- orig/egcc-CVS20050401/libiberty/bcopy.c	2005-03-27 01:10:51.000000000 -0500
+++ egcc-CVS20050401/libiberty/bcopy.c	2005-04-02 11:46:25.982131928 -0500
@@ -9,15 +9,17 @@ Copies @var{length} bytes from memory re
 
 */
 
+#include <stddef.h>
+
 void
-bcopy (register char *src, register char *dest, int len)
+bcopy (const char *src, char *dest, size_t len)
 {
   if (dest < src)
     while (len--)
       *dest++ = *src++;
   else
     {
-      char *lasts = src + (len-1);
+      const char *lasts = src + (len-1);
       char *lastd = dest + (len-1);
       while (len--)
         *(char *)lastd-- = *(char *)lasts--;
diff -rup orig/egcc-CVS20050401/libiberty/bzero.c egcc-CVS20050401/libiberty/bzero.c
--- orig/egcc-CVS20050401/libiberty/bzero.c	2005-03-27 01:10:51.000000000 -0500
+++ egcc-CVS20050401/libiberty/bzero.c	2005-04-02 11:51:32.904472640 -0500
@@ -12,12 +12,12 @@ is deprecated in favor of @code{memset}.
 
 */
 
+#include <stddef.h>
+
+extern void *memset(void *, int, size_t);
 
 void
-bzero (char *to, int count)
+bzero (void *to, size_t count)
 {
-  while (count-- > 0)
-    {
-      *to++ = 0;
-    }
+  memset (to, 0, count);
 }


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