Mercurial > mplayer.hg
annotate osdep/mmap_anon.c @ 23035:e8afcfccf359
Always deallocate glyphs. Fixes a memory leak.
author | eugeni |
---|---|
date | Sat, 21 Apr 2007 10:56:28 +0000 |
parents | 936209c39ed1 |
children | 10f268ec0d87 |
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 int fd; | |
38 void *result; | |
39 | |
40 /* From loader/ext.c: | |
41 * "Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap" | |
42 * Therefore we preserve the same behavior on all platforms, ie. no | |
43 * shared mappings of anon space (if the concepts are supported). */ | |
44 #if defined(MAP_SHARED) && defined(MAP_PRIVATE) | |
45 flags = (flags & ~MAP_SHARED) | MAP_PRIVATE; | |
46 #endif /* defined(MAP_SHARED) && defined(MAP_PRIVATE) */ | |
47 | |
48 #ifdef MAP_ANONYMOUS | |
49 /* BSD-style anonymous mapping */ | |
50 result = mmap(addr, len, prot, flags | MAP_ANONYMOUS, -1, offset); | |
51 #else | |
52 /* SysV-style anonymous mapping */ | |
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 } |