Mercurial > mplayer.hg
annotate osdep/shmem.c @ 18762:369074e0eb7b
support for audio stream switching
author | nicodvb |
---|---|
date | Tue, 20 Jun 2006 19:57:16 +0000 |
parents | 08cac43f1e38 |
children | 936209c39ed1 |
rev | line source |
---|---|
1 | 1 /* |
2 * shmem.c - Shared memory allocation | |
3 * | |
4 * based on mpg123's xfermem.c by | |
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> | |
6 * Sun Apr 6 02:26:26 MET DST 1997 | |
7 */ | |
8 | |
16985 | 9 #include "config.h" |
3008 | 10 |
10197 | 11 #if defined(USE_STREAM_CACHE) && !defined(WIN32) |
9797 | 12 |
1 | 13 #include <stdio.h> |
14 #include <stdlib.h> | |
15 #include <string.h> | |
16 #include <unistd.h> | |
17 #include <errno.h> | |
18 #include <sys/types.h> | |
19 #include <sys/time.h> | |
20 #include <sys/uio.h> | |
5298 | 21 #ifdef HAVE_SYS_MMAN_H |
1 | 22 #include <sys/mman.h> |
13612 | 23 #elif defined(__BEOS__) |
24 #include <mman.h> | |
5298 | 25 #endif |
1 | 26 #include <sys/socket.h> |
27 #include <fcntl.h> | |
28 | |
16985 | 29 #include "mp_msg.h" |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
30 |
1 | 31 #ifdef AIX |
32 #include <sys/select.h> | |
33 #endif | |
34 | |
3008 | 35 #ifdef HAVE_SHM |
1 | 36 #include <sys/ipc.h> |
37 #include <sys/shm.h> | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
38 #endif |
1 | 39 |
40 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) | |
41 #define MAP_ANON MAP_ANONYMOUS | |
42 #endif | |
43 | |
44 static int shmem_type=0; | |
45 | |
46 void* shmem_alloc(int size){ | |
47 void* p; | |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
48 static int devzero = -1; |
1 | 49 while(1){ |
50 switch(shmem_type){ | |
51 case 0: // ========= MAP_ANON|MAP_SHARED ========== | |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
52 #ifdef MAP_ANON |
1 | 53 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0); |
54 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
55 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p); |
1 | 56 return p; |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
57 #else |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
58 // system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
59 mp_dbg(MSGT_OSDEP, MSGL_DBG3, "shmem: using mmap anon failed\n"); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
60 #endif |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
61 break; |
1 | 62 case 1: // ========= MAP_SHARED + /dev/zero ========== |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
63 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break; |
1 | 64 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0); |
65 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
66 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p); |
1 | 67 return p; |
3280 | 68 case 2: { // ========= shmget() ========== |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
69 #ifdef HAVE_SHM |
1 | 70 struct shmid_ds shmemds; |
71 int shmemid; | |
72 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break; | |
6162
0e56fbf9039a
applied 64bit patch from Ulrich Hecht <uli at suse dot de>
alex
parents:
5298
diff
changeset
|
73 if ((p = shmat(shmemid, 0, 0)) == (void *)-1){ |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
74 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno)); |
1 | 75 shmctl (shmemid, IPC_RMID, &shmemds); |
76 break; | |
77 } | |
78 if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) { | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
79 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno)); |
1 | 80 if (shmdt(p) == -1) perror ("shmdt()"); |
81 break; | |
82 } | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
83 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p); |
1 | 84 return p; |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
85 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
86 mp_msg(MSGT_OSDEP, MSGL_FATAL, "shmem: no SHM support was compiled in!\n"); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
87 return(NULL); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
88 #endif |
3280 | 89 } |
1 | 90 default: |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
91 mp_msg(MSGT_OSDEP, MSGL_FATAL, |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
92 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size); |
1 | 93 return NULL; |
94 } | |
95 ++shmem_type; | |
96 } | |
97 } | |
98 | |
9914 | 99 void shmem_free(void* p,int size){ |
1 | 100 switch(shmem_type){ |
9914 | 101 case 0: |
102 case 1: | |
103 if(munmap(p,size)) { | |
104 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %d bytes: %s\n", | |
105 p,size,strerror(errno)); | |
106 } | |
107 break; | |
1 | 108 case 2: |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
109 #ifdef HAVE_SHM |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
110 if (shmdt(p) == -1) |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
111 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: shmdt() failed: %s\n", |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
112 strerror(errno)); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
113 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
114 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: no SHM support was compiled in!\n"); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
115 #endif |
1 | 116 break; |
117 } | |
118 } | |
9797 | 119 |
120 #endif |