Mercurial > mplayer.hg
annotate osdep/mmap_anon.c @ 25899:9d4121fa508a
Split osd related stuff from mp_core.h into new header file mp_osd.h.
author | ulion |
---|---|
date | Wed, 30 Jan 2008 07:21:05 +0000 |
parents | 10f268ec0d87 |
children | 5cfef41a1771 |
rev | line source |
---|---|
21187 | 1 /** |
2 * \file mmap_anon.c | |
3 * \brief Provide a compatible anonymous space mapping function | |
4 */ | |
21253
f2ddea0632d4
Disable when HAVE_SYS_MMAN_H is not defined, since it can not be compiled then.
reimar
parents:
21248
diff
changeset
|
5 #include "config.h" |
21187 | 6 |
7 #include <stdio.h> | |
8 #include <unistd.h> | |
9 #include <fcntl.h> | |
21489 | 10 #include <sys/mman.h> |
21187 | 11 |
12 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) | |
13 #define MAP_ANONYMOUS MAP_ANON | |
14 #endif | |
15 | |
16 /* | |
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. | |
20 */ | |
21 | |
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. | |
26 * | |
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. | |
34 */ | |
21248 | 35 void *mmap_anon(void *addr, size_t len, int prot, int flags, off_t offset) |
21187 | 36 { |
37 void *result; | |
38 | |
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) */ | |
46 | |
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 */ | |
24174 | 52 int fd; |
21187 | 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; | |
57 } | |
58 | |
59 result = mmap(addr, len, prot, flags, fd, offset); | |
21248 | 60 close(fd); |
21187 | 61 #endif /* MAP_ANONYMOUS */ |
62 | |
63 return result; | |
64 } |