aboutsummaryrefslogtreecommitdiffhomepage
path: root/osdep
diff options
context:
space:
mode:
authorGravatar nplourde <nplourde@b3059339-0415-0410-9bf9-f77b7e298cf2>2006-11-25 01:22:20 +0000
committerGravatar nplourde <nplourde@b3059339-0415-0410-9bf9-f77b7e298cf2>2006-11-25 01:22:20 +0000
commitac04dcd62588703bf03abf3a4c177af54a2475d0 (patch)
tree404310a1ea5cb27f488d016c792498f5b84de27b /osdep
parentb30a399c528917d620f153bfe478114c6d9f284b (diff)
added mmap_anon to osdep lib. Used in loader for now
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@21203 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'osdep')
-rw-r--r--osdep/Makefile1
-rw-r--r--osdep/mmap_anon.c67
-rw-r--r--osdep/mmap_anon.h8
3 files changed, 76 insertions, 0 deletions
diff --git a/osdep/Makefile b/osdep/Makefile
index a5ee59201e..abd38716b1 100644
--- a/osdep/Makefile
+++ b/osdep/Makefile
@@ -12,6 +12,7 @@ SRCS= shmem.c \
fseeko.c \
swab.c \
setenv.c \
+ mmap_anon.c \
# timer.c \
getch = getch2.c
diff --git a/osdep/mmap_anon.c b/osdep/mmap_anon.c
new file mode 100644
index 0000000000..b6d0b7cf35
--- /dev/null
+++ b/osdep/mmap_anon.c
@@ -0,0 +1,67 @@
+ /**
+ * \file mmap_anon.c
+ * \brief Provide a compatible anonymous space mapping function
+ */
+
+#include <sys/mman.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+
+/*
+ * mmap() anonymous space, depending on the system's mmap() style. On systems
+ * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
+ * of the opened /dev/zero.
+ */
+
+ /**
+ * \brief mmap() anonymous space, depending on the system's mmap() style. On systems
+ * that use the /dev/zero mapping idiom, zerofd will be set to the file descriptor
+ * of the opened /dev/zero.
+ *
+ * \param addr address to map at.
+ * \param len number of bytes from addr to be mapped.
+ * \param prot protections (region accessibility).
+ * \param flags specifies the type of the mapped object.
+ * \param offset start mapping at byte offset.
+ * \param zerofd
+ * \return a pointer to the mapped region upon successful completion, -1 otherwise.
+ */
+void *mmap_anon(void *addr, size_t len, int prot, int flags, int *zerofd, off_t offset)
+{
+ int fd;
+ void *result;
+
+ /* From loader/ext.c:
+ * "Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap"
+ * Therefore we preserve the same behavior on all platforms, ie. no
+ * shared mappings of anon space (if the concepts are supported). */
+#if defined(MAP_SHARED) && defined(MAP_PRIVATE)
+ flags = (flags & ~MAP_SHARED) | MAP_PRIVATE;
+#endif /* defined(MAP_SHARED) && defined(MAP_PRIVATE) */
+
+#ifdef MAP_ANONYMOUS
+ /* BSD-style anonymous mapping */
+ fd = -1;
+ result = mmap(addr, len, prot, flags | MAP_ANONYMOUS, -1, offset);
+#else
+ /* SysV-style anonymous mapping */
+ fd = open("/dev/zero", O_RDWR);
+ if(fd < 0){
+ perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: ");
+ return NULL;
+ }
+
+ result = mmap(addr, len, prot, flags, fd, offset);
+#endif /* MAP_ANONYMOUS */
+
+ if (zerofd)
+ *zerofd = fd;
+
+ return result;
+}
+
diff --git a/osdep/mmap_anon.h b/osdep/mmap_anon.h
new file mode 100644
index 0000000000..edb909bdc0
--- /dev/null
+++ b/osdep/mmap_anon.h
@@ -0,0 +1,8 @@
+#ifndef _OSDEP_MMAP_ANON_H_
+#define _OSDEP_MMAP_ANON_H_
+
+#include <sys/types.h>
+
+void *mmap_anon(void *, size_t, int, int, int *, off_t);
+
+#endif /* _OSDEP_MMAP_ANON_H_ */