Mercurial > mplayer.hg
annotate linux/shmem.c @ 5089:d28504b410ad
tv -noaudio support by Charles Henrich
author | alex |
---|---|
date | Fri, 15 Mar 2002 16:10:06 +0000 |
parents | 542f7c228e38 |
children | 7bb8ded875db |
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 | |
3008 | 9 #include "../config.h" |
10 | |
1 | 11 #include <stdio.h> |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <unistd.h> | |
15 #include <errno.h> | |
16 #include <sys/types.h> | |
17 #include <sys/time.h> | |
18 #include <sys/uio.h> | |
19 #include <sys/mman.h> | |
20 #include <sys/socket.h> | |
21 #include <fcntl.h> | |
22 | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
23 #include "../mp_msg.h" |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
24 |
1 | 25 #ifdef AIX |
26 #include <sys/select.h> | |
27 #endif | |
28 | |
3008 | 29 #ifdef HAVE_SHM |
1 | 30 #include <sys/ipc.h> |
31 #include <sys/shm.h> | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
32 #endif |
1 | 33 |
34 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) | |
35 #define MAP_ANON MAP_ANONYMOUS | |
36 #endif | |
37 | |
38 static int shmem_type=0; | |
39 | |
40 void* shmem_alloc(int size){ | |
41 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
|
42 static int devzero = -1; |
1 | 43 while(1){ |
44 switch(shmem_type){ | |
45 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
|
46 #ifdef MAP_ANON |
1 | 47 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0); |
48 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
49 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p); |
1 | 50 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
|
51 #else |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
52 // 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
|
53 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
|
54 #endif |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
55 break; |
1 | 56 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
|
57 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break; |
1 | 58 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0); |
59 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
60 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p); |
1 | 61 return p; |
3280 | 62 case 2: { // ========= shmget() ========== |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
63 #ifdef HAVE_SHM |
1 | 64 struct shmid_ds shmemds; |
65 int shmemid; | |
66 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break; | |
67 if ((int)(p = shmat(shmemid, 0, 0)) == -1){ | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
68 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno)); |
1 | 69 shmctl (shmemid, IPC_RMID, &shmemds); |
70 break; | |
71 } | |
72 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
|
73 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno)); |
1 | 74 if (shmdt(p) == -1) perror ("shmdt()"); |
75 break; | |
76 } | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
77 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p); |
1 | 78 return p; |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
79 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
80 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
|
81 return(NULL); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
82 #endif |
3280 | 83 } |
1 | 84 default: |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
85 mp_msg(MSGT_OSDEP, MSGL_FATAL, |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
86 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size); |
1 | 87 return NULL; |
88 } | |
89 ++shmem_type; | |
90 } | |
91 } | |
92 | |
93 void shmem_free(void* p){ | |
94 switch(shmem_type){ | |
95 case 2: | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
96 #ifdef HAVE_SHM |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
97 if (shmdt(p) == -1) |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
98 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
|
99 strerror(errno)); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
100 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
101 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
|
102 #endif |
1 | 103 break; |
104 } | |
105 } |