开发者

How do I mmap a _particular_ region in memory?

开发者 https://www.devze.com 2022-12-17 02:59 出处:网络
I have a program. I want it to be able to mmap a particular region of memory over different runs. I have the source code of the program. C/C++

I have a program. I want it to be able to mmap a particular region of memory over different runs.

  1. I have the source code of the program. C/C++
  2. I control how the program is compiled. gcc
  3. I control how the program is linked. gcc
  4. I control how the program is run (Linux).

I just want to have this particular region of memory, say 0xabcdabcd to 0开发者_JS百科xdeadbeef that I mmap to a particular file. Is there anyway to guarantee this? (I have to somehow make sure that other things aren't loaded into this particular region).

EDIT:

How do I make sure that nothing else takes this particular region in memory?


You need to do two things:

  1. Specify the starting address as the first argument to mmap.
  2. Include the MAP_FIXED flag.

For the starting address, you need to make sure it's a multiple of the pagesize. To get the pagesize, use the call sysconf(_SC_PAGESIZE) (that's the appropriate call on Linux, other platforms may be different).


Pass the address to map in addr. Try to get it on a 8KB boundary. You could try mlock() instead though.


You cannot make sure that nothing else takes that area of memory - first come, first served. However, as you need a particular part of the memory, I'm guessing that you have a pretty specialized environment, so you simply need to make sure that you are first (using start scripts)

0

精彩评论

暂无评论...
验证码 换一张
取 消