C++将文件 MMap 到内存,然后获取该内存的文件描述符
我看到这篇文章: 映射内存的系统调用到文件描述符(逆 mmap)?
我想要一个我拥有的库,它希望其接口中的文件描述符能够被赋予一个文件描述符,该文件描述符将从我所在的内存区域中读取mmap
编辑了文件。
我需要一些效果:
mmap
将大文件(5gb)的内容写入内存获取该内存区域的文件描述符
调用采用该文件描述符的库并执行某些操作
我使用的原始文件描述符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 mmap
ed the file.
I need something to the effect of:
mmap
the contents of a large file (5gb) to memoryGet a file descriptor to that memory region
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从某种意义上说,你所问的问题似乎微不足道。从另一种意义上来说,这是不可能的。
如果此内存区域代表您已经
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
mmap
ed, then you already have a file descriptor for it since you needed that file descriptor for the call tommap
. If you mapped it inMAP_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 ofmmap
inMAP_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.