Page 2 of 2

Re: Some help on application dev

Posted: Tue Aug 25, 2020 8:07 am
by thewrongchristian
PeterX wrote:
nexos wrote:mmap is not in the standard C library. I believe it is a part of POSIX. It is a system call, and allows you to map files and memory, can figure out a virtual address to allocate or have you specify one, it can set readable and writeable attributes, plus more. It is a very good function, but it allocates in pages. On x86, that means 4K blocks of memory. It is generally better to use malloc, and when you need granular control over the memory or when mapping a file or when you know the virtual address you want to use, then mmap is your friend. Just note that is not portable. I believe Windows uses VirtualAlloc or MapViewOfFile instead.
Well, it's bad that it is not portable to Windows. But it sounds cool.
Does it mean, I can map a file to memory without reading the bytes in one-by-one? That would come very handy for my project.
IMO, mmap (even if not exported to user space) should be one of the first things you implement when writing an OS. I just don't see anything other demand paging as remotely practical these days, and mmap forms the basis of mapping the user process from the filesystem and can be used to implement a generic read/write interface (file buffers just become mmap windows into the file).

Portability wise, mmap can be written in terms of MapViewOfFile, they're analogous functions, probably even using a macro if needs be.

Re: Some help on application dev

Posted: Tue Aug 25, 2020 8:31 am
by nexos
Portability wise, mmap can be written in terms of MapViewOfFile, they're analogous functions, probably even using a macro if needs be.
Strictly POSIX speaking, yes, but most POSIX systems (Linux included) allow so called "anonymous mappings", which actually allocates memory. This is useful if you want strict control over the memory allocated (i. e. you want it to be read only). Looked at Microsoft Docs, and MapViewOfFile can't do this.

Re: Some help on application dev

Posted: Tue Aug 25, 2020 8:50 am
by PeterX
thewrongchristian wrote:IMO, mmap (even if not exported to user space) should be one of the first things you implement when writing an OS. I just don't see anything other demand paging as remotely practical these days, and mmap forms the basis of mapping the user process from the filesystem and can be used to implement a generic read/write interface (file buffers just become mmap windows into the file).
Yes, this is quite cool. I just wasn't aware at all, that it existed. :)

Greetings
Peter

Re: Some help on application dev

Posted: Sun Aug 30, 2020 8:49 am
by Gigasoft
Anonymous private mappings correspond to VirtualAlloc on Windows.

Mapping a file on Windows is a two step process. First, a "section" is created using CreateFileMapping, CreateFileMappingNuma or NtCreateSection. These functions can also create shared memory sections. A view is then instantiated with MapViewOfFile or NtMapViewOfSection (the latter allowing views to be inherited by a child process).

Page protections can then later be updated with VirtualProtect.

It should be noted that addresses and file offsets on Windows must be multiples of 64KB, so care must be taken when emulating mmap which only requires page alignment.