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
rather than exit; it's sometimes useful to allocate zero, but some
older platforms might not like it.
* utils.h -- utility prototypes
*/
+/* Call malloc(3), exiting on NULL return. */
void *mh_xmalloc(size_t);
+
void *mh_xrealloc(void *, size_t);
void *mh_xcalloc(size_t, size_t);
char *pwd(void);
*/
#define MAXMSGS 256
-/*
- * Safely call malloc
- */
-void *
-mh_xmalloc(size_t size)
+/* Call malloc(3), exiting on NULL return. */
+void *mh_xmalloc(size_t size)
{
- void *memory;
+ void *p;
if (size == 0)
- adios(NULL, "Tried to malloc 0 bytes");
+ size = 1; /* Some mallocs don't like 0. */
+ p = malloc(size);
+ if (!p)
+ adios(NULL, "malloc failed, size wanted: %zu", size);
- memory = malloc(size);
- if (!memory)
- adios(NULL, "Malloc failed");
-
- return memory;
+ return p;
}
/*