diff --git a/programs/detect.sh b/programs/detect.sh index 9fc576f..0ba10b0 100755 --- a/programs/detect.sh +++ b/programs/detect.sh @@ -27,6 +27,8 @@ check_function() { check_function clock_gettime check_function futimens check_function futimes +check_function posix_fadvise +check_function posix_madvise echo echo "#endif /* _CONFIG_H */" diff --git a/programs/prog_util.c b/programs/prog_util.c index f5f92c9..320d2e3 100644 --- a/programs/prog_util.c +++ b/programs/prog_util.c @@ -39,6 +39,16 @@ # include #endif +#ifndef O_BINARY +# define O_BINARY 0 +#endif +#ifndef O_NOFOLLOW +# define O_NOFOLLOW 0 +#endif +#ifndef O_SEQUENTIAL +# define O_SEQUENTIAL 0 +#endif + /* The invocation name of the program (filename component only) */ const tchar *program_invocation_name; @@ -173,13 +183,17 @@ xopen_for_read(const tchar *path, struct file_stream *strm) if (strm->name == NULL) return -1; - strm->fd = topen(path, O_RDONLY | O_BINARY | O_NOFOLLOW); + strm->fd = topen(path, O_RDONLY | O_BINARY | O_NOFOLLOW | O_SEQUENTIAL); if (strm->fd < 0) { msg_errno("Can't open %"TS" for reading", strm->name); free(strm->name); return -1; } +#if defined(HAVE_POSIX_FADVISE) && (O_SEQUENTIAL == 0) + posix_fadvise(strm->fd, 0, 0, POSIX_FADV_SEQUENTIAL); +#endif + return 0; } @@ -267,7 +281,7 @@ map_file_contents(struct file_stream *strm, u64 size) CloseHandle((HANDLE)strm->mmap_token); return -1; } -#else +#else /* _WIN32 */ strm->mmap_mem = mmap(NULL, size, PROT_READ, MAP_SHARED, strm->fd, 0); if (strm->mmap_mem == MAP_FAILED) { strm->mmap_mem = NULL; @@ -280,7 +294,12 @@ map_file_contents(struct file_stream *strm, u64 size) } return -1; } + +#ifdef HAVE_POSIX_MADVISE + posix_madvise(strm->mmap_mem, size, POSIX_MADV_SEQUENTIAL); #endif + +#endif /* !_WIN32 */ strm->mmap_size = size; return 0; } diff --git a/programs/prog_util.h b/programs/prog_util.h index b18dd58..b6fe671 100644 --- a/programs/prog_util.h +++ b/programs/prog_util.h @@ -88,7 +88,6 @@ extern int wmain(int argc, wchar_t **argv); # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) # endif -# define O_NOFOLLOW 0 #else /* _WIN32 */ @@ -111,7 +110,6 @@ extern int wmain(int argc, wchar_t **argv); # define tunlink unlink # define tutimbuf utimbuf # define tutime utime -# define O_BINARY 0 #endif /* !_WIN32 */