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]

Re: libiberty, why aren't xcalloc/calloc provided? (PATCH to add 'em)


 > From: Jeffrey A Law <law@cygnus.com>
 > 
 >   In message <199811081707.MAA28818@caip.rutgers.edu>you write:
 >   >     Libiberty doesn't seem to implement xcalloc or calloc.  Any
 >   > reason why?  If not, then I'd like to add them.
 > 
 > No reason I'm aware of.
 > jeff


	Alright then here's a patch to add xcalloc, and a fallback
calloc, to libiberty.  Okay to install?

		--Kaveh



Mon Nov  9 15:48:42 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* configure.in: Check for calloc.

	* calloc.c: New	file.

	* xmalloc.c (xcalloc): New function.


diff -rup orig/egcs-CVS19981108/libiberty/configure.in egcs-CVS19981108/libiberty/configure.in
--- orig/egcs-CVS19981108/libiberty/configure.in	Sun Nov  8 18:28:29 1998
+++ egcs-CVS19981108/libiberty/configure.in	Mon Nov  9 15:11:19 1998
@@ -117,6 +117,7 @@ funcs="$funcs basename"
 funcs="$funcs bcmp"
 funcs="$funcs bcopy"
 funcs="$funcs bzero"
+funcs="$funcs calloc"
 funcs="$funcs clock"
 funcs="$funcs getcwd"
 funcs="$funcs getpagesize"
@@ -156,7 +157,7 @@ checkfuncs="getrusage on_exit psignal st
 # These are neither executed nor required, but they help keep
 # autoheader happy without adding a bunch of text to acconfig.h.
 if test "x" = "y"; then
-  AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bzero clock getcwd)
+  AC_CHECK_FUNCS(asprintf atexit basename bcmp bcopy bzero calloc clock getcwd)
   AC_CHECK_FUNCS(getpagesize index insque memchr memcmp memcpy memmove)
   AC_CHECK_FUNCS(memset random rename rindex sigsetmask strcasecmp)
   AC_CHECK_FUNCS(strchr strdup strncasecmp strrchr strstr strtod strtol)
diff -rup orig/egcs-CVS19981108/libiberty/calloc.c egcs-CVS19981108/libiberty/calloc.c
--- orig/egcs-CVS19981108/libiberty/calloc.c	Mon Nov  9 15:20:48 1998
+++ egcs-CVS19981108/libiberty/calloc.c	Mon Nov  9 14:57:50 1998
@@ -0,0 +1,26 @@
+#include "ansidecl.h"
+#include "libiberty.h"
+ 
+#ifdef ANSI_PROTOTYPES
+#include <stddef.h>
+#else
+#define size_t unsigned long
+#endif
+
+/* For systems with larger pointers than ints, this must be declared.  */
+PTR malloc PARAMS ((size_t));
+
+PTR
+calloc (nelem, elsize)
+  size_t nelem, elsize;
+{
+  register PTR ptr;  
+
+  if (nelem == 0 || elsize == 0)
+    nelem = elsize = 1;
+  
+  ptr = malloc (nelem * elsize);
+  if (ptr) bzero (ptr, nelem * elsize);
+  
+  return ptr;
+}
diff -rup orig/egcs-CVS19981108/libiberty/xmalloc.c egcs-CVS19981108/libiberty/xmalloc.c
--- orig/egcs-CVS19981108/libiberty/xmalloc.c	Sun Nov  8 18:28:33 1998
+++ egcs-CVS19981108/libiberty/xmalloc.c	Mon Nov  9 15:05:29 1998
@@ -36,6 +36,7 @@ Boston, MA 02111-1307, USA.  */
 /* For systems with larger pointers than ints, these must be declared.  */
 PTR malloc PARAMS ((size_t));
 PTR realloc PARAMS ((PTR, size_t));
+PTR calloc PARAMS ((size_t, size_t));
 PTR sbrk PARAMS ((ptrdiff_t));
 #endif
 
@@ -88,6 +89,41 @@ xmalloc (size)
               "\n%s%sCan not allocate %lu bytes\n",
               name, *name ? ": " : "",
               (unsigned long) size);
+#endif /* ! _WIN32 || __CYGWIN32 __ */
+      xexit (1);
+    }
+  return (newmem);
+}
+
+PTR
+xcalloc (nelem, elsize)
+  size_t nelem, elsize;
+{
+  PTR newmem;
+
+  if (nelem == 0 || elsize == 0)
+    nelem = elsize = 1;
+
+  newmem = calloc (nelem, elsize);
+  if (!newmem)
+    {
+#if ! defined (_WIN32) || defined (__CYGWIN32__)
+      extern char **environ;
+      size_t allocated;
+
+      if (first_break != NULL)
+	allocated = (char *) sbrk (0) - first_break;
+      else
+	allocated = (char *) sbrk (0) - (char *) &environ;
+      fprintf (stderr,
+	       "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
+	       name, *name ? ": " : "",
+	       (unsigned long) (nelem * elsize), (unsigned long) allocated);
+#else
+      fprintf (stderr,
+              "\n%s%sCan not allocate %lu bytes\n",
+              name, *name ? ": " : "",
+              (unsigned long) (nelem * elsize));
 #endif /* ! _WIN32 || __CYGWIN32 __ */
       xexit (1);
     }


Mon Nov  9 15:48:42 1998  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* libiberty.h: Prototype xcalloc.


diff -rup orig/egcs-CVS19981108/include/libiberty.h egcs-CVS19981108/include/libiberty.h
--- orig/egcs-CVS19981108/include/libiberty.h	Sun Nov  8 18:28:06 1998
+++ egcs-CVS19981108/include/libiberty.h	Mon Nov  9 14:59:43 1998
@@ -135,6 +135,11 @@ extern PTR xmalloc PARAMS ((size_t));
 
 extern PTR xrealloc PARAMS ((PTR, size_t));
 
+/* Allocate memory without fail and set it to zero.  This works like
+   xmalloc.  */
+
+extern PTR xcalloc PARAMS ((size_t, size_t));
+
 /* Copy a string into a memory buffer without fail.  */
 
 extern char *xstrdup PARAMS ((const char *));


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