Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Implements filebuf unbuffered. #76629

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

mordante
Copy link
Member

When calling setbuf(nullptr, 0) before performing file operations it should set the file to unbuffered mode. Currently the code avoids buffering internally, but the underlying stream still can buffer.

This is addressed by disabling the buffering of the underlying stream.

Fixes: #60509

@mordante mordante requested a review from a team as a code owner December 30, 2023 19:39
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 30, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 30, 2023

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

When calling setbuf(nullptr, 0) before performing file operations it should set the file to unbuffered mode. Currently the code avoids buffering internally, but the underlying stream still can buffer.

This is addressed by disabling the buffering of the underlying stream.

Fixes: #60509


Full diff: https://github.com/llvm/llvm-project/pull/76629.diff

2 Files Affected:

  • (modified) libcxx/include/fstream (+46-1)
  • (added) libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/setbuf.pass.cpp (+118)
diff --git a/libcxx/include/fstream b/libcxx/include/fstream
index 21fee202873e76..8a2df123486556 100644
--- a/libcxx/include/fstream
+++ b/libcxx/include/fstream
@@ -276,6 +276,30 @@ private:
   state_type __st_;
   state_type __st_last_;
   ios_base::openmode __om_;
+  // Used to track the currently used mode and track whether the output should
+  // be unbuffered.
+  // [filebuf.virtuals]/12
+  //   If setbuf(0, 0) is called on a stream before any I/O has occurred on
+  //   that stream, the stream becomes unbuffered. Otherwise the results are
+  //   implementation-defined.
+  // This allows calling setbuf(0, 0)
+  // - before opening a file,
+  // - after opening a file, before
+  //   - a read
+  //   - a write
+  //   - a seek.
+  // Note that opening a file with ios_base::ate does a seek operation.
+  // Normally underflow, overflow, and sync change this flag to
+  // ios_base::in, ios_base_out, or 0.
+  //
+  // The ios_base::trunc and ios_base::ate flags are used in the following way:
+  // - ios_base::trunc is set upon construction to indicate the unbuffered
+  //   state can be set. When requesting unbuffered output and the file is open
+  //   sets the mode. Else places a request by adding the ios_base::ate flag.
+  // - When a file is opened it checks whether both ios_base::trunc and
+  //   ios_base::ate are set. If so switches to unbuffered mode.
+  // - Using ase::ate in the open mode sets the flag to 0 so future calls to
+  //   setbuf no longer try to set the unbuffered mode.
   ios_base::openmode __cm_;
   bool __owns_eb_;
   bool __owns_ib_;
@@ -294,7 +318,10 @@ private:
       return nullptr;
 
     __om_ = __mode;
+	__try_set_unbuffered_mode();
+
     if (__mode & ios_base::ate) {
+      __cm_ = 0;
       if (fseek(__file_, 0, SEEK_END)) {
         fclose(__file_);
         __file_ = nullptr;
@@ -304,6 +331,23 @@ private:
 
     return this;
   }
+
+  _LIBCPP_HIDE_FROM_ABI void __try_set_unbuffered_mode() {
+    if (__cm_ == (ios_base::trunc | ios_base::ate)) {
+      std::setbuf(__file_, nullptr);
+      __cm_ = 0;
+    }
+  }
+  _LIBCPP_HIDE_FROM_ABI void __try_set_unbuffered_mode(char_type* __s, streamsize __n) {
+    if (__cm_ == ios_base::trunc && __s == nullptr && __n == 0) {
+      if (__file_) {
+        std::setbuf(__file_, nullptr);
+        __cm_ = 0;
+      } else {
+        __cm_ = ios_base::trunc | ios_base::ate;
+      }
+    }
+  }
 };
 
 template <class _CharT, class _Traits>
@@ -319,7 +363,7 @@ basic_filebuf<_CharT, _Traits>::basic_filebuf()
       __st_(),
       __st_last_(),
       __om_(0),
-      __cm_(0),
+      __cm_(ios_base::trunc),
       __owns_eb_(false),
       __owns_ib_(false),
       __always_noconv_(false) {
@@ -780,6 +824,7 @@ template <class _CharT, class _Traits>
 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
   this->setg(nullptr, nullptr, nullptr);
   this->setp(nullptr, nullptr);
+  __try_set_unbuffered_mode(__s, __n);
   if (__owns_eb_)
     delete[] __extbuf_;
   if (__owns_ib_)
diff --git a/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/setbuf.pass.cpp b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/setbuf.pass.cpp
new file mode 100644
index 00000000000000..3f561170f5fb24
--- /dev/null
+++ b/libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/setbuf.pass.cpp
@@ -0,0 +1,118 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <fstream>
+
+// basic_streambuf<charT, traits>* setbuf(char_type* s, streamsize n) override;
+
+#include <fstream>
+#include <cassert>
+
+#include "test_macros.h"
+
+template <class CharT>
+static std::size_t file_size(const char* filename) {
+  FILE* f = std::fopen(filename, "rb");
+  fseek(f, 0, SEEK_END);
+  long result = ftell(f);
+  fclose(f);
+  return result;
+}
+
+template <class CharT>
+struct filebuf : public std::basic_filebuf<CharT> {
+  CharT* base() { return this->pbase(); }
+  CharT* ptr() { return this->pptr(); }
+};
+
+template <class CharT>
+static void buffered_request() {
+  filebuf<CharT> buffer;
+
+  CharT b[10] = {0};
+  assert(buffer.pubsetbuf(b, 10) == &buffer);
+
+  buffer.open("test.dat", std::ios_base::out);
+  buffer.sputc(CharT('a'));
+  assert(b[0] == 'a');
+
+  buffer.close();
+  assert(file_size<CharT>("test.dat") == 1);
+}
+
+template <class CharT>
+static void unbuffered_request_before_open() {
+  filebuf<CharT> buffer;
+
+  assert(buffer.pubsetbuf(nullptr, 0) == &buffer);
+  assert(buffer.base() == nullptr);
+  assert(buffer.ptr() == nullptr);
+
+  buffer.open("test.dat", std::ios_base::out);
+  assert(buffer.base() == nullptr);
+  assert(buffer.ptr() == nullptr);
+
+  buffer.sputc(CharT('a'));
+  assert(buffer.base() == nullptr);
+  assert(buffer.ptr() == nullptr);
+
+  assert(file_size<CharT>("test.dat") == 1);
+}
+
+template <class CharT>
+static void unbuffered_request_after_open() {
+  filebuf<CharT> buffer;
+
+  buffer.open("test.dat", std::ios_base::out);
+
+  assert(buffer.pubsetbuf(nullptr, 0) == &buffer);
+  assert(buffer.base() == nullptr);
+  assert(buffer.ptr() == nullptr);
+
+  buffer.sputc(CharT('a'));
+  assert(buffer.base() == nullptr);
+  assert(buffer.ptr() == nullptr);
+
+  assert(file_size<CharT>("test.dat") == 1);
+}
+
+template <class CharT>
+static void unbuffered_request_after_open_ate() {
+  filebuf<CharT> buffer;
+
+  buffer.open("test.dat", std::ios_base::out | std::ios_base::ate);
+
+  assert(buffer.pubsetbuf(nullptr, 0) == &buffer);
+
+  buffer.sputc(CharT('a'));
+  assert(file_size<CharT>("test.dat") <= 1);
+  // on libc++ buffering is used by default.
+  LIBCPP_ASSERT(file_size<CharT>("test.dat") == 0);
+
+  buffer.close();
+  assert(file_size<CharT>("test.dat") == 1);
+}
+
+template <class CharT>
+static void test() {
+  buffered_request<CharT>();
+
+  unbuffered_request_before_open<CharT>();
+  unbuffered_request_after_open<CharT>();
+  unbuffered_request_after_open_ate<CharT>();
+}
+
+int main(int, char**) {
+  test<char>();
+
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  test<wchar_t>();
+#endif
+
+  return 0;
+}

libcxx/include/fstream Outdated Show resolved Hide resolved
libcxx/include/fstream Outdated Show resolved Hide resolved
Base automatically changed from users/mordante/fstream_refactor_open to main March 3, 2024 12:23
@mordante mordante force-pushed the users/mordante/implements_filebuf_unbuffered branch from a68f28e to 0e68b42 Compare March 3, 2024 13:42
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM with the comments! Thanks for fixing this bug!

When calling setbuf(nullptr, 0) before performing file operations it
should set the file to unbuffered mode. Currently the code avoids
buffering internally, but the underlying stream still can buffer.

This is addressed by disabling the buffering of the underlying stream.

Fixes: #60509
@mordante mordante force-pushed the users/mordante/implements_filebuf_unbuffered branch from 0e68b42 to 2e1652b Compare March 12, 2024 16:55
@mordante mordante merged commit 5afb937 into main Mar 14, 2024
54 checks passed
@mordante mordante deleted the users/mordante/implements_filebuf_unbuffered branch March 14, 2024 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ofstream.rdbuf()->pubsetbuf(nullptr, 0) do not enables "auto-flush" mode
4 participants