C++将文件 MMap 到内存,然后获取该内存的文件描述符

发布于 2024-12-04 16:22:37 字数 601 浏览 1 评论 0原文

我看到这篇文章: 映射内存的系统调用到文件描述符(逆 mmap)?

我想要一个我拥有的库,它希望其接口中的文件描述符能够被赋予一个文件描述符,该文件描述符将从我所在的内存区域中读取mmap 编辑了文件。

我需要一些效果:

  1. mmap 将大文件(5gb)的内容写入内存

  2. 获取该内存区域的文件描述符

  3. 调用采用该文件描述符的库并执行某些操作

我使用的原始文件描述符mmap 当然不起作用,因为它引用的是磁盘上的文件。

到目前为止,我能够将文件mmap到内存,但是我调用了fmemopen,但在调用该函数时未设置fileno。有什么指点吗?

我这样做的原因是我试图减少库在处理文件中的数据时所做的复制量。

I saw this post: system call to map memory to a file descriptor (inverse mmap)?

I would like a library that I have that wants a file descriptor in its interface to be able to be given a file descriptor that will read from the region of memory where I've mmaped the file.

I need something to the effect of:

  1. mmap the contents of a large file (5gb) to memory

  2. Get a file descriptor to that memory region

  3. Call a library that takes that file descriptor and do something

The original file descriptor I used for mmap won't work of course, since that refers to the file on disk.

So far I was able to mmap the file to the memory, however I called fmemopen, but fileno is not set when that function is called. Any pointers?

The reason that I am doing this is I'm trying to reduce the amount of copying the library does as it processes the data in the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

祁梦 2024-12-11 16:22:37

从某种意义上说,你所问的问题似乎微不足道。从另一种意义上来说,这是不可能的。

如果此内存区域代表您已经 mmap 编辑的文件,那么您已经拥有它的文件描述符,因为您需要该文件描述符来调用 mmap。如果您以 MAP_SHARED 模式映射它,则通过文件描述符对文件的修改应自动显示在内存中,对内存的修改也应自动显示在文件中。这就是 MAP_SHARED 模式下 mmap 的全部意义。

不过,查看手册页,您可能必须在某些系统上调用 msync 才能使您对内存所做的操作显示在文件中。

如果您有使用 malloc 或类似方法获取的某些内存区域,然后只想让该内存区域获取文件描述符,这是不可能的。

In one sense, what you're asking seems trivial. In another sense, it's impossible.

If this region of memory represents a file you've already mmaped, then you already have a file descriptor for it since you needed that file descriptor for the call to mmap. If you mapped it in MAP_SHARED mode, modifications to the file through the file descriptor should show up in memory automatically and modifications to memory should also show up in the file automatically. That's the whole point of mmap in MAP_SHARED mode.

Though, looking at the man page, it's possible you may have to call msync on some systems in order for the stuff you did to memory to show up in the file.

If you have some region of memory that you acquired using malloc or some similar means, and then just want this region of memory to acquire a file descriptor, that isn't possible.

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