Is mmap() supported on Windows?

I have seen quite a lot of different implementations of posix things for windows in the standard library, is this also the case for mmap()? do i have to use windows-specific functions when targetting all “Modern”-ish versions of windows? I know that windows recently implemented mmap() but I am not sure if depending on recent features is the best course of action…

Thanks in advance!

I checked, it seems like the MAP type in the stdlib for Windows actually is void, which is funny because that type is not supported in that context for sysv calls, is there any alternative? any library I could use?

I’ll try to answer what I see as the relevant questions:

  • Does Windows provide a POSIX-compliant mmap implementation? No (if you could provide a link to what you mean by “I know that windows recently implemented mmap()” that might be helpful as I’m unaware of that)
  • Does Zig’s std.posix layer provide an mmap implementation when targeting Windows? No
  • Does Windows provide something sorta similar? Yes
  • Does Zig’s standard library provide an interface to that? No, not currently

So, it’s up to you how you want to handle this use case. A starting point would be to read the Windows docs about file mapping, compare it to POSIX mmap, and figure out if you can design a cross-platform API that satisfies your use case. The basic idea, though, would be to switch on the target and call into the relevant POSIX/Windows syscalls.

5 Likes
  • Apparently I was a victim of misinformation :pensive_face:
  • Interesting, this is a pretty interesting change, will this mean that there will be some new APIs for socket handling? That’s my main concern.
  • Yeah! I had used something like this on a previous project of mine, though it was on C, I guess I shall use these, does Zig’s stdlib expose those somewhere? Is there a library I can include? Or should I just write some externs myself?
  • Also extremely interesting! Thank you for this information! Also, this seems like it answers my latter question.

Thanks for the thoughtful response! I appreciate this kind of information a lot.

1 Like

See std.Io.net

If you mean libc functions, you can find those in std.c, but you need to link libc to use them. For POSIX functions, using std.posix is likely fine for now, or you could go through std.posix.system instead which is likely more future-proof from what I can tell. If you mean things like kernel32.dll functions on Windows, then you have options:

  • Hand-write the necessary extern fn definitions based on the Win32 documentation, a la the function definitions in the standard library
    • (this is my personal preference, as you have to understand the function in order to translate the C types into the relevant Zig types so it’s often a useful exercise)
  • If you want something comprehensive, zigwin32 provides a full set of bindings generated from Microsoft-provided metadata
  • Wade into the weeds and use ntdll functions, which have a higher chance of having bindings provided in std.os.windows.ntdll (but not every function is provided, intentionally)
    • (my tentative advice would probably be to stay away from this unless you’re intending on contributing to the standard library; it often requires reverse engineering as many ntdll APIs are undocumented)
2 Likes