diff --git a/utils.c b/utils.c index d4e733f..03691c0 100644 --- a/utils.c +++ b/utils.c @@ -1,8 +1,19 @@ #define _GNU_SOURCE +#if __APPLE__ && __MACH__ + +#define _DARWIN_C_SOURCE + +#include /* For function fcntl. */ + +#endif /* Apple Macintosh */ + #include #include #include +#include +#include +#include #include #include #include @@ -215,3 +226,26 @@ void print_header(FILE *f, const char *name) "This is free software; see the source for copying conditions.\n" "\n", name); } + +#if __APPLE__ && __MACH__ + +/* This function is a _rough_ approximation of fdatasync(2). */ +static inline int fdatasync(int fd) +{ + return fcntl(fd, F_FULLFSYNC); +} + +/* This function is a _rough_ approximation of posix_fadvise(2). */ +int posix_fadvise(int fd, off_t offset, off_t len, int advice) +{ + switch (advice) { + case POSIX_FADV_SEQUENTIAL: + return fcntl(fd, F_RDAHEAD, 1); + case POSIX_FADV_DONTNEED: + return fcntl(fd, F_NOCACHE, 1); + default: + assert(0); + } +} + +#endif /* Apple Macintosh */ diff --git a/utils.h b/utils.h index 693e48a..8ce8e9a 100644 --- a/utils.h +++ b/utils.h @@ -1,11 +1,9 @@ #ifndef HEADER_UTILS_H #define HEADER_UTILS_H -#include -#include -#include -#include -#include +#include /* For type FILE. */ +#include /* For struct timeval. */ +#include /* For type uint64_t. */ #define SECTOR_SIZE (512) #define GIGABYTES (1024 * 1024 * 1024) @@ -38,34 +36,13 @@ static inline uint64_t random_number(uint64_t prv_number) #if __APPLE__ && __MACH__ -#define _DARWIN_C_SOURCE - -/* For function fcntl. */ -#include -/* For type off_t. */ -#include - -/* This function is a _rough_ approximation of fdatasync(2). */ -static inline int fdatasync(int fd) -{ - return fcntl(fd, F_FULLFSYNC); -} +#include /* For type off_t. */ #define POSIX_FADV_SEQUENTIAL 2 /* Expect sequential page references. */ #define POSIX_FADV_DONTNEED 4 /* Don't need these pages. */ -/* This function is a _rough_ approximation of posix_fadvise(2). */ -static inline int posix_fadvise(int fd, off_t offset, off_t len, int advice) -{ - switch (advice) { - case POSIX_FADV_SEQUENTIAL: - return fcntl(fd, F_RDAHEAD, 1); - case POSIX_FADV_DONTNEED: - return fcntl(fd, F_NOCACHE, 1); - default: - assert(0); - } -} +int fdatasync(int fd); +int posix_fadvise(int fd, off_t offset, off_t len, int advice); #endif /* Apple Macintosh */