Subject: Re: mapping shared memory at a fixed address
To: Bill Studenmund <wrstuden@netbsd.org>
From: Stephan Uphoff <ups@tree.com>
List: netbsd-users
Date: 02/08/2005 11:51:10
On Mon, 2005-02-07 at 13:39, Bill Studenmund wrote:
> On Fri, Feb 04, 2005 at 04:05:25PM -0800, Rahul Kulkarni wrote:
> > Wondering if anyone can give some pointers on how to
> > make mmap map a shared memory segment at a specified
> > address always, in different address spaces. MAP_FIXED
> > with a specified address works but is not gauranteed.
> > In other words, how can I "reserve" the same virtual
> > address in different address spaces to map to the same
> > segment.. 
> 
> You can't.
> 
> Why do you need to?
> 
> The problem is that if something else already is mapped at the address you 
> want, the new mmap won't work. The only way you could get around that was 
> if you mapped the area and then had the process fork into a lot of 
> children. However the children could not exec..

Mhhh .. maybe adding a zero filled section to the executable(s) at a
fixed address would work.
The executable could then unmap / remap the reserved address range.
If this would work it probably needs a linker script from hell ;-).

> 
> If you want to store (or pass) pointers to things in the area, just store 
> or pass relative pointers. Pass (intptr_t)ThePointer - 
> (intptr_t)MapAddress. Then in the recipient just add the stored or passed 
> integer to the local map address.

This is probably the best approach.
( Especially in term of maintainability) 
Good macros can make converting from/to relative pointers (And error
checking) really easy.


Stephan