Cross platform memory mapping Part 2
Back in this thread (which I would be replying to instead of creating a new topic but that thread's retired) I asked if there was a cross platform memory mapping library and someone posted some handy code to implement mmap() in Windows.
The thing is that code requires an integer file descriptor, which I can't figure out how to get. If I wanted to use the *nix function open() to open the file I could, but I can't since this must also work in Windows.
Is there a way to get a file descriptor from either a stdio FILE or a fstream object? Or will I still have to resort to a bunch of #ifdef's to use open() when compiling on *nix and some other Win32 function on Windows?
fileno (or _fileno) is the standard way to get the file descriptor for a given FILE*. Should work on all platforms.
hmm, the point of the mmap() wrapper is portability, right? You want to use the same POSIX routines in your main code, and let the emulation code do gyrations behind the scenes.
So why not emulate open() on Windows, just as with mmap()? All you have to do is call _open; depending on what you need to open, you can special-case /dev/tty and aio.
Anon Mike: ah, thanks! Didn't know fileno was portable, I always thought it (in the form of _fileno) was MS-specific. SUSv3 set me straight :)
Stdio-file handling + mmap is indeed possible.
So why not emulate open() on Windows, just as with mmap()? All you have to do is call _open; depending on what you need to open, you can special-case /dev/tty and aio.
Anon Mike: ah, thanks! Didn't know fileno was portable, I always thought it (in the form of _fileno) was MS-specific. SUSv3 set me straight :)
Stdio-file handling + mmap is indeed possible.
I discovered yesterday that Microsoft provides _open(), _close(), _read(), just about all the POSIX stuff I'd need. I just used some #define's to convert open() to _open(), etc. I haven't had a chance to test everything yet, but it compiles (I did all that before I heard about fileno(). Oh well.).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement