Document return values
[ACE_TAO.git] / ACE / ace / OS_NS_sys_sendfile.cpp
blob6b744fd8ad18854333f9ad1085dee08c1e7b4d0f
1 #include "ace/OS_NS_sys_sendfile.h"
2 #include "ace/OS_NS_sys_mman.h"
4 #if defined (ACE_WIN32)
5 # include "ace/OS_NS_sys_socket.h"
6 #endif /* ACE_WIN32 */
8 #include "ace/OS_NS_unistd.h"
10 #ifndef ACE_HAS_INLINED_OSCALLS
11 # include "ace/OS_NS_sys_sendfile.inl"
12 #endif /* ACE_HAS_INLINED_OSCALLS */
14 #include "ace/Malloc_Base.h"
15 #include "ace/OS_Memory.h"
16 #include "ace/os_include/os_errno.h"
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
20 #if defined ACE_HAS_SENDFILE && ACE_HAS_SENDFILE == 0
21 ssize_t
22 ACE_OS::sendfile_emulation (ACE_HANDLE out_fd,
23 ACE_HANDLE in_fd,
24 off_t *offset,
25 size_t count)
27 // @@ Is it possible to inline a call to ::TransmitFile() on
28 // MS Windows instead of emulating here?
30 // @@ We may want set up a signal lease (or oplock) if supported by
31 // the platform so that we don't get a bus error if the mmap()ed
32 // file is truncated.
33 void *buf = ACE_OS::mmap (0, count, PROT_READ, MAP_SHARED, in_fd, *offset);
34 const bool use_read = buf == MAP_FAILED && errno == ENODEV;
35 ACE_OFF_T prev_pos;
37 if (use_read)
39 # ifdef ACE_HAS_ALLOC_HOOKS
40 ACE_ALLOCATOR_RETURN (buf,
41 ACE_Allocator::instance ()->malloc (count), -1);
42 # else
43 ACE_NEW_RETURN (buf, char[count], -1);
44 # endif
45 prev_pos = ACE_OS::lseek (in_fd, 0, SEEK_CUR);
46 if (ACE_OS::lseek (in_fd, *offset, SEEK_SET) == -1
47 || ACE_OS::read (in_fd, buf, count) == -1)
48 return -1;
50 else if (buf == MAP_FAILED)
51 return -1;
53 #if defined (ACE_WIN32)
54 ssize_t const r =
55 ACE_OS::send (out_fd, static_cast<const char *> (buf), count);
56 #else
57 ssize_t const r = ACE_OS::write (out_fd, buf, count);
58 #endif /* ACE_WIN32 */
60 if (use_read)
62 ACE_OS::lseek (in_fd, prev_pos, SEEK_SET);
63 # ifdef ACE_HAS_ALLOC_HOOKS
64 ACE_Allocator::instance ()->free (buf);
65 # else
66 delete[] static_cast<char *> (buf);
67 # endif
69 else
70 (void) ACE_OS::munmap (buf, count);
72 if (r > 0)
73 *offset += static_cast<off_t> (r);
75 return r;
77 #endif /* ACE_HAS_SENDFILE==0 */
79 ACE_END_VERSIONED_NAMESPACE_DECL