emalloc.h - blind - suckless command-line video editing utility | |
git clone git://git.suckless.org/blind | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
emalloc.h (2045B) | |
--- | |
1 /* See LICENSE file for copyright and license details. */ | |
2 #include <stdlib.h> | |
3 #include <stdint.h> | |
4 | |
5 #define emalloc(...) enmalloc(1, __VA_ARGS__) | |
6 #define emalloc2(...) enmalloc2(1, __VA_ARGS__) | |
7 #define emalloc3(...) enmalloc3(1, __VA_ARGS__) | |
8 #define ecalloc(...) encalloc(1, __VA_ARGS__) | |
9 #define erealloc(...) enrealloc(1, __VA_ARGS__) | |
10 #define erealloc2(...) enrealloc2(1, __VA_ARGS__) | |
11 #define erealloc3(...) enrealloc3(1, __VA_ARGS__) | |
12 | |
13 #define malloc2(n, m) malloc(n * m); | |
14 #define malloc3(n1, n2, n3) malloc(n1 * n2 * n3); | |
15 #define realloc2(p, n, m) realloc(p, n * m); | |
16 #define realloc3(p, n1, n2, n3) realloc(p, n1 * n2 * n3); | |
17 | |
18 static inline void * | |
19 enmalloc(int status, size_t n) | |
20 { | |
21 void *ptr = malloc(n); | |
22 if (!ptr) | |
23 enprintf(status, "malloc: out of memory\n"); | |
24 return ptr; | |
25 } | |
26 | |
27 static inline void * | |
28 enmalloc2(int status, size_t n, size_t m) | |
29 { | |
30 void *ptr; | |
31 if (n > SIZE_MAX / m || !(ptr = malloc(n * m))) | |
32 enprintf(status, "malloc: out of memory\n"); | |
33 return ptr; | |
34 } | |
35 | |
36 static inline void * | |
37 enmalloc3(int status, size_t n1, size_t n2, size_t n3) | |
38 { | |
39 void *ptr; | |
40 size_t n = n1; | |
41 if (n2 > SIZE_MAX / n || | |
42 n3 > SIZE_MAX / (n *= n2) || | |
43 !(ptr = malloc(n * n3))) | |
44 enprintf(status, "malloc: out of memory\n"); | |
45 return ptr; | |
46 } | |
47 | |
48 static inline void * | |
49 encalloc(int status, size_t n, size_t m) | |
50 { | |
51 void *ptr = calloc(n, m); | |
52 if (!ptr) | |
53 enprintf(status, "calloc: out of memory\n"); | |
54 return ptr; | |
55 } | |
56 | |
57 static inline void * | |
58 enrealloc(int status, void *ptr, size_t n) | |
59 { | |
60 ptr = realloc(ptr, n); | |
61 if (!ptr) | |
62 enprintf(status, "realloc: out of memory\n"); | |
63 return ptr; | |
64 } | |
65 | |
66 static inline void * | |
67 enrealloc2(int status, void *ptr, size_t n, size_t m) | |
68 { | |
69 if (n > SIZE_MAX / m || !(ptr = realloc(ptr, n * m))) | |
70 enprintf(status, "realloc: out of memory\n"); | |
71 return ptr; | |
72 } | |
73 | |
74 static inline void * | |
75 enrealloc3(int status, void *ptr, size_t n1, size_t n2, size_t n3) | |
76 { | |
77 size_t n = n1; | |
78 if (n2 > SIZE_MAX / n || | |
79 n3 > SIZE_MAX / (n *= n2) || | |
80 !(ptr = realloc(ptr, n * n3))) | |
81 enprintf(status, "realloc: out of memory\n"); | |
82 return ptr; | |
83 } |