![]() |
![]() |
![]() |
![]() |
![]() |
This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs. |
Map a memory region into a process's address space
#include <sys/mman.h>
void * mmap( void * addr,
size_t len,
int prot,
int flags,
int fildes,
off_t off );
void * mmap64( void * addr,
size_t len,
int prot,
int flags,
int fildes,
off64_t off );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The mmap() function maps a region within the object beginning at off and continuing for len into the caller's address space and returns the location.
Typically, you don't need to use addr; you can just pass NULL instead. If you set addr to a non-NULL value, whether the object is mapped depends on whether or not you set MAP_FIXED in flags:
There are two parts to the flags parameter. The first part is a type (masked by the MAP_TYPE bits), which you must specify as one of the following:
You can OR the following flags into the above type to further specify the mapping:
![]() |
Use MAP_FIXED with caution. Not all memory models support it. In general, you should assume that you can MAP_FIXED only at an address (and size) that a call to mmap() without MAP_FIXED returned. |
A memory area being mapped with MAP_FIXED is first unmapped by the system using the same memory area. See munmap() for details.
MAP_NOX64K and MAP_BELOW16M are used to further define the MAP_ANON allocated memory (useful on x86 only).
![]() |
You should use mmap_device_memory() instead of MAP_PHYS. |
![]() |
This flag was added in the QNX Neutrino Core OS 6.3.2. |
Using the mapping flags described above, a process can easily share memory between processes:
/* Map in a shared memory region */ fd = shm_open( "/datapoints", O_RDWR, 0777 ); addr = mmap( 0, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
To share memory with hardware such as video memory on an x86 platform:
/* Map in VGA display memory */
addr = mmap( 0,
65536,
PROT_READ|PROT_WRITE,
MAP_PHYS|MAP_SHARED,
NOFD,
0xa0000 );
To allocate a DMA buffer for a bus-mastering PCI network card:
/* Allocate a physically contiguous buffer */
addr = mmap( 0,
262144,
PROT_READ|PROT_WRITE|PROT_NOCACHE,
MAP_PHYS|MAP_ANON,
NOFD,
0 );
The address of the mapped-in object, or MAP_FAILED if an error occurred (errno is set).
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/mman.h>
int main(int argc, char *argv[])
{
int i;
unsigned char *addr, c;
/* Map BIOS ROM */
addr = mmap(0, 0x10000, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_PHYS, NOFD, 0xf0000);
if (addr == MAP_FAILED) {
fprintf(stderr, "mmap failed : %s\n",
strerror(errno));
return EXIT_FAILURE;
}
printf("Map addr is %p\n",(void*) addr);
for (i = 0; i < 3 * 80; ++i) {
c = *addr++;
if (c >= ' ' && c <= 0x7f)
putchar(c);
else
putchar('.');
}
return EXIT_SUCCESS;
}
mmap() is POSIX 1003.1 MF|SHM|TYM; mmap64() is Large-file support
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
mmap_device_io(), mmap_device_memory(), munmap()
![]() |
![]() |
![]() |
![]() |