summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/io.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/io.c b/src/io.c
index b5bdc08..c797193 100644
--- a/src/io.c
+++ b/src/io.c
@@ -7,6 +7,9 @@
* General Public License as published by the Free Software Foundation;
* either version 2.1 or (at your option) any later version.
*/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
@@ -694,10 +697,13 @@ EXPORT_SYMBOL char *HX_slurp_fd(int fd, size_t *outsize)
if (buf == nullptr)
return nullptr;
ssize_t rdret;
- while ((rdret = read(fd, buf, bufsize - 1 - offset)) > 0) {
+ while ((rdret = read(fd, buf + offset, bufsize - 1 - offset)) > 0) {
offset += rdret;
+ /*
+ * Make it so that the next read call is not called
+ * with an exceptionally small size.
+ */
if (bufsize - offset >= 4095)
- /* any value would work, but >=1 is not all that efficient */
continue;
if (bufsize > SSIZE_MAX)
/* No more doubling */
@@ -720,6 +726,11 @@ EXPORT_SYMBOL char *HX_slurp_fd(int fd, size_t *outsize)
size_t fsize = sb.st_size; /* may truncate from loff_t to size_t */
if (fsize == SIZE_MAX)
--fsize;
+#ifdef HAVE_POSIX_FADVISE
+ if (fsize > 0 && posix_fadvise(fd, 0, fsize,
+ POSIX_FADV_SEQUENTIAL) != 0)
+ /* ignore */;
+#endif
char *buf = malloc(fsize + 1);
if (buf == NULL)
return NULL;