OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 04, 2021 10:21 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Some help on application dev
PostPosted: Tue Aug 25, 2020 8:07 am 
Offline
Member
Member

Joined: Tue Apr 03, 2018 2:44 am
Posts: 156
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.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Tue Aug 25, 2020 8:31 am 
Offline
Member
Member
User avatar

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 639
Quote:
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.

_________________
Currently working on the Nexware project, an attempt to make a less bloated version of GNU. All repos for it can be found at https://github.com/Nexware-Project.


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Tue Aug 25, 2020 8:50 am 
Offline
Member
Member
User avatar

Joined: Fri Nov 22, 2019 5:46 am
Posts: 576
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

_________________
The "fanboy" ;)
https://wiki.osdev.org/User:PeterX


Top
 Profile  
 
 Post subject: Re: Some help on application dev
PostPosted: Sun Aug 30, 2020 8:49 am 
Offline
Member
Member

Joined: Sat Nov 21, 2009 5:11 pm
Posts: 739
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group