20
20
21
21
#include "internal.h"
22
22
23
- #if defined(__FreeBSD__ )
24
- #include <fcntl.h>
25
- #define F_RDADVISE F_RDAHEAD
26
- #endif
27
-
28
23
#ifndef DISPATCH_IO_DEBUG
29
24
#define DISPATCH_IO_DEBUG DISPATCH_DEBUG
30
25
#endif
@@ -2235,9 +2230,8 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
2235
2230
(void )chunk_size ;
2236
2231
#else
2237
2232
if (_dispatch_io_get_error (op , NULL , true)) return ;
2238
- #if defined(__linux__ ) || defined(__FreeBSD__ )
2239
- // linux does not support fcntl (F_RDAVISE)
2240
- // define necessary datastructure and use readahead
2233
+ #if !defined(F_RDADVISE )
2234
+ // Compatibility struct whose values may be passed to posix_fadvise()
2241
2235
struct radvisory {
2242
2236
off_t ra_offset ;
2243
2237
int ra_count ;
@@ -2262,20 +2256,25 @@ _dispatch_operation_advise(dispatch_operation_t op, size_t chunk_size)
2262
2256
}
2263
2257
advise .ra_offset = op -> advise_offset ;
2264
2258
op -> advise_offset += advise .ra_count ;
2265
- #if defined(__linux__ )
2266
- _dispatch_io_syscall_switch (err ,
2267
- readahead (op -> fd_entry -> fd , advise .ra_offset , (size_t )advise .ra_count ),
2268
- case EINVAL : break ; // fd does refer to a non-supported filetype
2269
- default : (void )dispatch_assume_zero (err ); break ;
2270
- );
2271
- #else
2259
+ #if defined(F_RDADVISE )
2272
2260
_dispatch_io_syscall_switch (err ,
2273
2261
fcntl (op -> fd_entry -> fd , F_RDADVISE , & advise ),
2274
2262
case EFBIG : break ; // advised past the end of the file rdar://10415691
2275
2263
case ENOTSUP : break ; // not all FS support radvise rdar://13484629
2276
2264
// TODO: set disk status on error
2277
2265
default : (void )dispatch_assume_zero (err ); break ;
2278
2266
);
2267
+ #elif defined(HAVE_POSIX_FADVISE )
2268
+ err = posix_fadvise (op -> fd_entry -> fd , advise .ra_offset ,
2269
+ (off_t )advise .ra_count , POSIX_FADV_WILLNEED );
2270
+ switch (err ) {
2271
+ case 0 : break ;
2272
+ case EINVAL : break ; // unsupported advice or file type
2273
+ case ESPIPE : break ; // fd refers to a pipe or FIFO
2274
+ default : (void )dispatch_assume_zero (err ); break ;
2275
+ }
2276
+ #else
2277
+ #error "_dispatch_operation_advise not implemented on this platform"
2279
2278
#endif
2280
2279
#endif
2281
2280
}
0 commit comments