Skip to content

Commit 182dbdf

Browse files
committed
util: Detect posix_fallocate() instead of assuming
Don't assume that `posix_fallocate()` is available on Linux and not available on other operating systems. At least FreeBSD has it and we are not using it. Properly check whether `posix_fallocate()` is present and use it if it is.
1 parent 7eed413 commit 182dbdf

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

configure.ac

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,22 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],
851851
[ AC_MSG_RESULT(no)]
852852
)
853853

854+
dnl Check for posix_fallocate
855+
AC_MSG_CHECKING(for posix_fallocate)
856+
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
857+
// same as in src/util/system.cpp
858+
#ifdef __linux__
859+
#ifdef _POSIX_C_SOURCE
860+
#undef _POSIX_C_SOURCE
861+
#endif
862+
#define _POSIX_C_SOURCE 200112L
863+
#endif // __linux__
864+
#include <fcntl.h>]],
865+
[[ int f = posix_fallocate(0, 0, 0); ]])],
866+
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_POSIX_FALLOCATE, 1,[Define this symbol if you have posix_fallocate]) ],
867+
[ AC_MSG_RESULT(no)]
868+
)
869+
854870
AC_MSG_CHECKING([for visibility attribute])
855871
AC_LINK_IFELSE([AC_LANG_SOURCE([
856872
int foo_def( void ) __attribute__((visibility("default")));

src/util/system.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#endif
1818

1919
#ifndef WIN32
20-
// for posix_fallocate
20+
// for posix_fallocate, in configure.ac we check if it is present after this
2121
#ifdef __linux__
2222

2323
#ifdef _POSIX_C_SOURCE
@@ -1012,7 +1012,7 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
10121012
}
10131013
ftruncate(fileno(file), static_cast<off_t>(offset) + length);
10141014
#else
1015-
#if defined(__linux__)
1015+
#if defined(HAVE_POSIX_FALLOCATE)
10161016
// Version using posix_fallocate
10171017
off_t nEndPos = (off_t)offset + length;
10181018
if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;

0 commit comments

Comments
 (0)