summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2026-05-16 15:00:44 +0800
committerSadeep Madurange <sadeep@asciimx.com>2026-05-16 15:00:44 +0800
commite1a2e4150e1b1e6ddedde72dcb9c7146f6eaf85a (patch)
treebd05c52569e647b5c43038dc76c4037d2f298115
parente3f60d06d45a89ad16831c4ea20bb67277e588b6 (diff)
downloadglacier-e1a2e4150e1b1e6ddedde72dcb9c7146f6eaf85a.tar.gz
Defined helpers for memory management.HEADmaster
-rw-r--r--Makefile2
-rw-r--r--mem.h28
2 files changed, 29 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 59537c7..35d0540 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/mem.h b/mem.h
new file mode 100644
index 0000000..032740c
--- /dev/null
+++ b/mem.h
@@ -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 */