fd/fmt: Add buffered formatting to fd
This makes it possible to trivially write formatted data to a file
descriptor with built-in buffering to reduce the number of syscalls.
Change-Id: Ib66c062b65e2a611f13be570c2ed5fe5eb208fb7
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include-fd/stdplus/fd/fmt.hpp b/include-fd/stdplus/fd/fmt.hpp
new file mode 100644
index 0000000..9647d0a
--- /dev/null
+++ b/include-fd/stdplus/fd/fmt.hpp
@@ -0,0 +1,40 @@
+#pragma once
+#include <fmt/format.h>
+#include <functional>
+#include <stdplus/fd/intf.hpp>
+
+namespace stdplus
+{
+namespace fd
+{
+
+class FormatBuffer
+{
+ public:
+ explicit FormatBuffer(stdplus::Fd& fd, size_t max = 4096);
+ ~FormatBuffer() noexcept(false);
+ FormatBuffer(const FormatBuffer&) = delete;
+ FormatBuffer(FormatBuffer&&) = default;
+ FormatBuffer& operator=(const FormatBuffer&) = delete;
+ FormatBuffer& operator=(FormatBuffer&&) = default;
+
+ template <typename... Args>
+ void append(fmt::format_string<Args...> fmt, Args&&... args)
+ {
+ fmt::format_to(std::back_inserter(buf), fmt,
+ std::forward<Args>(args)...);
+ writeIfNeeded();
+ }
+
+ void flush();
+
+ private:
+ std::reference_wrapper<stdplus::Fd> fd;
+ fmt::memory_buffer buf;
+ size_t max;
+
+ void writeIfNeeded();
+};
+
+} // namespace fd
+} // namespace stdplus
diff --git a/src/fd/fmt.cpp b/src/fd/fmt.cpp
new file mode 100644
index 0000000..fc6392d
--- /dev/null
+++ b/src/fd/fmt.cpp
@@ -0,0 +1,36 @@
+#include <stdplus/fd/fmt.hpp>
+#include <stdplus/fd/ops.hpp>
+
+namespace stdplus
+{
+namespace fd
+{
+
+FormatBuffer::FormatBuffer(stdplus::Fd& fd, size_t max) : fd(fd), max(max)
+{
+}
+
+FormatBuffer::~FormatBuffer() noexcept(false)
+{
+ flush();
+}
+
+void FormatBuffer::flush()
+{
+ if (buf.size() > 0)
+ {
+ stdplus::fd::write(fd, buf);
+ buf.clear();
+ }
+}
+
+void FormatBuffer::writeIfNeeded()
+{
+ if (buf.size() >= max)
+ {
+ flush();
+ }
+}
+
+} // namespace fd
+} // namespace stdplus
diff --git a/src/meson.build b/src/meson.build
index 4364395..e491878 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -36,6 +36,7 @@
stdplus_srcs += [
'fd/create.cpp',
'fd/dupable.cpp',
+ 'fd/fmt.cpp',
'fd/impl.cpp',
'fd/line.cpp',
'fd/managed.cpp',
diff --git a/test/fd/fmt.cpp b/test/fd/fmt.cpp
new file mode 100644
index 0000000..726b5e5
--- /dev/null
+++ b/test/fd/fmt.cpp
@@ -0,0 +1,35 @@
+#include <gtest/gtest.h>
+
+#include <stdplus/fd/fmt.hpp>
+#include <stdplus/fd/managed.hpp>
+#include <stdplus/util/cexec.hpp>
+#include <sys/mman.h>
+
+namespace stdplus
+{
+namespace fd
+{
+
+TEST(FormatBuffer, Basic)
+{
+ auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
+ {
+ FormatBuffer buf(fd, 4096);
+ buf.append("hi\n");
+ EXPECT_EQ(0, fd.lseek(0, Whence::Cur));
+ buf.flush();
+
+ EXPECT_EQ(3, fd.lseek(0, Whence::Cur));
+ buf.append("{}", std::string(2050, 'a'));
+ EXPECT_EQ(3, fd.lseek(0, Whence::Cur));
+ buf.append("{}", std::string(2050, 'a'));
+ EXPECT_EQ(4103, fd.lseek(0, Whence::Cur));
+
+ buf.append("hi\n");
+ EXPECT_EQ(4103, fd.lseek(0, Whence::Cur));
+ }
+ EXPECT_EQ(4106, fd.lseek(0, Whence::Cur));
+}
+
+} // namespace fd
+} // namespace stdplus
diff --git a/test/meson.build b/test/meson.build
index a0be742..4ffd1a9 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -31,6 +31,7 @@
gtests += [
'fd/dupable',
'fd/managed',
+ 'fd/fmt',
'fd/intf',
'fd/impl',
'fd/line',