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

Noah Goldstein goldstein.w.n@gmail.com
Fri Dec 16 21:14:49 GMT 2022


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?

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.

>
> Thanks,
> Florian
>


More information about the Libc-alpha mailing list