diff --git a/xmalloc.h b/xmalloc.h new file mode 100644 index 00000000..40820135 --- /dev/null +++ b/xmalloc.h @@ -0,0 +1,26 @@ +#ifndef _XMALLOC_H +#define _XMALLOC_H 1 + +#include +#include + +__attribute__ ((noreturn, unused)) static void out_of_memory() { + fprintf(stderr,"out of memory\n"); + abort(); +} + +__attribute__ ((unused)) static void *xmalloc(size_t size) { + void *ptr = malloc(size); + if (__builtin_expect(ptr == NULL, 0)) + out_of_memory(); + return(ptr); +} + +__attribute__ ((unused)) static char *xstrndup(const char *s, size_t n) { + char *ptr = strndup(s, n); + if (__builtin_expect(ptr == NULL, 0)) + out_of_memory(); + return(ptr); +} + +#endif