Re: image buffering

Michael K. Johnson (johnsonm@redhat.com)
Thu, 29 May 1997 09:38:16 -0400

David Mosberger-Tang writes:
>I don't see how to avoid the 2*imagesize worse-case with pipes. Since
>we don't have any problems in the other case, the question is how
>important do we think this case is. To me it seems it would be quite
>reasonable to say "look, if you use pipes, be prepared that you'll
>need more temp space than if you use a regular file".

Yep. That is, in fact, how I decide what to do with three-pass scanners
in the UMAX driver -- I try to mmap the output, and if it doesn't work,
I malloc an area and write the whole thing out afterwards. The striping
code is exactly the same, because I use the same variable to hold the
malloced area as the mmaped area.

>Seriously though: I now think that using a temporary file is not such
>a bad idea. I think pipes could be supported easily as well: if a
>dummy lseek() fails, then a tmpfile(3) could be used as a temporary
>buffer.

Like I say, that's not even necessary. Just mmap it and see if it
fails. Here's sample code munged from the UMAX driver:

write_after = 0;
lseek(ofd, mmap_size, SEEK_SET);
ftruncate(ofd, mmap_size);
arena = mmap(NULL, mmap_size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
ofd, 0);
if ((! arena) || arena == -1) {
if (verbose) {
fprintf(stderr, "mmap failed, errno=%d: %s\n", errno, strerror(errno));
}
arena = malloc(mmap_size);
write_after = 1;
}

....

/* note that image_size may be different than page-aligned size */
if (write_after) {
write(ofd, arena, image_size);
free(arena);
} else {
munmap(us->arena, mmap_size);
/* will always smaller, so no need to lseek first */
ftruncate(ofd, image_size);
}

With that kind of code, the striping code shouldn't need any changes at all.

michaelkjohnson

"Ever wonder why the SAME PEOPLE make up ALL the conspiracy theories?"

--
Source code, list archive, and docs: http://www.azstarnet.com/~axplinux/sane/
To unsubscribe: mail -s unsubscribe sane-devel-request@listserv.azstarnet.com