diff options
Diffstat (limited to 'src/util/emalloc.h')
| -rw-r--r-- | src/util/emalloc.h | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/util/emalloc.h b/src/util/emalloc.h index 81816be..f4a7f12 100644 --- a/src/util/emalloc.h +++ b/src/util/emalloc.h @@ -1,9 +1,15 @@ /* See LICENSE file for copyright and license details. */ #include <stdlib.h> +#include <stdint.h> -#define emalloc(...) enmalloc(1, __VA_ARGS__) -#define ecalloc(...) encalloc(1, __VA_ARGS__) -#define erealloc(...) enrealloc(1, __VA_ARGS__) +#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) @@ -15,6 +21,15 @@ enmalloc(int status, size_t n) } 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); @@ -31,3 +46,11 @@ enrealloc(int status, void *ptr, size_t n) 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; +} |
