Skip to content

Commit 4e6dcce

Browse files
committed
[libc++][NFC] Refactor __request_unbuffered_mode
It's a bit awkward to pass the char* and the size to the function, which then only decides whether it should be a no-op with it. Instead, only call the function when the pointer is null and n is 0, which matches more closely what the standard says with respect to calling pubsetbuf(0, 0).
1 parent aae2b89 commit 4e6dcce

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

libcxx/include/fstream

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,12 @@ private:
419419
// If the file is already open, switch to unbuffered mode. Otherwise, record
420420
// the request to use unbuffered mode so that we use that mode when we
421421
// eventually open the file.
422-
_LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
423-
if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
424-
if (__file_) {
425-
std::setbuf(__file_, nullptr);
426-
__cm_ = 0;
427-
} else {
428-
__cm_ = __no_io_operations | __use_unbuffered_io;
429-
}
422+
_LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode() {
423+
if (__file_) {
424+
std::setbuf(__file_, nullptr);
425+
__cm_ = 0;
426+
} else {
427+
__cm_ = __no_io_operations | __use_unbuffered_io;
430428
}
431429
}
432430

@@ -934,7 +932,9 @@ template <class _CharT, class _Traits>
934932
basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
935933
this->setg(nullptr, nullptr, nullptr);
936934
this->setp(nullptr, nullptr);
937-
__request_unbuffered_mode(__s, __n);
935+
// Calling setbuf(nullptr, 0) before any i/o operation switches the stream to unbuffered mode
936+
if (__cm_ == __no_io_operations && __s == nullptr && __n == 0)
937+
__request_unbuffered_mode();
938938
if (__owns_eb_)
939939
delete[] __extbuf_;
940940
if (__owns_ib_)

0 commit comments

Comments
 (0)