Skip to content

Commit f9efa1b

Browse files
committed
Cleaning up casts and parameter in posix file streams. Fixed up putc implementation for file streams to correctly copy the char.
1 parent 00ba4cc commit f9efa1b

File tree

5 files changed

+48
-131
lines changed

5 files changed

+48
-131
lines changed

Release/include/cpprest/details/fileio.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ _ASYNCRTIMP bool __cdecl _close_fsb(_In_ concurrency::streams::details::_file_in
155155
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
156156
_ASYNCRTIMP size_t __cdecl _putn_fsb(_In_ concurrency::streams::details::_file_info *info, _In_ concurrency::streams::details::_filestream_callback *callback, const void *ptr, size_t count, size_t char_size);
157157

158-
/// <summary>
159-
/// Write a single byte to the file stream.
160-
/// </summary>
161-
/// <param name="info">The file info record of the file</param>
162-
/// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
163-
/// <param name="ptr">A pointer to a buffer where the data should be placed</param>
164-
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
165-
_ASYNCRTIMP size_t __cdecl _putc_fsb(_In_ concurrency::streams::details::_file_info *info, _In_ concurrency::streams::details::_filestream_callback *callback, int ch, size_t char_size);
166-
167158
/// <summary>
168159
/// Read data from a file stream into a buffer
169160
/// </summary>

Release/include/cpprest/filestream.h

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,24 @@ namespace details {
277277
/// <returns>A <c>task</c> that holds the value of the byte written. This is EOF if the write operation fails.</returns>
278278
virtual pplx::task<int_type> _putc(_CharType ch)
279279
{
280-
auto result_tce = pplx::task_completion_event<int_type>();
281-
auto callback = new _filestream_callback_putc(m_info, result_tce, ch);
282-
283-
size_t written = _putc_fsb(m_info, callback, ch, sizeof(_CharType));
280+
auto result_tce = pplx::task_completion_event<size_t>();
281+
auto callback = new _filestream_callback_write<size_t>(m_info, result_tce);
284282

285-
if ( written == sizeof(_CharType) )
283+
// Potentially we should consider deprecating this API, it is TERRIBLY inefficient.
284+
auto sharedCh = std::make_shared<_CharType>(ch);
285+
size_t written = _putn_fsb(m_info, callback, sharedCh.get(), 1, sizeof(_CharType));
286+
287+
288+
if (written == sizeof(_CharType))
286289
{
287290
delete callback;
288291
return pplx::task_from_result<int_type>(ch);
289292
}
290-
return pplx::create_task(result_tce);
293+
294+
return pplx::create_task(result_tce).then([sharedCh](size_t)
295+
{
296+
return static_cast<int_type>(*sharedCh);
297+
});
291298
}
292299

293300
/// <summary>
@@ -843,36 +850,6 @@ namespace details {
843850
pplx::task_completion_event<size_t> m_op;
844851
};
845852

846-
class _filestream_callback_putc : public details::_filestream_callback
847-
{
848-
public:
849-
_filestream_callback_putc(_In_ _file_info *info, pplx::task_completion_event<int_type> op, _CharType ch) : m_info(info), m_op(op), m_ch(ch) { }
850-
851-
virtual void on_completed(size_t result)
852-
{
853-
if ( result == sizeof(_CharType) )
854-
{
855-
m_op.set(m_ch);
856-
}
857-
else
858-
{
859-
m_op.set(traits::eof());
860-
}
861-
delete this;
862-
}
863-
864-
virtual void on_error(const std::exception_ptr & e)
865-
{
866-
m_op.set_exception(e);
867-
delete this;
868-
}
869-
870-
private:
871-
_file_info *m_info;
872-
pplx::task_completion_event<int_type> m_op;
873-
int_type m_ch;
874-
};
875-
876853
class _filestream_callback_bumpc : public details::_filestream_callback
877854
{
878855
public:

Release/src/streams/fileio_posix.cpp

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ class io_scheduler
9999
/// demand, it's referenced through a shared_ptr<T> and retrieved through function call.
100100
/// </summary>
101101
boost::mutex _g_lock;
102-
std::shared_ptr<io_scheduler> _g_scheduler;
102+
std::unique_ptr<io_scheduler> _g_scheduler;
103103

104104
/// <summary>
105105
/// Get the I/O scheduler instance.
106106
/// </summary>
107-
std::shared_ptr<io_scheduler> get_scheduler()
107+
io_scheduler * get_scheduler()
108108
{
109109
boost::lock_guard<boost::mutex> lck(_g_lock);
110110
if ( !_g_scheduler )
111111
{
112-
_g_scheduler = std::make_shared<io_scheduler>();
112+
_g_scheduler = std::unique_ptr<io_scheduler>(new io_scheduler());
113113
}
114114

115-
return _g_scheduler;
115+
return _g_scheduler.get();
116116
}
117117

118118
/***
@@ -130,12 +130,11 @@ std::shared_ptr<io_scheduler> get_scheduler()
130130
/// </summary>
131131
struct _file_info_impl : _file_info
132132
{
133-
_file_info_impl(std::shared_ptr<io_scheduler> sched, int handle, std::ios_base::openmode mode, bool buffer_reads) :
133+
_file_info_impl(int handle, std::ios_base::openmode mode, bool buffer_reads) :
134134
_file_info(mode, 512),
135135
m_handle(handle),
136136
m_buffer_reads(buffer_reads),
137-
m_outstanding_writes(0),
138-
m_scheduler(sched)
137+
m_outstanding_writes(0)
139138
{
140139
}
141140

@@ -152,13 +151,6 @@ struct _file_info_impl : _file_info
152151
std::vector<_filestream_callback *> m_sync_waiters;
153152

154153
std::atomic<long> m_outstanding_writes;
155-
156-
/// <summary>
157-
/// A pointer to the scheduler instance used.
158-
/// </summary>
159-
/// <remarks>Even though we're using a singleton scheduler instance, it may not always be
160-
/// the case, so it seems a good idea to keep a pointer here.</remarks>
161-
std::shared_ptr<io_scheduler> m_scheduler;
162154
};
163155

164156
}}}
@@ -174,8 +166,6 @@ bool _finish_create(int fh, _filestream_callback *callback, std::ios_base::openm
174166
{
175167
if ( fh != -1 )
176168
{
177-
std::shared_ptr<io_scheduler> sched = get_scheduler();
178-
179169
// Buffer reads internally if and only if we're just reading (not also writing) and
180170
// if the file is opened exclusively. If either is false, we're better off just
181171
// letting the OS do its buffering, even if it means that prompt reads won't
@@ -188,11 +178,11 @@ bool _finish_create(int fh, _filestream_callback *callback, std::ios_base::openm
188178
lseek(fh, 0, SEEK_END);
189179
}
190180

191-
auto info = new _file_info_impl(sched, fh, mode, buffer);
181+
auto info = new _file_info_impl(fh, mode, buffer);
192182

193183
if ( mode & std::ios_base::app || mode & std::ios_base::ate )
194184
{
195-
info->m_wrpos = (size_t)-1; // Start at the end of the file.
185+
info->m_wrpos = static_cast<size_t>(-1); // Start at the end of the file.
196186
}
197187

198188
callback->on_opened(info);
@@ -285,7 +275,7 @@ bool _close_fsb_nolock(_file_info **info, Concurrency::streams::details::_filest
285275
if ( callback == nullptr ) return false;
286276
if ( info == nullptr || *info == nullptr ) return false;
287277

288-
_file_info_impl *fInfo = (_file_info_impl *)*info;
278+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(*info);
289279

290280
if ( fInfo->m_handle == -1 ) return false;
291281

@@ -344,7 +334,7 @@ bool _close_fsb(_file_info **info, Concurrency::streams::details::_filestream_ca
344334
/// <param name="ptr">A pointer to the data to write</param>
345335
/// <param name="count">The size (in bytes) of the data</param>
346336
/// <returns>0 if the write request is still outstanding, -1 if the request failed, otherwise the size of the data written</returns>
347-
size_t _write_file_async(Concurrency::streams::details::_file_info_impl *fInfo, Concurrency::streams::details::_filestream_callback *callback, const uint8_t *ptr, size_t count, size_t position)
337+
size_t _write_file_async(Concurrency::streams::details::_file_info_impl *fInfo, Concurrency::streams::details::_filestream_callback *callback, const void *ptr, size_t count, size_t position)
348338
{
349339
// async file writes are emulated using tasks
350340
auto sched = get_scheduler();
@@ -357,7 +347,7 @@ size_t _write_file_async(Concurrency::streams::details::_file_info_impl *fInfo,
357347
off_t abs_position;
358348
bool must_restore_pos;
359349
off_t orig_pos;
360-
if( position == (size_t)-1 )
350+
if( position == static_cast<size_t>(-1) )
361351
{
362352
orig_pos = lseek(fInfo->m_handle, 0, SEEK_CUR);
363353
abs_position = lseek(fInfo->m_handle, 0, SEEK_END);
@@ -399,7 +389,6 @@ size_t _write_file_async(Concurrency::streams::details::_file_info_impl *fInfo,
399389
}
400390
}
401391

402-
delete[] ptr; // free buffer
403392
sched->complete_io();
404393
});
405394

@@ -441,7 +430,7 @@ template<typename Func>
441430
class _filestream_callback_fill_buffer : public _filestream_callback
442431
{
443432
public:
444-
_filestream_callback_fill_buffer(_file_info *info, _filestream_callback *callback, Func func) : m_info(info), m_func(func), m_callback(callback) { }
433+
_filestream_callback_fill_buffer(_file_info *info, _filestream_callback *callback, const Func &func) : m_info(info), m_func(func), m_callback(callback) { }
445434

446435
virtual void on_completed(size_t result) override
447436
{
@@ -461,7 +450,7 @@ class _filestream_callback_fill_buffer : public _filestream_callback
461450
};
462451

463452
template<typename Func>
464-
_filestream_callback_fill_buffer<Func> *create_callback(_file_info *info, _filestream_callback *callback, Func func)
453+
_filestream_callback_fill_buffer<Func> *create_callback(_file_info *info, _filestream_callback *callback, const Func &func)
465454
{
466455
return new _filestream_callback_fill_buffer<Func>(info, callback, func);
467456
}
@@ -474,7 +463,7 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
474463
if ( fInfo->m_buffer == nullptr )
475464
{
476465
fInfo->m_bufsize = std::max(PageSize, byteCount);
477-
fInfo->m_buffer = new char[(size_t)fInfo->m_bufsize];
466+
fInfo->m_buffer = new char[static_cast<size_t>(fInfo->m_bufsize)];
478467
fInfo->m_bufoff = fInfo->m_rdpos;
479468

480469
auto cb = create_callback(fInfo, callback,
@@ -500,7 +489,7 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
500489

501490
// Then, we allocate a new buffer.
502491

503-
char *newbuf = new char[(size_t)fInfo->m_bufsize];
492+
char *newbuf = new char[static_cast<size_t>(fInfo->m_bufsize)];
504493

505494
// Then, we copy the unread part to the new buffer and delete the old buffer
506495

@@ -538,14 +527,13 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
538527
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
539528
size_t _getn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::streams::details::_filestream_callback *callback, void *ptr, size_t count, size_t charSize)
540529
{
541-
if ( callback == nullptr ) return (size_t)-1;
542-
if ( info == nullptr ) return (size_t)-1;
530+
if ( callback == nullptr || info == nullptr ) return static_cast<size_t>(-1);
543531

544532
_file_info_impl *fInfo = (_file_info_impl *)info;
545533

546534
pplx::extensibility::scoped_recursive_lock_t lock(info->m_lock);
547535

548-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
536+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
549537

550538
size_t byteCount = count * charSize;
551539

@@ -590,18 +578,15 @@ size_t _getn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::s
590578
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
591579
size_t _putn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::streams::details::_filestream_callback *callback, const void *ptr, size_t count, size_t charSize)
592580
{
593-
if ( callback == nullptr ) return (size_t)-1;
594-
if ( info == nullptr ) return (size_t)-1;
581+
if (callback == nullptr || info == nullptr) return static_cast<size_t>(-1);
595582

596-
_file_info_impl *fInfo = (_file_info_impl *)info;
583+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
597584

598585
pplx::extensibility::scoped_recursive_lock_t lock(fInfo->m_lock);
599586

600-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
587+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
601588

602589
size_t byteSize = count * charSize;
603-
uint8_t *buf = new uint8_t[byteSize];
604-
memcpy(buf, ptr, byteSize);
605590

606591
// To preserve the async write order, we have to move the write head before read.
607592
auto lastPos = fInfo->m_wrpos;
@@ -611,19 +596,7 @@ size_t _putn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::s
611596
lastPos *= charSize;
612597
}
613598

614-
return _write_file_async(fInfo, callback, buf, byteSize, lastPos);
615-
}
616-
617-
/// <summary>
618-
/// Write a single byte to the file stream.
619-
/// </summary>
620-
/// <param name="info">The file info record of the file</param>
621-
/// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
622-
/// <param name="ptr">A pointer to a buffer where the data should be placed</param>
623-
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
624-
size_t _putc_fsb(Concurrency::streams::details::_file_info *info, Concurrency::streams::details::_filestream_callback *callback, int ch, size_t charSize)
625-
{
626-
return _putn_fsb(info, callback, &ch, 1, charSize);
599+
return _write_file_async(fInfo, callback, ptr, byteSize, lastPos);
627600
}
628601

629602
/// <summary>
@@ -637,7 +610,7 @@ bool _sync_fsb(Concurrency::streams::details::_file_info *info, Concurrency::str
637610
if ( callback == nullptr ) return false;
638611
if ( info == nullptr ) return false;
639612

640-
_file_info_impl *fInfo = (_file_info_impl *)info;
613+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
641614

642615
pplx::extensibility::scoped_recursive_lock_t lock(fInfo->m_lock);
643616

@@ -659,13 +632,13 @@ bool _sync_fsb(Concurrency::streams::details::_file_info *info, Concurrency::str
659632
/// <returns>New file position or -1 if error</returns>
660633
size_t _seekrdtoend_fsb(Concurrency::streams::details::_file_info *info, int64_t offset, size_t char_size)
661634
{
662-
if ( info == nullptr ) return (size_t)-1;
635+
if ( info == nullptr ) return static_cast<size_t>(-1);
663636

664-
_file_info_impl *fInfo = (_file_info_impl *)info;
637+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
665638

666639
pplx::extensibility::scoped_recursive_lock_t lock(info->m_lock);
667640

668-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
641+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
669642

670643
if ( fInfo->m_buffer != nullptr )
671644
{
@@ -676,21 +649,21 @@ size_t _seekrdtoend_fsb(Concurrency::streams::details::_file_info *info, int64_t
676649

677650
auto newpos = lseek(fInfo->m_handle, static_cast<off_t>(offset * char_size), SEEK_END);
678651

679-
if ( newpos == -1 ) return (size_t)-1;
652+
if ( newpos == -1 ) return static_cast<size_t>(-1);
680653

681654
fInfo->m_rdpos = newpos / char_size;
682655
return fInfo->m_rdpos;
683656
}
684657

685658
utility::size64_t _get_size(_In_ concurrency::streams::details::_file_info *info, size_t char_size)
686659
{
687-
if ( info == nullptr ) return (size_t)-1;
660+
if ( info == nullptr ) return static_cast<size_t>(-1);
688661

689-
_file_info_impl *fInfo = (_file_info_impl *)info;
662+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
690663

691664
pplx::extensibility::scoped_recursive_lock_t lock(info->m_lock);
692665

693-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
666+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
694667

695668
if ( fInfo->m_buffer != nullptr )
696669
{
@@ -720,13 +693,13 @@ utility::size64_t _get_size(_In_ concurrency::streams::details::_file_info *info
720693
/// <returns>New file position or -1 if error</returns>
721694
size_t _seekrdpos_fsb(Concurrency::streams::details::_file_info *info, size_t pos, size_t)
722695
{
723-
if ( info == nullptr ) return (size_t)-1;
696+
if ( info == nullptr ) return static_cast<size_t>(-1);
724697

725-
_file_info_impl *fInfo = (_file_info_impl *)info;
698+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
726699

727700
pplx::extensibility::scoped_recursive_lock_t lock(info->m_lock);
728701

729-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
702+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
730703

731704
if ( pos < fInfo->m_bufoff || pos > (fInfo->m_bufoff+fInfo->m_buffill) )
732705
{
@@ -747,13 +720,13 @@ size_t _seekrdpos_fsb(Concurrency::streams::details::_file_info *info, size_t po
747720
/// <returns>New file position or -1 if error</returns>
748721
size_t _seekwrpos_fsb(Concurrency::streams::details::_file_info *info, size_t pos, size_t)
749722
{
750-
if ( info == nullptr ) return (size_t)-1;
723+
if ( info == nullptr ) return static_cast<size_t>(-1);
751724

752-
_file_info_impl *fInfo = (_file_info_impl *)info;
725+
_file_info_impl *fInfo = static_cast<_file_info_impl *>(info);
753726

754727
pplx::extensibility::scoped_recursive_lock_t lock(info->m_lock);
755728

756-
if ( fInfo->m_handle == -1 ) return (size_t)-1;
729+
if ( fInfo->m_handle == -1 ) return static_cast<size_t>(-1);
757730

758731
fInfo->m_wrpos = pos;
759732
return fInfo->m_wrpos;

Release/src/streams/fileio_win32.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,18 +809,6 @@ size_t __cdecl _putn_fsb(_In_ streams::details::_file_info *info, _In_ streams::
809809
return _write_file_async(fInfo, callback, ptr, count*char_size, lastPos);
810810
}
811811

812-
/// <summary>
813-
/// Write a single byte to the file stream.
814-
/// </summary>
815-
/// <param name="info">The file info record of the file</param>
816-
/// <param name="callback">A pointer to the callback interface to invoke when the write request is completed.</param>
817-
/// <param name="ptr">A pointer to a buffer where the data should be placed</param>
818-
/// <returns>0 if the read request is still outstanding, -1 if the request failed, otherwise the size of the data read into the buffer</returns>
819-
size_t __cdecl _putc_fsb(_In_ streams::details::_file_info *info, _In_ streams::details::_filestream_callback *callback, int ch, size_t char_size)
820-
{
821-
return _putn_fsb(info, callback, &ch, 1, char_size);
822-
}
823-
824812
/// <summary>
825813
/// Flush all buffered data to the underlying file.
826814
/// </summary>

0 commit comments

Comments
 (0)