Added mh_xcalloc().
[mmh] / sbr / utils.c
index 515c9a0..b0e8274 100644 (file)
@@ -49,7 +49,7 @@ mh_xrealloc(void *ptr, size_t size)
 
        /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
        if (!ptr) {
-               return mh_xmalloc(size);
+               return mh_xcalloc((size_t) 1, size);
        }
 
        if (size == 0)
@@ -63,6 +63,24 @@ mh_xrealloc(void *ptr, size_t size)
 }
 
 /*
+ * Safely call calloc
+ */
+void *
+mh_xcalloc(size_t nmemb, size_t size)
+{
+    void *memory;
+
+    if (nmemb == 0  ||  size == 0)
+        adios(EX_SOFTWARE, NULL, "Tried to calloc 0 bytes");
+
+    if ((memory = calloc(nmemb, size))) {
+        return memory;
+    } else {
+        adios(EX_OSERR, NULL, "calloc failed");
+    }
+}
+
+/*
 ** Return the present working directory, if the current directory does not
 ** exist, or is too long, make / the pwd.
 */