views:

1082

answers:

3

Hi, I was playing around with memory-mapped files in C and was wondering if there is a way to replace the FILE * from fopen with a memory mapped file transparently.

Example:

FILE * fp = g_fopen(...);

//Program does things to this fp.

fclose();

But instead, is it possible to have FILE *fp = my_fopen(...)

Where my own function would open a file on disk mmap it, maybe change contents and then pass back the FILE * without the program seeing anything different except the new my_fopen() and my_fclose().

Is this possible to do without having to rewrite the way the operations are done in the program?

+4  A: 

The magic words are "library interposition". Here's a good tutorial: http://developers.sun.com/solaris/articles/lib_interposers.html

Dave
Great link, thank you
James
A: 

I don't understand. You can use read/write on a file you have mmaped, so your my_fopen would look like this

FILE * my_fopen()
{
    FILE * myfp
    myfp = fopen(...);
    mmap(fileno(myfp),...);
    return myfp;
}
shodanex
+2  A: 

The purpose of memory-mapped file IO is not to go through system calls for reads and writes, but rely on VM/buffer cache to do the work. If you hide the fact that the file has been memory mapped, what is the advantage here? How would you know at what address the file is mapped?

Nikolai N Fetissov
The perceived advantage for me was the ability to modify existing calls to do what I wish. Maybe inserting custom actions before a file is read or change the way it is read in without having to alter existing code.
James