source: project/release/3/sendfile/trunk/sendfile-implementation.c @ 10723

Last change on this file since 10723 was 10723, checked in by Kon Lovett, 13 years ago

Support for Darwin 9.0 (MacOS X 10.5), fixed wrong arg order in sendfile C impl, added errno to sendfile error, made sendfile error a composite conditionSupport for Darwin 9.0 (MacOS X 10.5), fixed wrong arg order in sendfile C impl, added errno to sendfile error, made sendfile error a composite condition.Support for Darwin 9.0 (MacOS X 10.5), fixed wrong arg order in sendfile C impl, added errno to sendfile error, made sendfile error a composite condition..

File size: 2.6 KB
Line 
1#include <unistd.h>
2#include <errno.h>
3#include "os-dep.h"
4
5
6/* gnu linux sendfile syscall */
7/* changes mostly inspired by lighttpd (a very nice http server)
8   keep up the good work folks. */
9
10#ifdef HAVE_POSIX_FADVISE
11#define READ_AHEAD (4 * 1024 * 1024)
12#endif
13
14
15#ifndef HAVE_MADVISE
16/* stub out madivise */
17int madvise(char* a, unsigned b, int c){
18  return 0;
19}
20#endif
21
22
23#if defined(USE_LINUX_IMPL)
24
25double sendfile_implementation(int src,int dst,double offset,size_t to_send){
26  int res;
27  off_t curoffset = (off_t)offset;
28
29
30#ifdef HAVE_POSIX_FADVISE
31  if((posix_fadvise(src,0,0,POSIX_FADV_SEQUENTIAL) < 0) && errno != ENOSYS)
32    return -1;
33#endif
34
35  res = sendfile(dst,src,&curoffset,to_send);
36 
37  if(res == -1 && (errno != EAGAIN &&  errno != EINTR))
38    return -1;
39 
40 
41#ifdef HAVE_POSIX_FADVISE
42  if(((off_t)offset & ~(READ_AHEAD - 1)) != (((off_t)offset + res) & ~(READ_AHEAD - 1)))
43    if(posix_fadvise(src,((off_t)offset + res) & ~(READ_AHEAD - 1),READ_AHEAD,POSIX_FADV_NOREUSE) < 0)
44      return -1;
45#endif
46  return (double)curoffset;
47}
48
49#elif defined(USE_BSD_IMPL)
50
51/* FreeBSD,DragonFly BSD */
52double sendfile_implementation(int src,int dst,double offset,size_t to_send){
53  size_t res = 0;
54  off_t tmpoffset = (off_t)offset;
55
56#ifdef HAVE_POSIX_FADVISE
57  if((posix_fadvise(src,0,0,POSIX_FADV_SEQUENTIAL) < 0) && errno != ENOSYS)
58    return -1;
59#endif
60
61
62  if(sendfile(src,dst,offset,to_send,NULL,&res,0) < 0){
63    if(errno != EAGAIN)
64      return -1;
65  }
66
67#ifdef HAVE_POSIX_FADVISE
68  if(((off_t)offset & ~(READ_AHEAD - 1)) != (((off_t)offset + res) & ~(READ_AHEAD - 1)))
69    if(posix_fadvise(src,((off_t)offset + res) & ~(READ_AHEAD - 1),READ_AHEAD,POSIX_FADV_NOREUSE) < 0)
70      return -1;
71#endif
72
73  return (double)(offset + res);
74}
75
76#elif defined(USE_DARWIN_IMPL)
77
78/* Darwin 9.0+ */
79double sendfile_implementation(int src,int dst,double offset,size_t to_send){
80  off_t res = to_send;
81  //struct sf_hdtr hdtr;
82 
83  //memset(&hdtr,0,sizeof(hdtr));
84  if(sendfile(src,dst,(off_t)offset,&res,NULL/*&hdtr*/,0) < 0){
85    if(errno != EAGAIN)
86      return -1;
87  }
88
89  return (double)(offset + res);
90}
91
92#elif defined(USE_SOLARIS_IMPL)
93
94double sendfile_implementation(int src,int dst,double offset,size_t to_send){
95  sendfilevec_t fvec;
96  size_t written;
97 
98  fvec.sfv_fd = src;
99  fvec.sfv_flag = 0;
100  fvec.sfv_len = to_send;
101  fvec.sfv_off = (off_t)offset;
102   
103  if(sendfilev(dst,&fvec,1,&written) == -1){
104    if(errno != EAGAIN)
105      return -1;
106  }
107
108  return (double)(offset + written);
109}
110
111
112#else
113
114double sendfile_implementation(int src,int dst,double offset,size_t to_send){
115  return 0;
116}
117
118
119#endif
120
121
122
Note: See TracBrowser for help on using the repository browser.