diff options
Diffstat (limited to '')
| -rw-r--r-- | src/util.c | 30 | ||||
| -rw-r--r-- | src/util.h | 2 | ||||
| -rw-r--r-- | src/util/emalloc.h | 29 | ||||
| -rw-r--r-- | src/util/io.h | 16 |
4 files changed, 72 insertions, 5 deletions
@@ -166,6 +166,34 @@ writezeroes(int fd, void *buf, size_t bufsize, size_t n) return 0; } +int +getfile(int fd, void *buffer, size_t *restrict ptr, size_t *restrict size) +{ + char *restrict *restrict buf = buffer; + void *new; + size_t r; + + for (;;) { + if (*ptr == *size) { + if (!(new = realloc(*buf, *size << 1))) { + errno = ENOMEM; + return -1; + } + *buf = new; + *size <<= 1; + } + r = read(fd, *buf + *ptr, *size - *ptr); + if (r <= 0) { + if (r) + return -1; + break; + } + *ptr += (size_t)r; + } + + return 0; +} + static inline pid_t enfork(int status) @@ -193,7 +221,7 @@ enfork_jobs(int status, size_t *start, size_t *end, size_t jobs, pid_t **pids) return 1; } *end = n / jobs + s; - *pids = enmalloc(status, jobs * sizeof(**pids)); + *pids = enmalloc2(status, jobs, sizeof(**pids)); for (j = 1; j < jobs; j++) { pid = enfork(status); if (!pid) { @@ -6,7 +6,7 @@ #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define CLIP(A, B, C) ((B) < (A) ? (A) : (B) > (C) ? (C) : (B)) #define STRLEN(STR) (sizeof(STR) - 1) -#define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : (5 * sizeof(TYPE) / 2)) + (TYPE)-1 < 0) +#define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : (5 * sizeof(TYPE) / 2)) + ((TYPE)-1 < 0)) #define USAGE(SYNOPSIS)\ static void usage(void)\ 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); + } +} |
