Getting FILE* from a memory map - Linux
This is a discussion on Getting FILE* from a memory map - Linux ; I have a memory map which essentially has the contents of a file. The
subsequent stage of my application uses an API which works on FILE*
(instead of memory). Is there any way I can do something like map the
...
-
Getting FILE* from a memory map
I have a memory map which essentially has the contents of a file. The
subsequent stage of my application uses an API which works on FILE*
(instead of memory). Is there any way I can do something like map the
memory to a file and get a FILE* for the mapped memory?
Regards
Nirnimesh
-
Re: Getting FILE* from a memory map
nirnimesh@gmail.com wrote:
> I have a memory map which essentially has the contents of a file. The
> subsequent stage of my application uses an API which works on FILE*
> (instead of memory). Is there any way I can do something like map the
> memory to a file and get a FILE* for the mapped memory?
I don't think there's any way to fake out the FILE * APIs without
modifying a bunch of system headers and libraries. And I don't know of
any way to associate an already-existing area of memory with a file.
You could create the area in memory as an mmap'd file initially. That
is, create an empty file to start, use ftruncate to establish the file
size, use mmap with the file descriptor to map the bytes into memory.
Now using the pointer that mmap returns, create your data structure.
(You might need to use msync next to flush bytes from memory into the
file; I'm not 100% sure.) It should now be possible to fopen the file
and pass the FILE * to your API.
GH
-
Re: Getting FILE* from a memory map
gil_hamilton@hotmail.com wrote:
> I don't think there's any way to fake out the FILE * APIs without
> modifying a bunch of system headers and libraries. And I don't know of
> any way to associate an already-existing area of memory with a file.
Following myself up, and for those that don't read
comp.os.linux.development.apps, there was a pointer posted there to a
facility called fmemopen, which I hadn't heard of before but which does
exactly what I said above didn't exist: it fakes the FILE * APIs using
in-memory operations. Cool. (How come I don't have man pages for
that? Oh well, Google is your friend.)
GH
-
Re: Getting FILE* from a memory map
gil_hamilton@hotmail.com wrote:
> Following myself up, and for those that don't read
> comp.os.linux.development.apps, there was a pointer posted there to a
> facility called fmemopen, which I hadn't heard of before but which does
> exactly what I said above didn't exist: it fakes the FILE * APIs using
> in-memory operations. Cool. (How come I don't have man pages for
> that? Oh well, Google is your friend.)
I too was surprised to find this esoteric function which does exactly
what I wanted. [and I actually got a bashing from the guy who replied
on comp.os.linux.development.apps]
Nirnimesh