From patchwork Fri Oct 2 12:39:17 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tomasz Kaminski X-Patchwork-Id: 145154 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 782974BA2E18 for ; Fri, 2 Oct 2026 12:50:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 782974BA2E18 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BZUQFthg X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 15A8E4BA2E18 for ; Fri, 2 Oct 2026 12:49:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15A8E4BA2E18 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 15A8E4BA2E18 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790945353; cv=none; b=m9ETqAiPV+zxbWrz2tPB1XBsqmNO1uLc9ZuClsH6MN2H7CRM/qyc/l6l3vWhRUK65u/K2/8RSbWIFmy/1dfeyeZyMpWUK7NXCwiczZAWr97SvBMY2JE/kmLLwg+EH1PnJcbGJyWg7pvF4sF1sxJBNOHs89i+VTX4dHR7o7Zwl70= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790945353; c=relaxed/simple; bh=4TTfZmtlzMCNls+R0n28wfbhiERWlpuf2fYAiZrPstY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=dtBGUjGEYm+8Z91EJXhWHjLmyJyqId+xZ/ztaLqp33BPQ6x6smTUs0HJ0mvlI1LLzViZnrne9lQ2QQ59NIGzCnC4UihY8lyzSs2J6btV/wdq2LcUk3tsM9b7R6/mTbzZsrtfVkTnstXmXVWlaJNYG+p7vFEqKH9NqzpsUFyG9gw= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BZUQFthg DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15A8E4BA2E18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790945352; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=88+qb+smL8AqiiY1M4Wn6nMdA5C6X/4whmh6/HasXwU=; b=BZUQFthgkej5M6o4ku8PPZn01DgssqAY8Gdaz0eMLy5YtU5/Jw94bF1wNIfOoERx1nlFnK JGei1ITyW/5XdRLNqpvlW9z9BZq2N4vDMQA4RtF9EeBWN5yHbRzyRCmcJiPOHGUkvzNMQu OYODTwHz0RSuByluqjVtAQsZTNfT+30= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-225-aC5fO9miN3-XSrozh189Ag-1; Fri, 02 Oct 2026 08:49:09 -0400 X-MC-Unique: aC5fO9miN3-XSrozh189Ag-1 X-Mimecast-MFC-AGG-ID: aC5fO9miN3-XSrozh189Ag_1790945348 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 529101955E8C; Fri, 2 Oct 2026 12:49:08 +0000 (UTC) Received: from localhost (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C0D3830000E4; Fri, 2 Oct 2026 12:49:07 +0000 (UTC) From: =?utf-8?q?Tomasz_Kami=C5=84ski?= To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Anlai Lu Subject: [PATCH v4 2/4] libstdc++: Add _Streambuf_sink for direct streambuf formatting Date: Fri, 2 Oct 2026 14:39:17 +0200 Message-ID: <20261002124906.1093441-1-tkaminsk@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ridIlCyj6lDVlYs2sE-XRkqmu9dZcAE-HOOL29iPrl4_1790945348 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Anlai Lu Introduce _Streambuf_sink that writes directly to basic_streambuf via sputn, preferring zero-copy writes into the streambuf put area (pptr/epptr/pbump) and falling back to the stack buffer and bulk sputn. Streambuf I/O errors are tracked internally (_M_write_failed) and reported after formatting completes, so that format-phase exceptions (format_error, bad_alloc) propagate without setting badbit per [ostream.formatted.print]/(4.2). The _M_write_failed is set also when empty streambuf pointer is provided, as this allow correctly propagating failed flag for default constructed ostreambuf_iterator. The _Streambuf_sink is specialized only for char_traits<_CharT>, as only in that case we are guaranteed that __safe_pbump member is present. libstdc++-v3/ChangeLog: * include/std/format (__format::_Streambuf_sink): New class template. * include/std/streambuf: Include bits/version.h. (__format::_Streambuf_sink) [__glibcxx_format]: Declare. (std::basic_streambuf) [__glibcxx_format]: Befriend __format::_Streambuf_sink. Co-authored-by: Tomasz Kamiński Signed-off-by: Anlai Lu --- v4: * remove traits parameter, so specialization works only for char_traits, I do not think that have any realistic impact * updated friend declarations - the char_traits version is always befriened, even if traits does not match, given that they are internal this is OK * set the _M_write_failed flag if __sbuf is null, and added check for _M_write_failed in constructor body and _M_reserve Testing on x86_64-linux wiht next patch. All *format* test already passed in all standard modes, debug, assertions, cxx98 abi. OK for trunk when all test passes? libstdc++-v3/include/std/format | 118 +++++++++++++++++++++++++++++ libstdc++-v3/include/std/streambuf | 9 +++ 2 files changed, 127 insertions(+) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index b4fb59b7397..c7762766909 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3542,6 +3542,124 @@ namespace __format { } }; + // A format sink that writes directly to a basic_streambuf. + // Prefers zero-copy writes into the streambuf's put area + // (pptr/epptr/pbump), falling back to the stack buffer + // (_M_buf) and bulk sputn. + template + class _Streambuf_sink : public _Buf_sink<_CharT> + { + protected: + basic_streambuf<_CharT>* _M_sbuf; + bool _M_write_failed = false; + + bool + _M_on_stack() const noexcept + { return this->_M_used().data() == this->_M_buf; } + + // Try to point our span directly into the streambuf's put + // area for zero-copy writes. Caller must fall back to + // using local buffer (_M_reset(_M_buf)). + bool + _M_reserve_put_area(size_t __n = 0) + { + if (auto __p = _M_sbuf->pptr()) + if (auto __e = _M_sbuf->epptr(); __e > __p) + if (size_t __a(__e - __p); __a >= __n) + { + this->_M_reset(span<_CharT>(__p, __a)); + return true; + } + return false; + } + + // Write characters to the streambuf, tracking errors. + // I/O exceptions from sputn are caught and converted to + // _M_write_failed so they do not propagate as vformat exceptions + // per [ostream.formatted.print]/(4.2). + void + _M_flush() + { + span __s = this->_M_used(); + if (__s.empty() || _M_write_failed) [[unlikely]] + return; + + __try + { + if (!_M_on_stack()) + _M_sbuf->__safe_pbump(__s.size()); + else if (_M_sbuf->sputn(__s.data(), __s.size()) + != static_cast(__s.size())) + _M_write_failed = true; + } + __catch(const __cxxabiv1::__forced_unwind&) + { throw; } + __catch(...) + { _M_write_failed = true; } + } + + void + _M_overflow() override + { + _M_flush(); + if (!_M_write_failed) [[likely]] + if (_M_reserve_put_area()) + return; + this->_M_reset(this->_M_buf); + } + + public: + [[__gnu__::__always_inline__]] + explicit + _Streambuf_sink(basic_streambuf<_CharT>* __sbuf) noexcept + : _M_sbuf(__sbuf), _M_write_failed(!__sbuf) + { + if (!_M_write_failed) [[likely]] + _M_reserve_put_area(); + } + + // _M_bump is not overridden: the default advances _M_next within + // the current span (stack buffer or put area). Actual commit + // to the streambuf happens via _M_flush, called from _M_overflow + // (buffer full) or _M_finish (end of formatting). + + typename _Sink<_CharT>::_Reservation + _M_reserve(size_t __n) override + { + if (_M_write_failed) [[unlikely]] + return { nullptr }; + + if (__n <= this->_M_unused().size()) + return { this }; + + if (!this->_M_used().empty()) + _M_flush(); + + // Try to write directly into the streambuf's put area. + if (_M_reserve_put_area(__n)) + return { this }; + + // Otherwise reset to the stack buffer. + this->_M_reset(this->_M_buf); + if (__n <= this->_M_unused().size()) + return { this }; + + return { nullptr }; + } + + bool + _M_discarding() const override + { return _M_write_failed; } + + void + _M_finish() + { _M_flush(); } + + bool + _M_failed() const noexcept + { return _M_write_failed; } + }; + using _GLIBCXX_STD_C::vector; // A sink that fills a sequence (e.g. std::string, std::vector, std::deque). diff --git a/libstdc++-v3/include/std/streambuf b/libstdc++-v3/include/std/streambuf index 616e44f74a7..0cc643c2a21 100644 --- a/libstdc++-v3/include/std/streambuf +++ b/libstdc++-v3/include/std/streambuf @@ -39,6 +39,8 @@ #include // iostreams +#include + #include #include #include @@ -57,6 +59,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*, basic_streambuf<_CharT, _Traits>*, bool&); +#ifdef __glibcxx_format // C++ >= 20 && HOSTED + namespace __format { template class _Streambuf_sink; } +#endif + /** * @brief The actual work of input and output (interface). * @ingroup io @@ -149,6 +155,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION friend class basic_ostream; friend class istreambuf_iterator; friend class ostreambuf_iterator; +#ifdef __glibcxx_format // C++ >= 20 && HOSTED + friend class __format::_Streambuf_sink; +#endif friend streamsize __copy_streambufs_eof<>(basic_streambuf*, basic_streambuf*, bool&); From patchwork Fri Oct 2 12:49:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tomasz Kaminski X-Patchwork-Id: 145155 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 34B7F4BA23D1 for ; Fri, 2 Oct 2026 12:51:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 34B7F4BA23D1 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Gd7dnw3l X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 3AFB24BA23D1 for ; Fri, 2 Oct 2026 12:50:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3AFB24BA23D1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3AFB24BA23D1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790945458; cv=none; b=xzCia1y+4K/B9D+KSJzldRVZlx2hLCQJrPKgShDesSREWtHC+/gZ99AstqLSLwn0/vBbVzUYLiwPej8TwN8ynW5yQNtE9oXKbbLrCTBG3tBJaYuQjEU2JYbFF1/3OSDTKxvOG5c4nqawTPv32QlplAFOOuBZzWjbfHPf4SJAN0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790945458; c=relaxed/simple; bh=ox7jF6qm/qutwG9VkYGoPnymL0j1E0Sroci79Kxxf2g=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tqBGgwkwH6MbA2Lnfw1vkS6XZbfAcxfaT0zz3wVXboTW4552vVYw2KBQM9qZ57S7TzmAig6JMnwWOeLeDEiAZXNrw68mbZlR3xvPaeHt2BiYYz1or27/K9oeg3uNO3LTJqMzSaFOXo5a+rgmsF9SUR/ymgGpXuI1V/PtrX7z6JE= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Gd7dnw3l DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3AFB24BA23D1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1790945457; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l8JCURG4L20qMW1+ZRsgCCRt0/6ZqPPqZiLfaMjwqtY=; b=Gd7dnw3lsGOWKTDBZOhEYc4CUJ6wXc1V/iY+WVQH+4PtUiwMJeoH8d0od0qcOiP3j8iAws 89MNUJb2mBLgfAfvCejArvDR8Tv9J/unW5bYVPP8ZBKwaixF37gZfBc1cbNZRZhAJLJk/o onYDF/VFPMRyDGVofOkreXxSii0T3SU= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-557-hT64cTf2M52SbsIcCPdaAg-1; Fri, 02 Oct 2026 08:50:56 -0400 X-MC-Unique: hT64cTf2M52SbsIcCPdaAg-1 X-Mimecast-MFC-AGG-ID: hT64cTf2M52SbsIcCPdaAg_1790945454 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 843651955EA0; Fri, 2 Oct 2026 12:50:54 +0000 (UTC) Received: from localhost (headnet05.pony-001.prod.iad2.dc.redhat.com [10.2.32.117]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 05C71180049F; Fri, 2 Oct 2026 12:50:53 +0000 (UTC) From: =?utf-8?q?Tomasz_Kami=C5=84ski?= To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Anlai Lu Subject: [PATCH v4 3/4] libstdc++: Specialize _Iter_sink for ostreambuf_iterator Date: Fri, 2 Oct 2026 14:49:51 +0200 Message-ID: <20261002125052.1107743-1-tkaminsk@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 1N5uFqgAkjiByK9qy_miKaeAF981J1xG7_YuGn8wDMg_1790945454 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, RCVD_IN_SBL_CSS, SPF_HELO_PASS, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: Anlai Lu Add partial specialization of _Iter_sink for ostreambuf_iterator that inherits _Streambuf_sink, replacing per-character sputc with bulk sputn and zero-copy put-area writes. As _Streambuf_sink this specialization is limited only to std::char_traits. All counting and truncation (_M_max) is handled in this specialization so that _Streambuf_sink stays a pure I/O layer. _M_overflow counts all characters and only writes up to the limit, so format_to_n can compute the total output length. _M_discarding returns false for the same reason. The _M_write_failed is propagated to returned iterator on _M_finish. The maximum count uses size_t with _S_no_limit sentinel, matching the _Ptr_sink convention and avoiding signed comparisons. libstdc++-v3/ChangeLog: * include/bits/streambuf_iterator.h (__format::_Iter_sink) [__glibcxx_format]: Declare with specialization for ostreambuf_iterator. (std::ostreambuf_iterator) [__glibcxx_format]: Befriend __format::_Iter_sink for ostreambuf_iterator. * include/std/format (_Iter_sink<_CharT, ostreambuf_iterator<...>>): Define new partial specialization. Co-authored-by: Tomasz Kamiński Signed-off-by: Anlai Lu --- v4: * removes _Traits parameter and updates friend declaration accordingly. Testing on x86_64-linux. *format* already passed. OK for trunk when all test passes? .../include/bits/streambuf_iterator.h | 12 +++ libstdc++-v3/include/std/format | 83 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h index 095928ca4d8..8a881730c9f 100644 --- a/libstdc++-v3/include/bits/streambuf_iterator.h +++ b/libstdc++-v3/include/bits/streambuf_iterator.h @@ -42,6 +42,14 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION +#ifdef __glibcxx_format // C++ >= 20 && HOSTED + namespace __format { + template class _Iter_sink; + template + class _Iter_sink<_CharT, ostreambuf_iterator<_CharT>>; + } +#endif + /** * @addtogroup iterators * @{ @@ -266,6 +274,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, ostreambuf_iterator<_CharT2>); +#ifdef __glibcxx_format // C++ >= 20 && HOSTED + friend class __format::_Iter_sink>; +#endif + private: streambuf_type* _M_sbuf; bool _M_failed; diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index c7762766909..f1cd49dd98a 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3865,6 +3865,89 @@ namespace __format } }; + // Specialization replacing per-character sputc with bulk sputn + // and zero-copy writes into the streambuf's put area. + template + class _Iter_sink<_CharT, ostreambuf_iterator<_CharT>> + : public _Streambuf_sink<_CharT> + { + using _Base = _Streambuf_sink<_CharT>; + using _OutIter = ostreambuf_iterator<_CharT>; + + static constexpr size_t _S_no_limit = size_t(-1); + + void + _M_trim_buf() + { + const size_t __avail = _M_max - _M_count; + if (this->_M_unused().size() > __avail) + this->_M_reset(this->_M_unused().first(__avail)); + } + + protected: + size_t _M_max = _S_no_limit; + size_t _M_count = 0; + + void + _M_overflow() override + { + const size_t __new = this->_M_used().size(); + const size_t __pcount = _M_count; + _M_count += __new; + + if (__pcount >= _M_max) + this->_M_reset(this->_M_buf); + else if (size_t __avail = _M_max - __pcount; __avail > __new) + { + _Base::_M_overflow(); + _M_trim_buf(); + } + else + { + this->_M_flush(); + this->_M_reset(this->_M_buf); + } + } + + bool + _M_discarding() const override + { return false; } + + public: + [[__gnu__::__always_inline__]] + explicit + _Iter_sink(_OutIter __out) + : _Base(__out._M_sbuf) + { } + + [[__gnu__::__always_inline__]] + explicit + _Iter_sink(_OutIter __out, iter_difference_t<_OutIter> __max) + : _Base(__out._M_sbuf), _M_max(__max < 0 ? _S_no_limit : size_t(__max)) + { _M_trim_buf(); } + + typename _Sink<_CharT>::_Reservation + _M_reserve(size_t __n) override + { + if (_M_count < _M_max) + if (size_t __avail = _M_max - _M_count; __avail >= __n) + return _Base::_M_reserve(__n); + return { nullptr }; + } + + format_to_n_result<_OutIter> + _M_finish() && + { + _M_count += this->_M_used().size(); + if (_M_count <= _M_max) + this->_M_flush(); + + _OutIter __out(this->_M_sbuf); + __out._M_failed = this->_M_write_failed; + return { std::move(__out), ptrdiff_t(_M_count) }; + } + }; + // Used for contiguous iterators. // No buffer is used, characters are written straight to the iterator. // We do not know the size of the output range, so the span size just grows