4

When I try to study some piece of code that deals with FPGA, I came across with munmap, mmap.

I go through the manual provided here. I am still not understanding the purpose of this function. What exactly this does?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Gilson PJ
  • 3,443
  • 3
  • 32
  • 53
  • 2
    I recommend reading the great answers to this question: http://stackoverflow.com/questions/258091/when-should-i-use-mmap-for-file-access. – bruceg Jan 05 '17 at 08:22

3 Answers3

6

mmap() is a system call, which helps in memory-mapped I/O operations. It allocates a memory region and maps that into the calling process virtual address space so as to enable the application to access the memory.

mmap() returns a pointer to the mapped area which can be used to access the memory.

Similarly, munmap() removes the mapping so no further access to the allocated memory remains legal.

These are lower level calls, behaviourally similar to what is offered by memory allocator functions like malloc() / free() on a higher level. However, this system call allow one to have fine grained control over the allocated region behaviour, like,

  • memory protection of the mapping (read, write, execute permission)
  • (approximate) location of the mapping (see MAP_FIXED flag)
  • the initial content of the mapped area (see MAP_UNINITIALIZED flag)

etc.

You can also refer to the wikipedia article if you think alternate wordings can help you.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

It maps a chunk of disk cache into process space so that the mapped file can be manipulated at a byte level instead of requiring the application to go through the VFS with read(), write(), et alia.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

The manual is clear:

mmap() creates a new mapping in the virtual address space of the calling process

In short, it maps a chunk of file/device memory/whatever into the process' space, so that it can directly access the content by just accessing the memory.

For example:

fd = open("xxx", O_RDONLY);
mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);

Will map the file's content to mem, reading mem is just like reading the content of the file xxx.

If the fd is some FPGA's device memory, then the mem becomes the content of the FPGA's content.

It is very convenient to use and efficient in some cases.

Mine
  • 4,123
  • 1
  • 25
  • 46