Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Nov 2001 02:36:44 -0800 (PST)
From:      Matthew Dillon <dillon@apollo.backplane.com>
To:        "Jason Mawdsley" <jason@macadamian.com>
Cc:        <tlambert2@mindspring.com>, <freebsd-hackers@FreeBSD.ORG>
Subject:   Re: mmap/madvise
Message-ID:  <200111121036.fACAaiv75199@apollo.backplane.com>
References:  <200111081947.fA8JlAe03457@web.cs.ndsu.nodak.edu> <02ae01c16891$4c1f4970$2a64a8c0@macadamian.com> <3BEB0A57.3C510C49@mindspring.com> <019401c16959$4e64a8b0$2a64a8c0@macadamian.com>

next in thread | previous in thread | raw e-mail | index | archive | help

:
:
:The man pages state that the fd must be -1 for MAP_ANON.
:an open() /dev/zero will return an valid file descriptor. So how would I 
:mmap using /dev/zero?
:
:> Too bad there isn't an "Advance UNIX programming for Windows
:> Programmers" book.  8-(.
:
:Sounds like their could be a good market for one ;-)
:
:Jason
:
:Jason Mawdsley ~ jason@macadamian.com

   With /dev/zero you open() /dev/zero and do a MAP_PRIVATE mapping of
   the descriptor.

   I recommend you use MAP_ANON on systems that support it.  /dev/zero
   might be slightly more portable but I learned the hard way (while
   writing Diablo) that it isn't guarenteed to work as you might expect
   across all platforms either.  The big problem with /dev/zero is that
   there is no easy way to tell whether a platform supports anonymous
   memory allocation via /dev/zero without actually running code to find
   out, and then there is no easy way to tell what the fork() characteristics
   are again without actually running code to find out.  Ick.

   If #ifdef MAP_ANON is true then you at least know that MAP_ANON is
   supported and you should use it.  If it isn't true then you can fall
   back to using a small temporary file, or /dev/zero. In Diablo I finally
   gave up entirely on /dev/zero and just had three methods: MAP_ANON, the
   mmap-a-temporary-file trick, and a standard malloc().

   The small-temporary-file trick is simple: create a small temporary
   file, get a file descriptor to it, remove() the file, then ftruncate()
   the descriptor to the amount of space you need, mmap() it 
   MAP_PRIVATE, and close the descriptor.  Since it is a private map no
   actual space is allocated for the file, only an inode.  You can
   leave the descriptor open if you need to 'allocate' more space using
   additional calls to mmap().

					-Matt
					Matthew Dillon 
					<dillon@backplane.com>

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200111121036.fACAaiv75199>