diff options
| author | Sadeep Madurange <sadeep@asciimx.com> | 2026-07-26 19:58:44 +0800 |
|---|---|---|
| committer | Sadeep Madurange <sadeep@asciimx.com> | 2026-07-26 20:00:08 +0800 |
| commit | a201804e6d3c5e0d99e6c397dcb1c0705ba7bc5c (patch) | |
| tree | 20138b7f7ea33ba6e95885b6b982bbea92b1b556 /mem.h | |
| parent | 4fb8801eebca334bebd52c2e96b03fccc07a1dc5 (diff) | |
| download | wordbook-master.tar.gz | |
Diffstat (limited to 'mem.h')
| -rw-r--r-- | mem.h | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -0,0 +1,38 @@ +#ifndef MEM_H +#define MEM_H + +#include <err.h> +#include <stdlib.h> + +#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 */ |
