diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | mem.h | 28 |
2 files changed, 29 insertions, 1 deletions
@@ -2,7 +2,7 @@ CC = cc CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -g -O0 LDFLAGS = -HDRS = +HDRS = mem.h SRCS = main.c OBJS = $(SRCS:.c=.o) TARGET = glacier @@ -0,0 +1,28 @@ +#ifndef MEM_H +#define MEM_H + +#include <err.h> +#include <stdlib.h> + +#define MALLOC(s) xmalloc((s), __FILE__, __LINE__) +#define REALLOC(p, s) xrealloc((p), (s), __FILE__, __LINE__) + +static inline void *xmalloc(size_t s, const char *file, int line) +{ + void *p; + + if (!(p = malloc(s))) + err(1, "%s:%d: malloc", file, line); + return p; +} + +static inline void *xrealloc(void *ptr, size_t s, const char *file, int line) +{ + void *p; + + if (!(p = realloc(ptr, s))) + err(1, "%s:%d: realloc", file, line); + return p; +} + +#endif /* MEM_H */ |
