summaryrefslogtreecommitdiffstats
path: root/mem.h
blob: 2cdb36e3e776fb7270e3852a98f5b411a0f381ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 */