Some help on application dev

Programming, for all ages and all languages.
thewrongchristian
Member
Member
Posts: 425
Joined: Tue Apr 03, 2018 2:44 am

Re: Some help on application dev

Post 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.
nexos
Member
Member
Posts: 1079
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Some help on application dev

Post 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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: Some help on application dev

Post 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
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Some help on application dev

Post 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.
Post Reply