@@ -99,20 +99,20 @@ class io_scheduler
99
99
// / demand, it's referenced through a shared_ptr<T> and retrieved through function call.
100
100
// / </summary>
101
101
boost::mutex _g_lock;
102
- std::shared_ptr <io_scheduler> _g_scheduler;
102
+ std::unique_ptr <io_scheduler> _g_scheduler;
103
103
104
104
// / <summary>
105
105
// / Get the I/O scheduler instance.
106
106
// / </summary>
107
- std::shared_ptr< io_scheduler> get_scheduler ()
107
+ io_scheduler * get_scheduler ()
108
108
{
109
109
boost::lock_guard<boost::mutex> lck (_g_lock);
110
110
if ( !_g_scheduler )
111
111
{
112
- _g_scheduler = std::make_shared <io_scheduler>();
112
+ _g_scheduler = std::unique_ptr <io_scheduler>(new io_scheduler () );
113
113
}
114
114
115
- return _g_scheduler;
115
+ return _g_scheduler. get () ;
116
116
}
117
117
118
118
/* **
@@ -130,12 +130,11 @@ std::shared_ptr<io_scheduler> get_scheduler()
130
130
// / </summary>
131
131
struct _file_info_impl : _file_info
132
132
{
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) :
134
134
_file_info (mode, 512 ),
135
135
m_handle (handle),
136
136
m_buffer_reads (buffer_reads),
137
- m_outstanding_writes (0 ),
138
- m_scheduler (sched)
137
+ m_outstanding_writes (0 )
139
138
{
140
139
}
141
140
@@ -152,13 +151,6 @@ struct _file_info_impl : _file_info
152
151
std::vector<_filestream_callback *> m_sync_waiters;
153
152
154
153
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;
162
154
};
163
155
164
156
}}}
@@ -174,8 +166,6 @@ bool _finish_create(int fh, _filestream_callback *callback, std::ios_base::openm
174
166
{
175
167
if ( fh != -1 )
176
168
{
177
- std::shared_ptr<io_scheduler> sched = get_scheduler ();
178
-
179
169
// Buffer reads internally if and only if we're just reading (not also writing) and
180
170
// if the file is opened exclusively. If either is false, we're better off just
181
171
// 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
188
178
lseek (fh, 0 , SEEK_END);
189
179
}
190
180
191
- auto info = new _file_info_impl (sched, fh, mode, buffer);
181
+ auto info = new _file_info_impl (fh, mode, buffer);
192
182
193
183
if ( mode & std::ios_base::app || mode & std::ios_base::ate )
194
184
{
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.
196
186
}
197
187
198
188
callback->on_opened (info);
@@ -285,7 +275,7 @@ bool _close_fsb_nolock(_file_info **info, Concurrency::streams::details::_filest
285
275
if ( callback == nullptr ) return false ;
286
276
if ( info == nullptr || *info == nullptr ) return false ;
287
277
288
- _file_info_impl *fInfo = ( _file_info_impl *) *info;
278
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( *info) ;
289
279
290
280
if ( fInfo ->m_handle == -1 ) return false ;
291
281
@@ -344,7 +334,7 @@ bool _close_fsb(_file_info **info, Concurrency::streams::details::_filestream_ca
344
334
// / <param name="ptr">A pointer to the data to write</param>
345
335
// / <param name="count">The size (in bytes) of the data</param>
346
336
// / <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)
348
338
{
349
339
// async file writes are emulated using tasks
350
340
auto sched = get_scheduler ();
@@ -357,7 +347,7 @@ size_t _write_file_async(Concurrency::streams::details::_file_info_impl *fInfo,
357
347
off_t abs_position;
358
348
bool must_restore_pos;
359
349
off_t orig_pos;
360
- if ( position == ( size_t )- 1 )
350
+ if ( position == static_cast < size_t >(- 1 ) )
361
351
{
362
352
orig_pos = lseek (fInfo ->m_handle , 0 , SEEK_CUR);
363
353
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,
399
389
}
400
390
}
401
391
402
- delete[] ptr; // free buffer
403
392
sched->complete_io ();
404
393
});
405
394
@@ -441,7 +430,7 @@ template<typename Func>
441
430
class _filestream_callback_fill_buffer : public _filestream_callback
442
431
{
443
432
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) { }
445
434
446
435
virtual void on_completed (size_t result) override
447
436
{
@@ -461,7 +450,7 @@ class _filestream_callback_fill_buffer : public _filestream_callback
461
450
};
462
451
463
452
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)
465
454
{
466
455
return new _filestream_callback_fill_buffer<Func>(info, callback, func);
467
456
}
@@ -474,7 +463,7 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
474
463
if ( fInfo ->m_buffer == nullptr )
475
464
{
476
465
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 ) ];
478
467
fInfo ->m_bufoff = fInfo ->m_rdpos ;
479
468
480
469
auto cb = create_callback (fInfo , callback,
@@ -500,7 +489,7 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
500
489
501
490
// Then, we allocate a new buffer.
502
491
503
- char *newbuf = new char [( size_t ) fInfo ->m_bufsize ];
492
+ char *newbuf = new char [static_cast < size_t >( fInfo ->m_bufsize ) ];
504
493
505
494
// Then, we copy the unread part to the new buffer and delete the old buffer
506
495
@@ -538,14 +527,13 @@ size_t _fill_buffer_fsb(_file_info_impl *fInfo, _filestream_callback *callback,
538
527
// / <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>
539
528
size_t _getn_fsb (Concurrency::streams::details::_file_info *info, Concurrency::streams::details::_filestream_callback *callback, void *ptr, size_t count, size_t charSize)
540
529
{
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 );
543
531
544
532
_file_info_impl *fInfo = (_file_info_impl *)info;
545
533
546
534
pplx::extensibility::scoped_recursive_lock_t lock (info->m_lock );
547
535
548
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
536
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
549
537
550
538
size_t byteCount = count * charSize;
551
539
@@ -590,18 +578,15 @@ size_t _getn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::s
590
578
// / <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>
591
579
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)
592
580
{
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 );
595
582
596
- _file_info_impl *fInfo = ( _file_info_impl *) info;
583
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
597
584
598
585
pplx::extensibility::scoped_recursive_lock_t lock (fInfo ->m_lock );
599
586
600
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
587
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
601
588
602
589
size_t byteSize = count * charSize;
603
- uint8_t *buf = new uint8_t [byteSize];
604
- memcpy (buf, ptr, byteSize);
605
590
606
591
// To preserve the async write order, we have to move the write head before read.
607
592
auto lastPos = fInfo ->m_wrpos ;
@@ -611,19 +596,7 @@ size_t _putn_fsb(Concurrency::streams::details::_file_info *info, Concurrency::s
611
596
lastPos *= charSize;
612
597
}
613
598
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);
627
600
}
628
601
629
602
// / <summary>
@@ -637,7 +610,7 @@ bool _sync_fsb(Concurrency::streams::details::_file_info *info, Concurrency::str
637
610
if ( callback == nullptr ) return false ;
638
611
if ( info == nullptr ) return false ;
639
612
640
- _file_info_impl *fInfo = ( _file_info_impl *) info;
613
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
641
614
642
615
pplx::extensibility::scoped_recursive_lock_t lock (fInfo ->m_lock );
643
616
@@ -659,13 +632,13 @@ bool _sync_fsb(Concurrency::streams::details::_file_info *info, Concurrency::str
659
632
// / <returns>New file position or -1 if error</returns>
660
633
size_t _seekrdtoend_fsb (Concurrency::streams::details::_file_info *info, int64_t offset, size_t char_size)
661
634
{
662
- if ( info == nullptr ) return ( size_t )- 1 ;
635
+ if ( info == nullptr ) return static_cast < size_t >(- 1 ) ;
663
636
664
- _file_info_impl *fInfo = ( _file_info_impl *) info;
637
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
665
638
666
639
pplx::extensibility::scoped_recursive_lock_t lock (info->m_lock );
667
640
668
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
641
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
669
642
670
643
if ( fInfo ->m_buffer != nullptr )
671
644
{
@@ -676,21 +649,21 @@ size_t _seekrdtoend_fsb(Concurrency::streams::details::_file_info *info, int64_t
676
649
677
650
auto newpos = lseek (fInfo ->m_handle , static_cast <off_t >(offset * char_size), SEEK_END);
678
651
679
- if ( newpos == -1 ) return ( size_t )- 1 ;
652
+ if ( newpos == -1 ) return static_cast < size_t >(- 1 ) ;
680
653
681
654
fInfo ->m_rdpos = newpos / char_size;
682
655
return fInfo ->m_rdpos ;
683
656
}
684
657
685
658
utility::size64_t _get_size (_In_ concurrency::streams::details::_file_info *info, size_t char_size)
686
659
{
687
- if ( info == nullptr ) return ( size_t )- 1 ;
660
+ if ( info == nullptr ) return static_cast < size_t >(- 1 ) ;
688
661
689
- _file_info_impl *fInfo = ( _file_info_impl *) info;
662
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
690
663
691
664
pplx::extensibility::scoped_recursive_lock_t lock (info->m_lock );
692
665
693
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
666
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
694
667
695
668
if ( fInfo ->m_buffer != nullptr )
696
669
{
@@ -720,13 +693,13 @@ utility::size64_t _get_size(_In_ concurrency::streams::details::_file_info *info
720
693
// / <returns>New file position or -1 if error</returns>
721
694
size_t _seekrdpos_fsb (Concurrency::streams::details::_file_info *info, size_t pos, size_t )
722
695
{
723
- if ( info == nullptr ) return ( size_t )- 1 ;
696
+ if ( info == nullptr ) return static_cast < size_t >(- 1 ) ;
724
697
725
- _file_info_impl *fInfo = ( _file_info_impl *) info;
698
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
726
699
727
700
pplx::extensibility::scoped_recursive_lock_t lock (info->m_lock );
728
701
729
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
702
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
730
703
731
704
if ( pos < fInfo ->m_bufoff || pos > (fInfo ->m_bufoff +fInfo ->m_buffill ) )
732
705
{
@@ -747,13 +720,13 @@ size_t _seekrdpos_fsb(Concurrency::streams::details::_file_info *info, size_t po
747
720
// / <returns>New file position or -1 if error</returns>
748
721
size_t _seekwrpos_fsb (Concurrency::streams::details::_file_info *info, size_t pos, size_t )
749
722
{
750
- if ( info == nullptr ) return ( size_t )- 1 ;
723
+ if ( info == nullptr ) return static_cast < size_t >(- 1 ) ;
751
724
752
- _file_info_impl *fInfo = ( _file_info_impl *) info;
725
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( info) ;
753
726
754
727
pplx::extensibility::scoped_recursive_lock_t lock (info->m_lock );
755
728
756
- if ( fInfo ->m_handle == -1 ) return ( size_t )- 1 ;
729
+ if ( fInfo ->m_handle == -1 ) return static_cast < size_t >(- 1 ) ;
757
730
758
731
fInfo ->m_wrpos = pos;
759
732
return fInfo ->m_wrpos ;
0 commit comments