[PATCH] stdio-common: Add the fgetln function
Paul Eggert
eggert@cs.ucla.edu
Wed May 4 00:40:04 GMT 2022
If the stream is not already oriented, FreeBSD getln sets the stream to
byte-orientation. Should glibc getln do the same?
On 5/3/22 00:36, Florian Weimer via Libc-alpha wrote:
> + /* Discard the old buffer. This optimizes for a buffered stream,
> + with multiple lines in each buffer. */
> + if (fp->_fgetln_buf != NULL)
> + {
> + free (fp->_fgetln_buf);
> + fp->_fgetln_buf = NULL;
> + }
Hope you don't mind a bit of bikeshedding here....
Why free the fgetln buffer eagerly? Instead, free it only when closing.
That would lessen pressure on the memory allocator and would save a few
insns in fgetln's usual case.
Come to think of it, how about if we restrict fgetln to streams for
which either (1) the user has not called setvbuf with a nonnull buffer,
or (2) the input line fits in the user-supplied setvbuf buffer. Then we
wouldn't need to worry about adding a _fgetln_buf slot, as fgetln could
always return a pointer into the already-existing stdio buffer, possibly
by enlarging the buffer in case (1). (In case (2) fgetln could fail with
ENOMEM if the input line is longer than the user-supplied buffer.) This
would suffice for 99.9% of applications and would be more efficient than
what FreeBSD does, and the whole point of fgetln is low-level efficiency
right?
More information about the Libc-alpha
mailing list