简体   繁体   English

C ++ M将文件映射到内存,然后将文件描述符获取到该内存

[英]C++ MMap a file to memory and then get a file descriptor to that memory

I saw this post: system call to map memory to a file descriptor (inverse mmap)? 我看到了这篇文章: 将内存映射到文件描述符的系统调用(反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. 我想要一个想要在其接口中提供文件描述符的库,以便能够从我mmap文件的内存区域中读取文件描述符。

I need something to the effect of: 我需要一些效果:

  1. mmap the contents of a large file (5gb) to memory mmap一个大文件(5GB)到存储器中的内容

  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. 我用于mmap的原始文件描述符当然不会起作用,因为它是指磁盘上的文件。

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. 到目前为止,我能mmap文件到内存,但是我叫fmemopen ,但fileno当函数被调用时没有设置。 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. 我这样做的原因是我试图减少库处理文件中数据时的复制量。

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 to mmap . 如果此内存区域表示您已经mmap过的文件,那么您已经有了该文件的描述符,因为调用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. 如果以MAP_SHARED模式映射它,则通过文件描述符对文件的修改应自动显示在内存中,并且对内存的修改也应自动显示在文件中。 That's the whole point of mmap in MAP_SHARED mode. 这就是MAP_SHARED模式下mmap

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. 但是,查看手册页,可能必须在某些系统上调用msync才能将存储在内存中的内容显示在文件中。

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. 如果您有使用malloc或某些类似方法获取的某个内存区域,然后只希望该内存区域获取文件描述符,则不可能。

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

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