• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Using mmap to extend an existing mapping?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using mmap to extend an existing mapping?


  • Subject: Re: Using mmap to extend an existing mapping?
  • From: Terry Lambert <email@hidden>
  • Date: Thu, 08 Oct 2009 11:14:14 -0700

On Oct 8, 2009, at 9:08 AM, Jens Alfke <email@hidden> wrote:
On Oct 7, 2009, at 11:12 AM, Terry Lambert wrote:
Typically you are expected to keep your mapped regions away from stack and heap allocations, which means either limiting yourself to a fraction of the address space and using a generally unused region, or by establishing your mapping followed immediately by an adjacent mapping of zero-fill pages that you can later choose to map over top of

Thanks! This sounds like it requires knowing ahead of time where to put the mappings, and specifying fixed addresses. But how do you know?

You are going down the wrong path; you just need to know the location of a hole large enough for your maximum eventual region size early in the life of your program.


Hardcoding an address makes assumptions about how the OS lays out process address spaces (and this is code I'd like to make cross- platform), and runs the risk of colliding with other libraries that are making the same assumptions and trying to use the same addresses.

Don't hard-code the address if the data contents you plan to put there don't require it through position dependence.


But I don't know of any API that would let me figure this out dynamically, i.e. by iterating over the already-allocated address ranges.

There are Mach APIs that can do this, but I would discourage their use, particularly in multithreaded programs. Preflighting in general is a bad idea (key Yoda: "Do or not do; there is no try"), and by the time you go to use your preflight answer, the answer may have changed on you because of the actions of another thread.



Let's say my file is currently 100k but I want to allow for growth up to 10MB. If I call mmap with a range of (0,10MB) what happens? Does the kernel only allocate 100k, or do I get a 10MB region? If the latter, then if I append to the file does the new data appear in the region (assuming I used MAP_SHARED)? That would get me the behavior I want...

If you map a file, you get however many pages are in the file, up to 10MB; if you map anonymous memory, you get 10MB always.


So ask for 10MB of anonymous memory at any location, get it, and then map the file on top of it using MAP_FIXED and the base address you got back when you asked for the 10MB to make sure it maps on top of your 10MB reservation. However much of the file exists will replace the initial pages in the 10MB region, leaving the rest of the 10MB region reserved for future expansion of the file. Your disadvantage (one mapping overwrites another) has now become your advantage. Because you have the 10MB mapping already, the system won't break up your 10MB address region on you with its own mappings and allocations: it's effectively reserved exclusively for your use.

(Apologies if these are obvious newbie questions. Feel free to point me to in-depth coverage of mmap; I've done some googling but not exhaustively.)

They're pretty basic, but not entirely obvious if you are used to owning the entire address space and having stacks grow down in it and heaps grow up and the system not putting anything in the middle because you don't have thread stacks getting allocated and you don't have fragmentation of the address space from allocations and deallocations because you don't have a garbage collector and you don't dynamically load/unload code and data and you either don't use object oriented languages, or all your objects are globally scoped, etc.. Modern address spaces are a lot more complicated than they were in the 1970s and early 80s. 8-).


-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Using mmap to extend an existing mapping?
      • From: Jens Alfke <email@hidden>
References: 
 >Using mmap to extend an existing mapping? (From: Jens Alfke <email@hidden>)
 >Re: Using mmap to extend an existing mapping? (From: Terry Lambert <email@hidden>)
 >Re: Using mmap to extend an existing mapping? (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: Using mmap to extend an existing mapping?
  • Next by Date: Re: Using mmap to extend an existing mapping?
  • Previous by thread: Re: Using mmap to extend an existing mapping?
  • Next by thread: Re: Using mmap to extend an existing mapping?
  • Index(es):
    • Date
    • Thread