aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/emalloc.h
blob: f4a7f12049ce4e91845a803048f1532943088eb7 (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* See LICENSE file for copyright and license details. */
#include <stdlib.h>
#include <stdint.h>

#define emalloc(...)   enmalloc(1, __VA_ARGS__)
#define emalloc2(...)  enmalloc2(1, __VA_ARGS__)
#define ecalloc(...)   encalloc(1, __VA_ARGS__)
#define erealloc(...)  enrealloc(1, __VA_ARGS__)
#define erealloc2(...) enrealloc2(1, __VA_ARGS__)

#define malloc2(n, m)     malloc(n * m);
#define realloc3(p, n, m) realloc(p, n * m);

static inline void *
enmalloc(int status, size_t n)
{
	void *ptr = malloc(n);
	if (!ptr)
		enprintf(status, "malloc: out of memory\n");
	return ptr;
}

static inline void *
enmalloc2(int status, size_t n, size_t m)
{
	void *ptr;
	if (n > SIZE_MAX / m || !(ptr = malloc(n * m)))
		enprintf(status, "malloc: out of memory\n");
	return ptr;
}

static inline void *
encalloc(int status, size_t n, size_t m)
{
	void *ptr = calloc(n, m);
	if (!ptr)
		enprintf(status, "calloc: out of memory\n");
	return ptr;
}

static inline void *
enrealloc(int status, void *ptr, size_t n)
{
	ptr = realloc(ptr, n);
	if (!ptr)
		enprintf(status, "realloc: out of memory\n");
	return ptr;
}

static inline void *
enrealloc2(int status, void *ptr, size_t n, size_t m)
{
	if (n > SIZE_MAX / m || !(ptr = realloc(ptr, n * m)))
		enprintf(status, "realloc: out of memory\n");
	return ptr;
}