ref: a39f014b1f00d5526ab964a37a6af79bc0745f19
parent: 30289d4a13c4c6d6a60ad1a0eff6151c37e2cf49
author: rrt <rrt>
date: Fri Dec 22 20:24:44 EST 2006
Great minds think alike! I had also just added xstrdup. My version actually aborts on error though. Make xcalloc abort on error. Fix condition in xmalloc only to trip for a malloc of zero; otherwise calls that should free won't.
--- a/src/xmalloc.c
+++ b/src/xmalloc.c
@@ -33,7 +33,7 @@
void *xrealloc(void *ptr, size_t newsize)
{
/* Behaviour in this case is unspecified for malloc */
- if (newsize == 0)
+ if (ptr && newsize == 0)
return NULL;
if ((ptr = realloc(ptr, newsize)) == NULL) {
@@ -51,8 +51,10 @@
{
void *ptr = calloc(nmemb, size);
- if (ptr == NULL)
+ if (ptr == NULL) {
st_fail("out of memory");
+ exit(2);
+ }
return ptr;
}
@@ -60,12 +62,14 @@
/*
* Perform a strdup; abort if not possible.
*/
-char * xstrdup(char const * s)
+void *xstrdup(const char *s)
{
- char * ptr = strdup(s);
+ void *t = strdup(s);
- if (ptr == NULL)
+ if (t == NULL) {
st_fail("out of memory");
+ exit(2);
+ }
return ptr;
}
--- a/src/xmalloc.h
+++ b/src/xmalloc.h
@@ -27,6 +27,6 @@
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t newsize);
-char * xstrdup(char const * s);
+char *xstrdup(const char *s);
#endif