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.