From owner-freebsd-hackers Mon Nov 12 2:36:53 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from apollo.backplane.com (apollo.backplane.com [216.240.41.2]) by hub.freebsd.org (Postfix) with ESMTP id 06AA637B417 for ; Mon, 12 Nov 2001 02:36:51 -0800 (PST) Received: (from dillon@localhost) by apollo.backplane.com (8.11.6/8.9.1) id fACAaiv75199; Mon, 12 Nov 2001 02:36:44 -0800 (PST) (envelope-from dillon) Date: Mon, 12 Nov 2001 02:36:44 -0800 (PST) From: Matthew Dillon Message-Id: <200111121036.fACAaiv75199@apollo.backplane.com> To: "Jason Mawdsley" Cc: , Subject: Re: mmap/madvise References: <200111081947.fA8JlAe03457@web.cs.ndsu.nodak.edu> <02ae01c16891$4c1f4970$2a64a8c0@macadamian.com> <3BEB0A57.3C510C49@mindspring.com> <019401c16959$4e64a8b0$2a64a8c0@macadamian.com> Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG : : :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 To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message