Re: B*gg*r mallocs, mmap JOY!

Aaron M. Ucko (amu@mit.edu)
17 Feb 1997 11:58:45 -0500


John Carter <john@dwaf-hri.pwv.gov.za> writes:

> On Mon, 17 Feb 1997, Michael Weller wrote:
>
> > I agree and was quite surprised about this usage of ftruncate. What you
> > need to do is simply create an empty file of length <n> when you want to
> > mmap <n> bytes. From my knowledge THE standard way to do so is:
> >
> > fseek(fd, <n>, SEEK_SET);
> >
> > fflush(fd); /* probably not needed, fseek does that. */
> >
> > mmap(... fileno(fd) ...);
>
> Hokay Folks.....
> From the "info glibc" on the subject of fseek....
>
> INFO> You can move the stream's file position with `fseek' (*note File
> INFO> Positioning::.). Moving the file position past the end of the
> INFO> data already written fills the intervening space with zeroes.
>
> Note that ftruncate does it instananeously. It isn't wasting my time
> scribbling zero's to disk when I'm about to scribble lots of more
> interesting things directly thereafter. (It takes a looong time to
> scribble 400MB of zero's and to then immediately fill it with
> satellite image. I know. Yawn. Too well. How about a cup of coffee? Has

If you're using a filesystem that allows sparse ("holey") files (such
as ext2fs), fseek() is the conventional way to create them. If you
seek past the end of a file, any full blocks between the end of the
file and the position you sought take up no disk space; the inode
stores a special number where the blocks' locations would normally go.
The zeroes are only written to disk on filesystems that don't allow
sparse files.

-- 
Aaron M. Ucko (amu@mit.edu) | For Geek Code, PGP public key, and other info,
finger amu@monk.mit.edu. | "Kids! Bringing about Armageddon can be dangerous.
Do not attempt it in your home." -- T. Pratchett & N. Gaiman, _Good Omens_