[PATCH v6 02/11] stdio-common: Introduce buffers for implementing printf

Florian Weimer fweimer@redhat.com
Fri Dec 16 22:32:51 GMT 2022


* Noah Goldstein:

> On Fri, Dec 16, 2022 at 12:59 PM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * Noah Goldstein:
>>
>> >> +/* Switch to the file buffer if possible.  If the file has write_ptr
>> >> +   == write_end, use the stage buffer instead.  */
>> >> +void
>> >> +__printf_buffer_to_file_switch (struct __printf_buffer_to_file *buf)
>> >> +{
>> >> +  if (buf->fp->_IO_write_ptr < buf->fp->_IO_write_end)
>> >> +    {
>> >
>> > Think maybe this should have a minimum bound so we don't end up
>> > in a situation where the strnlen / memcpy loop is only doing 1/2
>> > bytes at a time.
>> > Probably something like 32/64 would make sense.
>>
>> I think this would only matter with setvbuf and a small buffer.  We can
>> use half of the staging buffer size (PRINTF_BUFFER_SIZE_TO_FILE_STAGE),
>> that would be your 64 number.
>
> I don't follow, is it impossible for `_IO_write_ptr < _IO_write_end` and for
> the buffer to be less than 64?

Typical buffers are 8192 bytes large, I believe, but when printf is
called, an arbitrary amount can already be consumed.

> Otherwise I it seems `write_ptr` will be set to `_IO_write_ptr` and
> `write_end` to `_IO_write_end` so the loop:
> ```
> +      if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf))
> +        return;
> +      assert (buf->write_ptr != buf->write_end);
> +      size_t to_copy = STRNLEN (s, buf->write_end - buf->write_ptr);
> +      MEMCPY (buf->write_ptr, s, to_copy);
> +      buf->write_ptr += to_copy;
> +      s += to_copy;
> ```
>
> Will be doing 1/2 bytes at a time.

There are two buffers here: the staging buffer, and the (usually larger,
but optional) FILE * buffer.  If there is no FILE * buffer (yet), we
must use the staging buffer.  But we can instruct printf to write
directly to the FILE * buffer if it is there.  Once the buffer is full,
we need to use the staging buffer for a bit, but flushing that into the
FILE * buffer will most likely make the FILE * buffer available again.

The core issue here is that there is no real way to flush the FILE *
once it's full.  We always have to write a character:

      /* The buffer in buf->fp has been filled.  This should just call
         __overflow (buf->fp, EOF), but flush-only overflow is obscure
         and not always correctly implemented.  See bug 28949.  Be
         conservative and switch to a one-character buffer instead, to
         obtain one more character for a regular __overflow call.  */

This is basically a backwards-compatibility kludge for certain C++
programs compiled with GCC 2.95.

Thanks,
Florian



More information about the Libc-alpha mailing list