Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / osdep / mmap_anon.c
blobf692e2b341ce87cab7afaa6c8bff9239c2876e12
1 /**
2 * \file mmap_anon.c
3 * \brief Provide a compatible anonymous space mapping function
4 */
5 #include "config.h"
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <sys/mman.h>
12 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
13 #define MAP_ANONYMOUS MAP_ANON
14 #endif
17 * mmap() anonymous space, depending on the system's mmap() style. On systems
18 * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
19 * of the opened /dev/zero.
22 /**
23 * \brief mmap() anonymous space, depending on the system's mmap() style. On systems
24 * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
25 * of the opened /dev/zero.
27 * \param addr address to map at.
28 * \param len number of bytes from addr to be mapped.
29 * \param prot protections (region accessibility).
30 * \param flags specifies the type of the mapped object.
31 * \param offset start mapping at byte offset.
32 * \param zerofd
33 * \return a pointer to the mapped region upon successful completion, -1 otherwise.
35 void *mmap_anon(void *addr, size_t len, int prot, int flags, off_t offset)
37 void *result;
39 /* From loader/ext.c:
40 * "Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap"
41 * Therefore we preserve the same behavior on all platforms, ie. no
42 * shared mappings of anon space (if the concepts are supported). */
43 #if defined(MAP_SHARED) && defined(MAP_PRIVATE)
44 flags = (flags & ~MAP_SHARED) | MAP_PRIVATE;
45 #endif /* defined(MAP_SHARED) && defined(MAP_PRIVATE) */
47 #ifdef MAP_ANONYMOUS
48 /* BSD-style anonymous mapping */
49 result = mmap(addr, len, prot, flags | MAP_ANONYMOUS, -1, offset);
50 #else
51 /* SysV-style anonymous mapping */
52 int fd;
53 fd = open("/dev/zero", O_RDWR);
54 if(fd < 0){
55 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
56 return NULL;
59 result = mmap(addr, len, prot, flags, fd, offset);
60 close(fd);
61 #endif /* MAP_ANONYMOUS */
63 return result;