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 开发者_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 mmap
ed, 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.
精彩评论