]> diplodocus.org Git - nmh/blobdiff - sbr/utils.c
Tweak mh_xrealloc(); print size on error, follow POSIX.
[nmh] / sbr / utils.c
index d3228909549184e695a4f7857bb91ef2141e6040..83c32170223e91de2b6c773fc9cd920cc42b4e45 100644 (file)
@@ -39,26 +39,26 @@ void *mh_xmalloc(size_t size)
     return p;
 }
 
-/*
- * Safely call realloc
- */
-void *
-mh_xrealloc(void *ptr, size_t size)
+/* Call realloc(3), exiting on NULL return. */
+void *mh_xrealloc(void *ptr, size_t size)
 {
-    void *memory;
+    void *new;
 
-    /* Some non-POSIX realloc()s don't cope with realloc(NULL,sz) */
+    /* Copy POSIX behaviour, coping with non-POSIX systems. */
+    if (size == 0) {
+        if (ptr) {
+            free(ptr);
+        }
+        return mh_xmalloc(1); /* Get a unique pointer. */
+    }
     if (!ptr)
         return mh_xmalloc(size);
 
-    if (size == 0)
-        adios(NULL, "Tried to realloc 0 bytes");
-
-    memory = realloc(ptr, size);
-    if (!memory)
-        adios(NULL, "Realloc failed");
+    new = realloc(ptr, size);
+    if (!new)
+        adios(NULL, "realloc failed, size wanted: %zu", size);
 
-    return memory;
+    return new;
 }
 
 /*