Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there really no mremap in Darwin?

I'm trying to find out how to remap memory-mapped files on a Mac (when I want to expand the available space).

I see our friends in the Linux world have mremap but I can find no such function in the headers on my Mac. /Developer/SDKs/MacOSX10.6.sdk/usr/include/sys/mman.h has the following:

  • mmap
  • mprotect
  • msync
  • munlock
  • munmap
  • but no mremap

man mremap confirms my fears.

I'm currently having to munmap and mmmap if I want to resize the size of the mapped file, which involves invalidating all the loaded pages. There must be a better way. Surely?

I'm trying to write code that will work on Mac OS X and Linux. I could settle for a macro to use the best function in each case if I had to but I'd rather do it properly.

like image 758
Joe Avatar asked Aug 19 '10 11:08

Joe


2 Answers

If you need to shrink the map, just munmap the part at the end you want to remove.

If you need to enlarge the map, you can mmap the proper offset with MAP_FIXED to the addresses just above the old map, but you need to be careful that you don't map over something else that's already there...

The above text under strikeout is an awful idea; MAP_FIXED is fundamentally wrong unless you already know what's at the target address and want to atomically replace it. If you are trying to opportunistically map something new if the address range is free, you need to use mmap with a requested address but without MAP_FIXED and see if it succeeds and gives you the requested address; if it succeeds but with a different address you'll want to unmap the new mapping you just created and assume that allocation at the requested address was not possible.

like image 91
R.. GitHub STOP HELPING ICE Avatar answered Nov 12 '22 08:11

R.. GitHub STOP HELPING ICE


If you expand in large enough chunks (say, 64 MB, but it depends on how fast it grows) then the cost of invalidating the old map is negligible. As always, benchmark before assuming a problem.

like image 45
Zan Lynx Avatar answered Nov 12 '22 07:11

Zan Lynx