@@ -121,8 +121,8 @@ void CALLBACK IoCompletionCallback(
121
121
CASABLANCA_UNREFERENCED_PARAMETER (ctxt);
122
122
CASABLANCA_UNREFERENCED_PARAMETER (instance);
123
123
124
- EXTENDED_OVERLAPPED *pExtOverlapped = ( EXTENDED_OVERLAPPED *) pOverlapped;
125
- pExtOverlapped->func (result, ( DWORD) numberOfBytesTransferred, ( LPOVERLAPPED) pOverlapped);
124
+ EXTENDED_OVERLAPPED *pExtOverlapped = static_cast < EXTENDED_OVERLAPPED *>( pOverlapped) ;
125
+ pExtOverlapped->func (result, static_cast < DWORD>( numberOfBytesTransferred), static_cast < LPOVERLAPPED>( pOverlapped) );
126
126
delete pOverlapped;
127
127
}
128
128
#endif
@@ -272,7 +272,7 @@ bool __cdecl _close_fsb_nolock(_In_ _file_info **info, _In_ streams::details::_f
272
272
_ASSERTE (info != nullptr );
273
273
_ASSERTE (*info != nullptr );
274
274
275
- _file_info_impl *fInfo = ( _file_info_impl *) *info;
275
+ _file_info_impl *fInfo = static_cast < _file_info_impl *>( *info) ;
276
276
277
277
if ( fInfo ->m_handle == INVALID_HANDLE_VALUE ) return false ;
278
278
@@ -372,7 +372,7 @@ VOID CALLBACK _ReadFileCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfByte
372
372
// / <returns>0 if the write request is still outstanding, -1 if the request failed, otherwise the size of the data written</returns>
373
373
size_t _write_file_async (_In_ streams::details::_file_info_impl *fInfo , _In_ streams::details::_filestream_callback *callback, const void *ptr, size_t count, size_t position)
374
374
{
375
- auto pOverlapped = new EXTENDED_OVERLAPPED (_WriteFileCompletionRoutine<streams::details::_file_info_impl>, callback);
375
+ auto pOverlapped = std::unique_ptr<EXTENDED_OVERLAPPED>( new EXTENDED_OVERLAPPED (_WriteFileCompletionRoutine<streams::details::_file_info_impl>, callback) );
376
376
377
377
if (position == static_cast <size_t >(-1 ))
378
378
{
@@ -392,15 +392,19 @@ size_t _write_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ str
392
392
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
393
393
StartThreadpoolIo (static_cast <PTP_IO>(fInfo ->m_io_context ));
394
394
395
- BOOL wrResult = WriteFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped);
395
+ BOOL wrResult = WriteFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped. get () );
396
396
DWORD error = GetLastError ();
397
397
398
398
// WriteFile will return false when a) the operation failed, or b) when the request is still
399
399
// pending. The error code will tell us which is which.
400
- if ( wrResult == FALSE && error == ERROR_IO_PENDING )
400
+ if (wrResult == FALSE && error == ERROR_IO_PENDING)
401
+ {
402
+ // Overlapped is deleted in the threadpool callback.
403
+ pOverlapped.release ();
401
404
return 0 ;
405
+ }
402
406
403
- CancelThreadpoolIo (( PTP_IO) fInfo ->m_io_context );
407
+ CancelThreadpoolIo (static_cast < PTP_IO>( fInfo ->m_io_context ) );
404
408
405
409
size_t result = static_cast <size_t >(-1 );
406
410
@@ -413,14 +417,12 @@ size_t _write_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ str
413
417
result = GetOverlappedResult (fInfo ->m_handle , pOverlapped, &written, FALSE ) ? static_cast <size_t >(written) : static_cast <size_t >(-1 );
414
418
}
415
419
416
- delete pOverlapped;
417
-
418
420
if (result == static_cast <size_t >(-1 ))
419
421
callback->on_error (std::make_exception_ptr (utility::details::create_system_error (error)));
420
422
421
423
return result;
422
424
#else
423
- BOOL wrResult = WriteFile (fInfo ->m_handle , ptr, (DWORD)count, nullptr , pOverlapped);
425
+ BOOL wrResult = WriteFile (fInfo ->m_handle , ptr, (DWORD)count, nullptr , pOverlapped. get () );
424
426
DWORD error = GetLastError ();
425
427
426
428
// 1. If WriteFile returned true, it must be because the operation completed immediately.
@@ -431,16 +433,22 @@ size_t _write_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ str
431
433
// We do not need to call GetOverlappedResult, the workerthread will call the "on_error()" if the WriteFaile falied.
432
434
// "req" is deleted in "_WriteFileCompletionRoutine, "pOverlapped" is deleted in io_scheduler::FileIOCompletionRoutine.
433
435
if (wrResult == TRUE )
436
+ {
437
+ pOverlapped.release ();
434
438
return 0 ;
439
+ }
435
440
436
441
// 2. If WriteFile returned false and GetLastError is ERROR_IO_PENDING, return 0,
437
442
// The xp threadpool will create a workerthread to run "_WriteFileCompletionRoutine" after the operation completed.
438
443
if (wrResult == FALSE && error == ERROR_IO_PENDING)
444
+ {
445
+ // Overlapped is deleted in the threadpool callback.
446
+ pOverlapped.release ();
439
447
return 0 ;
448
+ }
440
449
441
- // 3. If ReadFile returned false and GetLastError is not ERROR_IO_PENDING, we must call "callback->on_error()" and delete .
450
+ // 3. If ReadFile returned false and GetLastError is not ERROR_IO_PENDING, we must call "callback->on_error()".
442
451
// The threadpools will not start the workerthread.
443
- delete pOverlapped;
444
452
callback->on_error (std::make_exception_ptr (utility::details::create_system_error (error)));
445
453
446
454
return static_cast <size_t >(-1 );
@@ -458,7 +466,7 @@ size_t _write_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ str
458
466
// / <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>
459
467
size_t _read_file_async (_In_ streams::details::_file_info_impl *fInfo , _In_ streams::details::_filestream_callback *callback, _Out_writes_ (count) void *ptr, _In_ size_t count, size_t offset)
460
468
{
461
- auto pOverlapped = new EXTENDED_OVERLAPPED (_ReadFileCompletionRoutine<streams::details::_file_info_impl>, callback);
469
+ auto pOverlapped = std::unique_ptr<EXTENDED_OVERLAPPED>( new EXTENDED_OVERLAPPED (_ReadFileCompletionRoutine<streams::details::_file_info_impl>, callback) );
462
470
pOverlapped->Offset = static_cast <DWORD>(offset);
463
471
#ifdef _WIN64
464
472
pOverlapped->OffsetHigh = static_cast <DWORD>(offset >> 32 );
@@ -469,24 +477,26 @@ size_t _read_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ stre
469
477
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
470
478
StartThreadpoolIo ((PTP_IO)fInfo ->m_io_context );
471
479
472
- BOOL wrResult = ReadFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped);
480
+ BOOL wrResult = ReadFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped. get () );
473
481
DWORD error = GetLastError ();
474
482
475
483
// ReadFile will return false when a) the operation failed, or b) when the request is still
476
484
// pending. The error code will tell us which is which.
477
-
478
- if ( wrResult == FALSE && error == ERROR_IO_PENDING )
485
+ if (wrResult == FALSE && error == ERROR_IO_PENDING)
486
+ {
487
+ // Overlapped is deleted in the threadpool callback.
488
+ pOverlapped.release ();
479
489
return 0 ;
490
+ }
480
491
481
492
// We find ourselves here because there was a synchronous completion, either with an error or
482
493
// success. Either way, we don't need the thread pool I/O request here, or the request and
483
494
// overlapped structures.
484
-
485
495
CancelThreadpoolIo (static_cast <PTP_IO>(fInfo ->m_io_context ));
486
496
487
497
size_t result = static_cast <size_t >(-1 );
488
498
489
- if ( wrResult == TRUE )
499
+ if (wrResult == TRUE )
490
500
{
491
501
// If ReadFile returned true, it must be because the operation completed immediately.
492
502
// However, we didn't pass in an address for the number of bytes written, so
@@ -495,20 +505,18 @@ size_t _read_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ stre
495
505
result = GetOverlappedResult (fInfo ->m_handle , pOverlapped, &read, FALSE ) ? static_cast <size_t >(read) : static_cast <size_t >(-1 );
496
506
}
497
507
498
- delete pOverlapped;
499
-
500
- if ( wrResult == FALSE && error == ERROR_HANDLE_EOF )
508
+ if (wrResult == FALSE && error == ERROR_HANDLE_EOF)
501
509
{
502
510
callback->on_completed (0 );
503
511
return 0 ;
504
512
}
505
513
506
- if ( result == static_cast <size_t >(-1 ) )
514
+ if (result == static_cast <size_t >(-1 ))
507
515
callback->on_error (std::make_exception_ptr (utility::details::create_system_error (error)));
508
516
509
517
return result;
510
518
#else
511
- BOOL wrResult = ReadFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped);
519
+ BOOL wrResult = ReadFile (fInfo ->m_handle , ptr, static_cast <DWORD>(count), nullptr , pOverlapped. get () );
512
520
DWORD error = GetLastError ();
513
521
514
522
// 1. If ReadFile returned true, it must be because the operation completed immediately.
@@ -519,25 +527,30 @@ size_t _read_file_async(_In_ streams::details::_file_info_impl *fInfo, _In_ stre
519
527
// We do not need to call GetOverlappedResult, the workerthread will call the "on_error()" if the ReadFile falied.
520
528
// "req" is deleted in "_ReadFileCompletionRoutine, "pOverlapped" is deleted in io_scheduler::FileIOCompletionRoutine.
521
529
if (wrResult == TRUE )
530
+ {
531
+ pOverlapped.release ();
522
532
return 0 ;
533
+ }
523
534
524
535
// 2. If ReadFile returned false and GetLastError is ERROR_IO_PENDING, return 0.
525
536
// The xp threadpool will create a workerthread to run "_WriteFileCompletionRoutine" after the operation completed.
526
537
if (wrResult == FALSE && error == ERROR_IO_PENDING)
538
+ {
539
+ // Overlapped is deleted in the threadpool callback.
540
+ pOverlapped.release ();
527
541
return 0 ;
542
+ }
528
543
529
- // 3. If ReadFile returned false and GetLastError is ERROR_HANDLE_EOF, we must call "callback->on_completed(0)" and delete .
544
+ // 3. If ReadFile returned false and GetLastError is ERROR_HANDLE_EOF, we must call "callback->on_completed(0)".
530
545
// The threadpool will not start the workerthread.
531
546
if (wrResult == FALSE && error == ERROR_HANDLE_EOF)
532
547
{
533
- delete pOverlapped;
534
548
callback->on_completed (0 );
535
549
return 0 ;
536
550
}
537
551
538
- // 4. If ReadFile returned false and GetLastError is not a valid error code, we must call "callback->on_error()" and delete .
552
+ // 4. If ReadFile returned false and GetLastError is not a valid error code, we must call "callback->on_error()".
539
553
// The threadpool will not start the workerthread.
540
- delete pOverlapped;
541
554
callback->on_error (std::make_exception_ptr (utility::details::create_system_error (error)));
542
555
543
556
return static_cast <size_t >(-1 );
0 commit comments