This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Question on fseek optimization in newlib


On 06/06/2012 12:47 AM, Can Finner wrote:
> Hi,
> In function _fseek_r, it seems for buffered file(opened for writing),
> newlib always decrease "curoff" by n(=fp->_p - fp->_bf._base).
> The variable “curoff” is returned by “seekfn (ptr, fp->_cookie, 0L,
> SEEK_CUR)”, which should be __seek.
> My question is why should we decrease “curoff” by “n”, in my
> understanding, the file position returned by __seek/lseek does
> not include bytes in writing buffer.

The file position returned by lseek() and the stream position returned
by ftell (and manipulated by fseek) are independent.  POSIX requires
that they be synchronized at certain points, but that they can be
independent most other times.


>     fwrite("hello world\n", 1, 10, pfile);
>     p1 = lseek (fd, 0, SEEK_CUR);

This is a point where POSIX says the lseek() is undefined in relation to
the stream position.

>     fflush (pfile);
>     p2 = lseek (fd, 0, SEEK_CUR);

And thanks to the fflush(), this is a point where POSIX requires the
lseek() to match the ftell() position.

> 
> So, did I miss something important? Please help.

I'm hoping I answered your question, although it wasn't really obvious
to me why you think something was wrong.  As far as I have tested (and I
have tested quite extensively, since some of the stdio behavior in
newlib is due to my patching as a result of my testing), the current
newlib behavior complies with the POSIX requirements for the points
where lseek() must match stream position, without penalizing things to
always require the correspondence in the cases where POSIX permits
optimizations by treating the two positions independently.

-- 
Eric Blake   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]