
Question:
Is it possible to overload memory mapped file loader? In case of page fault, Linux kernel would not load the requested data from disk, but instead of it, user-defined function would be called, which would fill the memory page.
I would like to use this for memory mapping of huge compressed raster file. Used compression algorithm allows fast block decompression. User-defined loading function would decompress the file page by page on the fly.
The compressed files are read-only. User-space solution is preferred.
Answer1:<blockquote>
Is it possible to overload memory mapped file loader? In case of page fault, Linux kernel would not load the requested data from disk, but instead of it, user-defined function would be called, which would fill the memory page.
</blockquote>It is possible and some libraries do just that. See <a href="https://www.gnu.org/software/libsigsegv/" rel="nofollow">libsigsegv</a>.
Install a signal handler for SIGSEGV
with sigaction
using the following signature for your handler:
void sigsegv_handler(int, siginfo_t* si, void*) {
si->si_addr; // Memory location which caused the page fault.
// mmap the missing page and return here
// On error alternatives:
// * restore the previous SIGSEGV handler and return (the default one dumps core), or
// * abort() (dumps core), or
// * _exit(EXIT_FAILURE).
}