]> diplodocus.org Git - nmh/commitdiff
Tweak mh_xcalloc(); print size on error, follow POSIX.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 16 Oct 2016 17:17:31 +0000 (18:17 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sun, 16 Oct 2016 17:17:31 +0000 (18:17 +0100)
Using `%zu' for the size_t value.  That might be a problem on older
platforms, but we'll see.  If asked for zero bytes then allocate one to
get a unique pointer.

h/utils.h
sbr/utils.c

index 20ebd2cf1234b56abe91136a1f616340d6b090f6..6a99466cb056cef20fa80cdb60788ba170910596 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -9,7 +9,9 @@ void *mh_xmalloc(size_t);
 /* Call realloc(3), exiting on NULL return. */
 void *mh_xrealloc(void *, size_t);
 
 /* Call realloc(3), exiting on NULL return. */
 void *mh_xrealloc(void *, size_t);
 
+/* Call calloc(3), exiting on NULL return. */
 void *mh_xcalloc(size_t, size_t);
 void *mh_xcalloc(size_t, size_t);
+
 char *pwd(void);
 char *add(const char *, char *);
 char *addlist(char *, const char *);
 char *pwd(void);
 char *add(const char *, char *);
 char *addlist(char *, const char *);
index 83c32170223e91de2b6c773fc9cd920cc42b4e45..0766809d9df06c92e361ff96af0884df911f8d81 100644 (file)
@@ -61,22 +61,19 @@ void *mh_xrealloc(void *ptr, size_t size)
     return new;
 }
 
     return new;
 }
 
-/*
- * Safely call calloc
- */
-void *
-mh_xcalloc(size_t nmemb, size_t size)
+/* Call calloc(3), exiting on NULL return. */
+void *mh_xcalloc(size_t nelem, size_t elsize)
 {
 {
-    void *memory;
+    void *p;
 
 
-    if (nmemb == 0  ||  size == 0)
-        adios(NULL, "Tried to calloc 0 bytes");
+    if (!nelem || !elsize)
+        return mh_xmalloc(1); /* Get a unique pointer. */
 
 
-    if ((memory = calloc(nmemb, size))) {
-        return memory;
-    } else {
-        adios(NULL, "calloc failed");
-    }
+    p = calloc(nelem, elsize);
+    if (!p)
+        adios(NULL, "calloc failed, size wanted: %zu * %zu", nelem, elsize);
+
+    return p;
 }
 
 /*
 }
 
 /*