Hi,
The explanation is very clear. Thanks a lot.
Regards, Hank
-----Original Message----- From: Jesse McGrew [mailto:jmcgrew@hansprestige.com] Sent: Thursday, November 27, 2003 3:12 PM To: mad-dev@lists.mars.org Subject: Re: [mad-dev] A question for mmap()
On Thu, 27 Nov 2003 14:57:11 +0800, hank.jan@billionton.com.tw wrote:
I have a question about mmap(), hope someone can clarify for me. As I know, mmap() maps the whole file into memory, so OS can access the source file directly from memory. However there should be a restriction to memory size on the embedded system.
In my test, I compile madplay with mmap supported, and then play a mp3 file of 43MB, and it plays well. But my question is I only get 32MB SDRAM on my development board. It seems that the memory size is not large enough to store all the data. How does mmap() work?
mmap() maps pages of memory to correspond to the file, but it doesn't actually load the file into memory. The file is loaded on demand. When you access one of the mapped pages, the OS loads the correct part of the file into memory. The effect is similar to virtual memory, where pages are swapped to and from the disk when they're needed.
Thus, if you're using a 32 bit CPU, you may be able to map up to 4 GB with mmap(), even if you only have 32 MB of RAM. Adding more RAM will allow you to keep more of the file in memory at once, so random access will be faster, but it doesn't affect the size of the file you can map.