开发者

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

开发者 https://www.devze.com 2023-04-04 20:33 出处:网络
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 descript

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 开发者_JS百科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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消