3 * \brief Provide a compatible anonymous space mapping function
12 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
13 #define MAP_ANONYMOUS MAP_ANON
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.
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.
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
)
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) */
48 /* BSD-style anonymous mapping */
49 result
= mmap(addr
, len
, prot
, flags
| MAP_ANONYMOUS
, -1, offset
);
51 /* SysV-style anonymous mapping */
53 fd
= open("/dev/zero", O_RDWR
);
55 perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
59 result
= mmap(addr
, len
, prot
, flags
, fd
, offset
);
61 #endif /* MAP_ANONYMOUS */