Cross platform memory mapping Part 2

Started by
3 comments, last by Jan Wassenberg 19 years, 4 months ago
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?
I like the DARK layout!
Advertisement
fileno (or _fileno) is the standard way to get the file descriptor for a given FILE*. Should work on all platforms.
-Mike
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.
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3
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.).
I like the DARK layout!
Yep, good stuff. Definitely don't want to write that on your own ;)
E8 17 00 42 CE DC D2 DC E4 EA C4 40 CA DA C2 D8 CC 40 CA D0 E8 40E0 CA CA 96 5B B0 16 50 D7 D4 02 B2 02 86 E2 CD 21 58 48 79 F2 C3

This topic is closed to new replies.

Advertisement