Sub-optimal libc's read-ahead buffering behaviour
Peter Jeremy
PeterJeremy at optushome.com.au
Thu Aug 4 19:39:01 GMT 2005
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
More information about the freebsd-current
mailing list