1 #include "ace/FILE_IO.h"
3 #include "ace/Log_Category.h"
4 #include "ace/OS_NS_sys_stat.h"
5 #include "ace/OS_Memory.h"
6 #include "ace/Truncate.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (__ACE_INLINE__)
12 #include "ace/FILE_IO.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_ALLOC_HOOK_DEFINE(ACE_FILE_IO
)
20 ACE_FILE_IO::dump () const
22 #if defined (ACE_HAS_DUMP)
23 ACE_TRACE ("ACE_FILE_IO::dump");
25 ACELIB_DEBUG ((LM_DEBUG
, ACE_BEGIN_DUMP
, this));
27 ACELIB_DEBUG ((LM_DEBUG
, ACE_END_DUMP
));
28 #endif /* ACE_HAS_DUMP */
31 // Simple-minded do nothing constructor.
33 ACE_FILE_IO::ACE_FILE_IO ()
35 ACE_TRACE ("ACE_FILE_IO::ACE_FILE_IO");
38 // Send N char *ptrs and int lengths. Note that the char *'s precede
39 // the ints (basically, an varargs version of writev). The count N is
40 // the *total* number of trailing arguments, *not* a couple of the
41 // number of tuple pairs!
44 ACE_FILE_IO::send (size_t n
, ...) const
46 ACE_TRACE ("ACE_FILE_IO::send");
47 #ifdef ACE_LACKS_VA_FUNCTIONS
49 ACE_NOTSUP_RETURN (-1);
52 int total_tuples
= ACE_Utils::truncate_cast
<int> (n
/ 2);
54 #if defined (ACE_HAS_ALLOCA)
55 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
57 # ifdef ACE_HAS_ALLOC_HOOKS
58 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
59 ACE_Allocator::instance ()->malloc (total_tuples
*
66 # endif /* ACE_HAS_ALLOC_HOOKS */
67 #endif /* !defined (ACE_HAS_ALLOCA) */
71 for (int i
= 0; i
< total_tuples
; i
++)
73 iovp
[i
].iov_base
= va_arg (argp
, char *);
74 iovp
[i
].iov_len
= va_arg (argp
, int);
77 ssize_t result
= ACE_OS::writev (this->get_handle (),
80 #if !defined (ACE_HAS_ALLOCA)
81 # ifdef ACE_HAS_ALLOC_HOOKS
82 ACE_Allocator::instance ()->free (iovp
);
85 # endif /* ACE_HAS_ALLOC_HOOKS */
86 #endif /* !defined (ACE_HAS_ALLOCA) */
89 #endif // ACE_LACKS_VA_FUNCTIONS
92 // This is basically an interface to ACE_OS::readv, that doesn't use
93 // the struct iovec explicitly. The ... can be passed as an arbitrary
94 // number of (char *ptr, int len) tuples. However, the count N is the
95 // *total* number of trailing arguments, *not* a couple of the number
99 ACE_FILE_IO::recv (size_t n
, ...) const
101 ACE_TRACE ("ACE_FILE_IO::recv");
102 #ifdef ACE_LACKS_VA_FUNCTIONS
104 ACE_NOTSUP_RETURN (-1);
107 int total_tuples
= ACE_Utils::truncate_cast
<int> (n
/ 2);
109 #if defined (ACE_HAS_ALLOCA)
110 iovp
= (iovec
*) alloca (total_tuples
* sizeof (iovec
));
112 # ifdef ACE_HAS_ALLOC_HOOKS
113 ACE_ALLOCATOR_RETURN (iovp
, (iovec
*)
114 ACE_Allocator::instance ()->malloc (total_tuples
*
118 ACE_NEW_RETURN (iovp
,
121 # endif /* ACE_HAS_ALLOC_HOOKS */
122 #endif /* !defined (ACE_HAS_ALLOCA) */
126 for (int i
= 0; i
< total_tuples
; i
++)
128 iovp
[i
].iov_base
= va_arg (argp
, char *);
129 iovp
[i
].iov_len
= va_arg (argp
, int);
132 ssize_t
const result
= ACE_OS::readv (this->get_handle (),
135 #if !defined (ACE_HAS_ALLOCA)
136 # ifdef ACE_HAS_ALLOC_HOOKS
137 ACE_Allocator::instance ()->free (iovp
);
140 # endif /* ACE_HAS_ALLOC_HOOKS */
141 #endif /* !defined (ACE_HAS_ALLOCA) */
144 #endif // ACE_LACKS_VA_FUNCTIONS
147 // Allows a client to read from a file without having to provide a
148 // buffer to read. This method determines how much data is in the
149 // file, allocates a buffer of this size, reads in the data, and
150 // returns the number of bytes read.
153 ACE_FILE_IO::recvv (iovec
*io_vec
)
155 ACE_TRACE ("ACE_FILE_IO::recvv");
157 io_vec
->iov_base
= 0;
158 ACE_OFF_T
const length
= ACE_OS::filesize (this->get_handle ());
162 // Restrict to max size we can record in iov_len.
163 size_t len
= ACE_Utils::truncate_cast
<u_long
> (length
);
164 #if defined (ACE_HAS_ALLOC_HOOKS)
165 ACE_ALLOCATOR_RETURN (io_vec
->iov_base
,
166 static_cast<char*>(ACE_Allocator::instance()->malloc(sizeof(char) * len
)),
169 ACE_NEW_RETURN (io_vec
->iov_base
,
172 #endif /* ACE_HAS_ALLOC_HOOKS */
173 io_vec
->iov_len
= static_cast<u_long
> (this->recv_n (io_vec
->iov_base
,
175 return io_vec
->iov_len
;
179 return ACE_Utils::truncate_cast
<ssize_t
> (length
);
183 ACE_END_VERSIONED_NAMESPACE_DECL