如何映射内存中的_特定_区域?

发布于 2024-08-18 20:44:32 字数 310 浏览 3 评论 0原文

我有一个程序。我希望它能够在不同的运行中映射特定的内存区域。

  1. 我有该程序的源代码。 C/C++
  2. 我控制程序的编译方式。 gcc
  3. 我控制程序如何链接。 gcc
  4. 我控制程序如何运行(Linux)。

我只想拥有这个特定的内存区域,比如 0xabcdabcd 到 0xdeadbeef,我将其映射到特定文件。有什么办法可以保证这一点吗? (我必须以某种方式确保其他东西没有加载到这个特定区域)。

编辑:

我如何确保没有其他东西占用内存中的这个特定区域?

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 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深已缘浅 2024-08-25 20:44:32

您需要做两件事:

  1. 将起始地址指定为 mmap 的第一个参数。
  2. 包括 MAP_FIXED 标志。

对于起始地址,您需要确保它是页面大小的倍数。要获取页面大小,请使用调用 sysconf(_SC_PAGESIZE) (这是 Linux 上的适当调用,其他平台可能有所不同)。

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).

白色秋天 2024-08-25 20:44:32

将地址传递给 addr 中的映射。尝试将其控制在 8KB 范围内。不过,您可以尝试 mlock()

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

暮倦 2024-08-25 20:44:32

您无法确保没有其他东西占用该内存区域 - 先到先得。但是,由于您需要内存的特定部分,我猜测您有一个非常专业的环境,因此您只需确保您是第一个(使用启动脚本)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文