From a201804e6d3c5e0d99e6c397dcb1c0705ba7bc5c Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 26 Jul 2026 19:58:44 +0800 Subject: wip: prompt just about works. --- mem.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 mem.h (limited to 'mem.h') diff --git a/mem.h b/mem.h new file mode 100644 index 0000000..2cdb36e --- /dev/null +++ b/mem.h @@ -0,0 +1,38 @@ +#ifndef MEM_H +#define MEM_H + +#include +#include + +#define MALLOC(s) xmalloc((s), __FILE__, __LINE__) +#define CALLOC(n, s) xcalloc((n), (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 *xcalloc(size_t n, size_t s, const char *file, int line) +{ + void *p; + + if (!(p = calloc(n, s))) + err(1, "%s:%d: calloc", 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 */ -- cgit v1.2.3