How to use mmap to get a pointer to a memory?

885 views
Skip to first unread message

Robert Sandra

unread,
Oct 26, 2012, 4:02:01 PM10/26/12
to golan...@googlegroups.com
Hi all,

I am now playing with mmap in Go. I know that this function is used to map a file to the memory and then we can access the file through accessing that memory. But can we just use mmap to get a pointer to a memory and then use unsafe to manage the pointer? If yes, how should I set those arguments of mmap()?

Thanks very much.

Robert

bryanturley

unread,
Oct 26, 2012, 4:26:31 PM10/26/12
to golan...@googlegroups.com
syscall.Mmap() returns a slice, why not just use the slice?
You could use a int64 as an index and pretend it is a pointer.

d[p] instead of just p,   p is technically pointing to your value (just inside of d...)

You also don't need unsafe for arithmetic on a int64 index

If you are doing something very strange that needs a pointer it might be a better idea to use cgo to call mmap yourself.
That would be more inline with something that required the use of unsafe.

Robert Sandra

unread,
Oct 26, 2012, 4:40:28 PM10/26/12
to golan...@googlegroups.com
Thanks for reply.

On Friday, October 26, 2012 4:26:31 PM UTC-4, bryanturley wrote:
syscall.Mmap() returns a slice, why not just use the slice?
You could use a int64 as an index and pretend it is a pointer.
Yes, actually it is what I want to do. My question I do not know how to set the mmap's arguments, since the first argument need to be a file descriptor .. 

bryanturley

unread,
Oct 26, 2012, 4:54:08 PM10/26/12
to golan...@googlegroups.com

Yes, actually it is what I want to do. My question I do not know how to set the mmap's arguments, since the first argument need to be a file descriptor .. 

http://golang.org/pkg/os/#File.Fd  ?

Kind of seems like your not trying though... ;)

Robert Sandra

unread,
Oct 26, 2012, 5:03:03 PM10/26/12
to golan...@googlegroups.com
But I have no file to open.. I do not use mmap to map a file into the memory, I just want use mmap to allocate some memory.. Am I wrong?

bryanturley

unread,
Oct 26, 2012, 5:24:00 PM10/26/12
to golan...@googlegroups.com
I could be wrong but you might need a run or 2 through http://tour.golang.org/#1


What are you planning on using this memory for that you couldn't just use make?

slice_o_mem := make([]byte, 1024)   ?
slice_o_mem[10] = 3

You can't have a slice that has more than 2billion elements until a future version of go though.

The stuff is syscall is not portable,  you shouldn't use it unless you need it.
The only reason I can see to use the syscall.Mmap() version of mmap is to mmap files.


Ian Lance Taylor

unread,
Oct 26, 2012, 6:09:46 PM10/26/12
to Robert Sandra, golan...@googlegroups.com
On Fri, Oct 26, 2012 at 2:03 PM, Robert Sandra
<robert.s...@gmail.com> wrote:
> But I have no file to open.. I do not use mmap to map a file into the
> memory, I just want use mmap to allocate some memory.. Am I wrong?

Pass -1 as the file descriptor. Note that mmap is not portable but
that will work on most systems. See "man mmap" for details.

Ian

bryanturley

unread,
Oct 27, 2012, 2:41:15 AM10/27/12
to golan...@googlegroups.com
Pass -1 as the file descriptor.  Note that mmap is not portable but
that will work on most systems.  See "man mmap" for details.

Ian

But what benefit could it have over make()?
Since this mmap can't request an address (I understand that would be bad for go) like the c one does and
make([]byte, X) would just do the same thing unless you are mapping a file, why use mmap?

Address of the backing array is different?  Perhaps out of heap or not in the gc's vision?

I guess you could change the protection flags on it after writing something into it, if those calls are also in syscall

Seriously curious here.

Reply all
Reply to author
Forward
0 new messages