diff options
Diffstat (limited to 'src/util/emalloc.h')
| -rw-r--r-- | src/util/emalloc.h | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util/emalloc.h b/src/util/emalloc.h index 214a773..39efcea 100644 --- a/src/util/emalloc.h +++ b/src/util/emalloc.h @@ -4,12 +4,14 @@ #define emalloc(...) enmalloc(1, __VA_ARGS__) #define emalloc2(...) enmalloc2(1, __VA_ARGS__) +#define emalloc3(...) enmalloc3(1, __VA_ARGS__) #define ecalloc(...) encalloc(1, __VA_ARGS__) #define erealloc(...) enrealloc(1, __VA_ARGS__) #define erealloc2(...) enrealloc2(1, __VA_ARGS__) #define erealloc3(...) enrealloc3(1, __VA_ARGS__) #define malloc2(n, m) malloc(n * m); +#define malloc3(n1, n2, n3) malloc(n1 * n2 * n3); #define realloc2(p, n, m) realloc(p, n * m); #define realloc3(p, n1, n2, n3) realloc(p, n1 * n2 * n3); @@ -32,6 +34,18 @@ enmalloc2(int status, size_t n, size_t m) } static inline void * +enmalloc3(int status, size_t n1, size_t n2, size_t n3) +{ + void *ptr; + size_t n = n1; + if (n2 > SIZE_MAX / n || + n3 > SIZE_MAX / (n *= n2) || + !(ptr = malloc(n * n3))) + 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); |
