[vorbis-dev] ogg123: shared memory by mmap()

Christian Weisgerber naddy at mips.inka.de
Sat Mar 17 09:01:34 PST 2001



The patch below adds:

- acinclude.m4:  A new macro A_FUNC_SMMAP to check that sharing pages
  through mmap() works.  This is taken from Joerg Schilling's star.
- configure.in:  A_FUNC_SMMAP
- ogg123/buffer.c:  If we have a working mmap(), use it to create
  a region of shared memory instead of using System V IPC.

Works on BSD.  Should also work on SVR4 and offspring (Solaris),
and Linux.

--- acinclude.m4.orig	Wed Feb 28 03:36:50 2001
+++ acinclude.m4	Sat Mar 17 17:39:58 2001
@@ -300,3 +300,65 @@
   AC_SUBST(AO_LIBS)
   rm -f conf.aotest
 ])
+
+dnl Shamelessly stolen from Joerg Schilling's star.
+dnl Copyright 1998 J. Schilling
+
+dnl Checks if mmap() works to get shared memory
+dnl Defines HAVE_SMMAP on success.
+AC_DEFUN(AC_FUNC_SMMAP,
+[AC_CACHE_CHECK([if mmap works to get shared memory], ac_cv_func_smmap,
+                [AC_TRY_RUN([
+#include <sys/types.h>
+#include <sys/mman.h>
+
+char *
+mkshare()
+{
+        int     size = 8192;
+        int     f;
+        char    *addr;
+
+        if ((f = open("/dev/zero", 2)) < 0)
+                exit(1);
+        addr = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);
+        if (addr == (char *)-1)
+                exit(1);
+        close(f);
+
+        return (addr);
+}
+
+main()
+{
+        char    *addr;
+        
+        addr = mkshare(8192);
+        *addr = 'I';
+
+        switch (fork()) {
+
+        case -1:
+                printf("help\n"); exit(1);
+
+        case 0: /* child */
+                *addr = 'N';
+                _exit(0);
+                break;
+        default: /* parent */
+                wait(0);
+                sleep(1);
+                break;
+        }
+
+        if (*addr != 'N')
+                exit(1);
+        exit(0);
+}
+], 
+                [ac_cv_func_smmap=yes],
+                [ac_cv_func_smmap=no],
+                [ac_cv_func_smmap=no])])
+if test $ac_cv_func_smmap = yes; then
+  AC_DEFINE(HAVE_SMMAP)
+fi])
--- configure.in.orig	Mon Feb 26 06:56:46 2001
+++ configure.in	Sat Mar 17 17:39:45 2001
@@ -67,7 +67,7 @@
 dnl Check for library functions
 dnl --------------------------------------------------
 
-dnl none
+AC_FUNC_SMMAP
 
 dnl --------------------------------------------------
 dnl Work around FHS stupidity
--- ogg123/buffer.c.old	Sat Mar 17 15:37:07 2001
+++ ogg123/buffer.c	Sat Mar 17 17:40:16 2001
@@ -6,16 +6,16 @@
  */
 
 #include <sys/types.h>
+#if HAVE_SMMAP
+#include <sys/mman.h>
+#else
 #include <sys/ipc.h>
 #include <sys/shm.h>
+#endif
 #include <sys/time.h>
 #include <unistd.h> /* for fork and pipe*/
 #include <fcntl.h>
 
-#ifndef DARWIN
-#include <malloc.h>
-#endif
-
 #include "ogg123.h"
 #include "buffer.h"
 
@@ -72,6 +72,22 @@
   int childpid;
   buf_t *buf;
 
+#if HAVE_SMMAP
+  int fd;
+
+  if ((fd = open("/dev/zero", O_RDWR)) < 0)
+    {
+      perror ("cannot open /dev/zero");
+      exit (1);
+    }
+  if ((buf = (buf_t *) mmap (0, sizeof(buf_t) + sizeof (chunk_t) * (size - 1),
+                             PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0)) < 0)
+    {
+      perror("mmap");
+      exit(1);
+    }
+  close(fd);
+#else
   /* Get the shared memory segment. */
   int shmid = shmget (IPC_PRIVATE,
                           sizeof(buf_t) + sizeof (chunk_t) * (size - 1),
@@ -94,6 +110,7 @@
 
   /* Remove segment after last process detaches it or terminates. */
   shmctl(shmid, IPC_RMID, 0);
+#endif /* HAVE_SMMAP */
 
   buffer_init (buf, size);
   

-- 
Christian "naddy" Weisgerber                          naddy at mips.inka.de

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'vorbis-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.




More information about the Vorbis-dev mailing list