Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / OS_NS_sys_uio.cpp
blobeeccc55a5da97a6ced8a98434de608f6c2131367
1 #include "ace/OS_NS_sys_uio.h"
3 #if !defined (ACE_HAS_INLINED_OSCALLS)
4 # include "ace/OS_NS_sys_uio.inl"
5 #endif /* ACE_HAS_INLINED_OSCALLS */
7 #include "ace/OS_Memory.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_unistd.h"
11 #ifdef ACE_HAS_ALLOC_HOOKS
12 # include "ace/Global_Macros.h"
13 # include "ace/Malloc_Base.h"
14 #endif
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 #if defined (ACE_LACKS_READV)
20 // "Fake" readv for operating systems without it. Note that this is
21 // thread-safe.
23 ssize_t
24 ACE_OS::readv_emulation (ACE_HANDLE handle,
25 const iovec *iov,
26 int n)
28 ACE_OS_TRACE ("ACE_OS::readv_emulation");
30 // In case there's a single element, skip the memcpy.
31 if (1 == n)
32 return ACE_OS::read (handle, iov[0].iov_base, iov[0].iov_len);
34 ssize_t length = 0;
35 int i;
37 for (i = 0; i < n; ++i)
38 if (static_cast<int> (iov[i].iov_len) < 0)
39 return -1;
40 else
41 length += iov[i].iov_len;
43 char *buf;
44 # ifdef ACE_HAS_ALLOC_HOOKS
45 ACE_ALLOCATOR_RETURN (buf,
46 (char *) ACE_Allocator::instance ()->malloc (length),
47 -1);
48 # else
49 ACE_NEW_RETURN (buf,
50 char[length],
51 -1);
52 # endif /* ACE_HAS_ALLOC_HOOKS */
54 length = ACE_OS::read (handle, buf, length);
56 if (length != -1)
58 char *ptr = buf;
59 ssize_t copyn = length;
61 for (i = 0;
62 i < n && copyn > 0;
63 ++i)
65 ACE_OS::memcpy (iov[i].iov_base, ptr,
66 // iov_len is int on some platforms, size_t on others
67 copyn > (int) iov[i].iov_len
68 ? (size_t) iov[i].iov_len
69 : (size_t) copyn);
70 ptr += iov[i].iov_len;
71 copyn -= iov[i].iov_len;
75 # ifdef ACE_HAS_ALLOC_HOOKS
76 ACE_Allocator::instance ()->free (buf);
77 # else
78 delete [] buf;
79 # endif /* ACE_HAS_ALLOC_HOOKS */
80 return length;
82 #endif /* ACE_LACKS_READV */
84 #if defined (ACE_LACKS_WRITEV)
86 // "Fake" writev for operating systems without it. Note that this is
87 // thread-safe.
89 ssize_t
90 ACE_OS::writev_emulation (ACE_HANDLE handle, const iovec *iov, int n)
92 ACE_OS_TRACE ("ACE_OS::writev_emulation");
94 // 'handle' may be a datagram socket (or similar) so this operation
95 // must not be divided into multiple smaller writes.
97 if (n == 1)
98 return ACE_OS::write (handle, iov[0].iov_base, iov[0].iov_len);
100 ssize_t length = 0;
101 for (int i = 0; i < n; ++i)
102 length += iov[i].iov_len;
104 char *buf;
105 # ifdef ACE_HAS_ALLOC_HOOKS
106 ACE_ALLOCATOR_RETURN (buf,
107 (char *) ACE_Allocator::instance ()->malloc (length),
108 -1);
109 # else
110 ACE_NEW_RETURN (buf, char[length], -1);
111 # endif /* ACE_HAS_ALLOC_HOOKS */
113 char *iter = buf;
114 for (int i = 0; i < n; iter += iov[i++].iov_len)
115 ACE_OS::memcpy (iter, iov[i].iov_base, iov[i].iov_len);
117 const ssize_t result = ACE_OS::write (handle, buf, length);
119 # ifdef ACE_HAS_ALLOC_HOOKS
120 ACE_Allocator::instance ()->free (buf);
121 # else
122 delete[] buf;
123 # endif /* ACE_HAS_ALLOC_HOOKS */
125 return result;
127 # endif /* ACE_LACKS_WRITEV */
129 ACE_END_VERSIONED_NAMESPACE_DECL