diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/emalloc.h | 29 | ||||
| -rw-r--r-- | src/util/io.h | 16 |
2 files changed, 42 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; +} diff --git a/src/util/io.h b/src/util/io.h index 8c646cb..44c0a92 100644 --- a/src/util/io.h +++ b/src/util/io.h @@ -1,5 +1,6 @@ /* See LICENSE file for copyright and license details. */ #include <fcntl.h> +#include <errno.h> #if defined(POSIX_FADV_SEQUENTIAL) # define fadvise_sequential(...) posix_fadvise(__VA_ARGS__, POSIX_FADV_SEQUENTIAL) @@ -17,6 +18,7 @@ #define ereadall(...) enreadall(1, __VA_ARGS__) #define epwriteall(...) enpwriteall(1, __VA_ARGS__) #define ewritezeroes(...) enwritezeroes(1, __VA_ARGS__) +#define egetfile(...) engetfile(1, __VA_ARGS__) int writeall(int fd, void *buf, size_t n); @@ -55,3 +57,17 @@ enwritezeroes(int status, int fd, void *buf, size_t bufsize, size_t n, const cha if (writezeroes(fd, buf, bufsize, n)) enprintf(status, "write %s:", fname); } + +int getfile(int fd, void *bufp, size_t *restrict ptrp, size_t *restrict sizep); + +static inline void +engetfile(int status, int fd, void *bufp, size_t *restrict ptrp, + size_t *restrict sizep, const char *fname) +{ + if (getfile(fd, bufp, ptrp, sizep)) { + if (errno == ENOMEM) + enprintf(status, "realloc:"); + else + enprintf(status, "read %s:", fname); + } +} |
