| Questions on use of mmap() The first parameter to mmap is a pointer you provide. If it is zero/NULL, mmap gets its own area of memory to use and you gain access to this memory sort of like using malloc() except it comes already filled in with the data from your file (or all zeros if you map the file /dev/zero). If you get your own storage (say via malloc() ), you can use that and mmap will try to map your file there - just be aware that memory maps must start on full page boundaries and will extend to a full page boundary. I think on Linux pages are always 4096 (4K) bytes, but on Solaris you can find 8192 (8K) page sizes. If mmap cannot fit your mapped data into the memory area your pointer points to, it disregards it and finds its own free area within the process address space that is large enough to hold all the data being mapped.
If mmap() obtains the chunk of memory for you, it is freed when you munmap it and you will get a memory fault if you try to access it after it is unmapped. If you provided the memory for the mapping, it is still available for you to use after you munmap it.
I hope this helps. |