如何映射内存中的_特定_区域?
我有一个程序。我希望它能够在不同的运行中映射特定的内存区域。
- 我有该程序的源代码。 C/C++
- 我控制程序的编译方式。 gcc
- 我控制程序如何链接。 gcc
- 我控制程序如何运行(Linux)。
我只想拥有这个特定的内存区域,比如 0xabcdabcd 到 0xdeadbeef,我将其映射到特定文件。有什么办法可以保证这一点吗? (我必须以某种方式确保其他东西没有加载到这个特定区域)。
编辑:
我如何确保没有其他东西占用内存中的这个特定区域?
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 control how the program is compiled. gcc
- I control how the program is linked. gcc
- I control how the program is run (Linux).
I just want to have this particular region of memory, say 0xabcdabcd to 0xdeadbeef 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要做两件事:
对于起始地址,您需要确保它是页面大小的倍数。要获取页面大小,请使用调用
sysconf(_SC_PAGESIZE)
(这是 Linux 上的适当调用,其他平台可能有所不同)。You need to do two things:
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).将地址传递给
addr
中的映射。尝试将其控制在 8KB 范围内。不过,您可以尝试mlock()
。Pass the address to map in
addr
. Try to get it on a 8KB boundary. You could trymlock()
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)