annotate osdep/mmap_anon.c @ 27078:7f6bec0700aa

sync w/r27107, patch by C¸«±dric Viou
author gpoirier
date Fri, 20 Jun 2008 20:11:57 +0000
parents 10f268ec0d87
children 5cfef41a1771
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21187
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
1 /**
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
2 * \file mmap_anon.c
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
3 * \brief Provide a compatible anonymous space mapping function
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
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
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
6
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
7 #include <stdio.h>
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
8 #include <unistd.h>
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
9 #include <fcntl.h>
21489
969de5b130ae fix include clash for mmap_anon on darwin ppc
nplourde
parents: 21253
diff changeset
10 #include <sys/mman.h>
21187
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
11
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
12 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
13 #define MAP_ANONYMOUS MAP_ANON
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
14 #endif
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
15
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
16 /*
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
17 * mmap() anonymous space, depending on the system's mmap() style. On systems
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
18 * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
19 * of the opened /dev/zero.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
20 */
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
21
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
22 /**
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
23 * \brief mmap() anonymous space, depending on the system's mmap() style. On systems
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
24 * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
25 * of the opened /dev/zero.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
26 *
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
27 * \param addr address to map at.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
28 * \param len number of bytes from addr to be mapped.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
29 * \param prot protections (region accessibility).
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
30 * \param flags specifies the type of the mapped object.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
31 * \param offset start mapping at byte offset.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
32 * \param zerofd
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
33 * \return a pointer to the mapped region upon successful completion, -1 otherwise.
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
34 */
21248
2de480457872 Remove useless fd parameter for mmap_anon
reimar
parents: 21187
diff changeset
35 void *mmap_anon(void *addr, size_t len, int prot, int flags, off_t offset)
21187
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
36 {
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
37 void *result;
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
38
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
39 /* From loader/ext.c:
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
40 * "Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap"
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
41 * Therefore we preserve the same behavior on all platforms, ie. no
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
42 * shared mappings of anon space (if the concepts are supported). */
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
43 #if defined(MAP_SHARED) && defined(MAP_PRIVATE)
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
44 flags = (flags & ~MAP_SHARED) | MAP_PRIVATE;
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
45 #endif /* defined(MAP_SHARED) && defined(MAP_PRIVATE) */
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
46
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
47 #ifdef MAP_ANONYMOUS
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
48 /* BSD-style anonymous mapping */
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
49 result = mmap(addr, len, prot, flags | MAP_ANONYMOUS, -1, offset);
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
50 #else
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
51 /* SysV-style anonymous mapping */
24174
10f268ec0d87 warning fix:
diego
parents: 21855
diff changeset
52 int fd;
21187
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
53 fd = open("/dev/zero", O_RDWR);
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
54 if(fd < 0){
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
55 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
56 return NULL;
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
57 }
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
58
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
59 result = mmap(addr, len, prot, flags, fd, offset);
21248
2de480457872 Remove useless fd parameter for mmap_anon
reimar
parents: 21187
diff changeset
60 close(fd);
21187
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
61 #endif /* MAP_ANONYMOUS */
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
62
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
63 return result;
d9cedf7b8069 added mmap_anon to osdep lib. Used in loader for now
nplourde
parents:
diff changeset
64 }