简体   繁体   English

如何映射内存中的_particular_区域?

[英]How do I mmap a _particular_ region in memory?

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++ 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). 我控制程序的运行方式(Linux)。

I just want to have this particular region of memory, say 0xabcdabcd to 0xdeadbeef that I mmap to a particular file. 我只想拥有这个特定的内存区域,比如说我映射到特定文件的0xabcdabcd到0xdeadbeef。 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. 将起始地址指定为mmap的第一个参数。
  2. Include the MAP_FIXED flag. 包括MAP_FIXED标志。

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). 要获取页面大小,请使用调用sysconf(_SC_PAGESIZE) (在Linux上是适当的调用,其他平台可能有所不同)。

Pass the address to map in addr . 将地址传递给addr map。 Try to get it on a 8KB boundary. 尝试在8KB的边界上获取它。 You could try mlock() instead though. 您可以尝试使用mlock()代替。

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) 但是,由于您需要内存的特定部分,所以我猜您有一个非常专业的环境,因此您只需要确保自己是第一名(使用开始脚本)即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM