Mercurial > mplayer.hg
annotate osdep/shmem.c @ 34645:0bce77ccae9a
Fix compiling with osdep setenv
Since we have -Werror-implicit-function-declaration in CFLAGS,
we need a prototype for the osdep setenv() implementation.
Reported-by: redxii (in #mplayer IRC channel)
author | al |
---|---|
date | Sat, 18 Feb 2012 00:08:30 +0000 |
parents | 514016233368 |
children | cc658103f214 |
rev | line source |
---|---|
1 | 1 /* |
28744 | 2 * shared memory allocation |
3 * | |
4 * based on mpg123's xfermem.c by | |
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> | |
6 * | |
7 * This file is part of MPlayer. | |
8 * | |
9 * MPlayer is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * MPlayer is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License along | |
20 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
1 | 22 */ |
23 | |
16985 | 24 #include "config.h" |
3008 | 25 |
1 | 26 #include <stdio.h> |
27 #include <stdlib.h> | |
28 #include <string.h> | |
29 #include <unistd.h> | |
30 #include <errno.h> | |
31 #include <sys/types.h> | |
32 #include <sys/time.h> | |
33 #include <sys/uio.h> | |
33412
2a2e9b6551d8
configure: Convert HAVE_SYS_MMAN_H into a 0/1 definition.
diego
parents:
30554
diff
changeset
|
34 #if HAVE_SYS_MMAN_H |
1 | 35 #include <sys/mman.h> |
5298 | 36 #endif |
1 | 37 #include <sys/socket.h> |
38 #include <fcntl.h> | |
39 | |
16985 | 40 #include "mp_msg.h" |
30554 | 41 #include "shmem.h" |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
42 |
1 | 43 #ifdef AIX |
44 #include <sys/select.h> | |
45 #endif | |
46 | |
3008 | 47 #ifdef HAVE_SHM |
1 | 48 #include <sys/ipc.h> |
49 #include <sys/shm.h> | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
50 #endif |
1 | 51 |
52 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) | |
53 #define MAP_ANON MAP_ANONYMOUS | |
54 #endif | |
55 | |
56 static int shmem_type=0; | |
57 | |
58 void* shmem_alloc(int size){ | |
59 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
|
60 static int devzero = -1; |
1 | 61 while(1){ |
62 switch(shmem_type){ | |
63 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
|
64 #ifdef MAP_ANON |
1 | 65 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0); |
66 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
67 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p); |
1 | 68 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
|
69 #else |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
70 // 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
|
71 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
|
72 #endif |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
73 break; |
1 | 74 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
|
75 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break; |
1 | 76 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0); |
77 if(p==MAP_FAILED) break; // failed | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
78 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p); |
1 | 79 return p; |
3280 | 80 case 2: { // ========= shmget() ========== |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
81 #ifdef HAVE_SHM |
1 | 82 struct shmid_ds shmemds; |
83 int shmemid; | |
84 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
|
85 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
|
86 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno)); |
1 | 87 shmctl (shmemid, IPC_RMID, &shmemds); |
88 break; | |
89 } | |
90 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
|
91 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno)); |
1 | 92 if (shmdt(p) == -1) perror ("shmdt()"); |
93 break; | |
94 } | |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
95 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p); |
1 | 96 return p; |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
97 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
98 mp_msg(MSGT_OSDEP, MSGL_FATAL, "shmem: no SHM support was compiled in!\n"); |
26759
8eff880f638c
cosmetics: Remove useless parentheses from return statements.
diego
parents:
21855
diff
changeset
|
99 return NULL; |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
100 #endif |
3280 | 101 } |
1 | 102 default: |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
103 mp_msg(MSGT_OSDEP, MSGL_FATAL, |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
104 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size); |
1 | 105 return NULL; |
106 } | |
107 ++shmem_type; | |
108 } | |
109 } | |
110 | |
9914 | 111 void shmem_free(void* p,int size){ |
1 | 112 switch(shmem_type){ |
9914 | 113 case 0: |
114 case 1: | |
115 if(munmap(p,size)) { | |
116 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %d bytes: %s\n", | |
117 p,size,strerror(errno)); | |
118 } | |
119 break; | |
1 | 120 case 2: |
3084
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
121 #ifdef HAVE_SHM |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
122 if (shmdt(p) == -1) |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
123 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
|
124 strerror(errno)); |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
125 #else |
a3f947d5f847
converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents:
3008
diff
changeset
|
126 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
|
127 #endif |
1 | 128 break; |
129 } | |
130 } |