Date: Fri, 5 Aug 2005 05:38:48 +1000 From: Peter Jeremy <PeterJeremy@optushome.com.au> To: Andrey Chernov <ache@freebsd.org>, Maxim.Sobolev@portaone.com, Dan Nelson <dnelson@allantgroup.com>, "current@freebsd.org" <current@freebsd.org> Subject: Re: Sub-optimal libc's read-ahead buffering behaviour Message-ID: <20050804193848.GC2104@cirb503493.alcatel.com.au> In-Reply-To: <20050804105208.GA26150@nagual.pp.ru> References: <42F0CCD5.9090200@portaone.com> <20050803150117.GD93405@dan.emsphone.com> <42F0E9B2.9080208@portaone.com> <20050804060251.GA21228@nagual.pp.ru> <20050804063908.GA21871@nagual.pp.ru> <20050804075711.GB271@cirb503493.alcatel.com.au> <20050804082527.GA22992@nagual.pp.ru> <20050804104236.GC271@cirb503493.alcatel.com.au> <20050804105208.GA26150@nagual.pp.ru>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2005-Aug-04 14:52:08 +0400, Andrey Chernov wrote: >But there is no similar user-visible knob to turn on/off fseek's in-buffer >seeking, so it is always off for chardev for more safety. ... >> In both cases above, seek really needs to be intelligent - more so >> than for regular files. It needs to lseek() in multiples of the >> device block size and then adjust the buffer offset to handle any >> remainder. > >I don't understand this statement well enough. Currently fseek always >sense in-buffer data for regular files for both SEEK_SET and SEEK_CUR. Consider buffered stdio to a disk device. The underlying device requires I/O to be in multiples of 512 bytes with an offset at multiples of 512 bytes. IMHO, these alignment requirements should be hidden from the user - I should be able to write code like: c = getc(disk); fseek(disk, 3, SEEK_CUR); w = getw(disk); fseek(disk, 1, SEEK_CUR); c1 = getc(disk); fseek(disk, c1, SEEK_CUR); to work my way through data on the disk. Currently, I can't do that because the fseek() is transparent and the underlying lseek() will fail. Instead, I need to write code like: fread(buf, sizef(buf), 1, disk); char *bp = buf; ... /* getc */ c = *bp++; if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* fseek(+3) */ bp += 3; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* getw */ if (buf + sizeof(buf) - bp > sizeof(int)) { w = *(int *)bp; bp += sizeof(int); } else { int i; for (w = i = 0; i < sizeof(int); i++) w |= *bp++ << (i * 8); if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } } } /* fseek(+1) */ bp++; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* getc */ c1 = *bp++; if (bp == buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } /* fseek(+c1) */ bp += c1; if (bp >= buf + sizeof(buf)) { fread(buf, sizef(buf), 1, disk); bp = buf; } ie, I've had to implement my own buffering which basically negates the whole purpose of stdio. (Note that both sets of code examples need error checking added - which significantly increases the size of each). -- Peter Jeremy
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050804193848.GC2104>