diff --git a/src/misc.c b/src/misc.c new file mode 100644 index 0000000000000000000000000000000000000000..97de89488c1f1afb9c95333f97f7b8935d0c3cfb --- /dev/null +++ b/src/misc.c @@ -0,0 +1,47 @@ +/** @file misc.c + * @brief Miscellaneous utilities. + */ + +#include +#include + +extern char *invok_name; + +/** @brief Allocates memory with check and (eventually) error reporting. */ +void* +xmalloc (size_t size) +{ + void *value = malloc(size); + if (value == 0) { + fprintf (stderr, "%s: virtual memory exhausted", invok_name); + exit (1); + } + return value; +} + +/** @brief Reallocates memory */ +void * +xrealloc (void *ptr, size_t size) +{ + void *value = realloc (ptr, size); + if (value == 0) { + fprintf (stderr, "%s: Virtual memory exhausted", invok_name); + exit (1); + } + return value; +} + + +/** @brief Reallocates memory, but now initializes the memory to zero. */ +void* +xcalloc (size_t count, size_t size) +{ + void *value; + + value = calloc (count, size); + if (value == 0){ + fprintf (stderr, "%s: virtual memory exhausted", invok_name); + exit (1); + } + return value; +}