X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=xmalloc.c;h=e8b93670f0f086503f5f20558df9f5b254711bda;hb=01b9630089d68e7e0e570cf9793c00a8519df973;hp=22eb7cf49a6ce0de945327140421f2d7c7a31192;hpb=da0ee5e7efab316635f59d212028844f848fa6ce;p=fips diff --git a/xmalloc.c b/xmalloc.c index 22eb7cf..e8b9367 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -21,6 +21,7 @@ #include #include +#include #include "xmalloc.h" @@ -30,6 +31,52 @@ xmalloc (size_t size) void *ret; ret = malloc (size); + if (size != 0 && ret == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + return ret; +} + +void * +xcalloc (size_t nmemb, size_t size) +{ + void *ret; + + ret = calloc (nmemb, size); + if (size != 0 && ret == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + return ret; +} + +void * +xrealloc (void *ptr, size_t size) +{ + void *ret; + + ret = realloc (ptr, size); + if (size != 0 && ret == NULL) { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + return ret; +} + +char * +xstrdup (const char *s) +{ + void *ret; + + if (s == NULL) + return NULL; + + ret = strdup (s); + if (ret == NULL) { fprintf (stderr, "Out of memory\n"); exit (1); @@ -37,3 +84,4 @@ xmalloc (size_t size) return ret; } +