annotate osdep/shmem.c @ 36395:2b9bc3c2933d

Remove some macros and switch to libavutil equivalents.
author reimar
date Sat, 26 Oct 2013 09:36:23 +0000
parents d5e1a1ad86b8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 /*
28744
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
2 * shared memory allocation
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
3 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
4 * based on mpg123's xfermem.c by
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
6 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
7 * This file is part of MPlayer.
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
8 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
9 * MPlayer is free software; you can redistribute it and/or modify
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
10 * it under the terms of the GNU General Public License as published by
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
12 * (at your option) any later version.
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
13 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
14 * MPlayer is distributed in the hope that it will be useful,
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
17 * GNU General Public License for more details.
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
18 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
19 * You should have received a copy of the GNU General Public License along
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
5cfef41a1771 Add standard license headers to files.
diego
parents: 26759
diff changeset
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
22 */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13612
diff changeset
24 #include "config.h"
3008
8687599be101 #ifdef HAVE_SHM
alex
parents: 1347
diff changeset
25
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26 #include <stdio.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27 #include <stdlib.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
28 #include <string.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29 #include <unistd.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30 #include <errno.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 #include <sys/types.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32 #include <sys/time.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35 #include <sys/mman.h>
5298
7bb8ded875db check for mman.h
arpi
parents: 3280
diff changeset
36 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37 #include <sys/socket.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 #include <fcntl.h>
34711
d5e1a1ad86b8 Correct r34798.
ib
parents: 34710
diff changeset
39 #include <inttypes.h>
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
40
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13612
diff changeset
41 #include "mp_msg.h"
30554
321e9ea69b9f #include corresponding .h files in .c files.
diego
parents: 28744
diff changeset
42 #include "shmem.h"
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
43
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
44 #ifdef AIX
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 #include <sys/select.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
46 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47
3008
8687599be101 #ifdef HAVE_SHM
alex
parents: 1347
diff changeset
48 #ifdef HAVE_SHM
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 #include <sys/ipc.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
50 #include <sys/shm.h>
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
51 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 #define MAP_ANON MAP_ANONYMOUS
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57 static int shmem_type=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
59 void* shmem_alloc(int64_t size){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 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
61 static int devzero = -1;
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
62 if (size > SIZE_MAX) {
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
63 mp_msg(MSGT_OSDEP, MSGL_FATAL,
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
64 "Shared memory allocation larger than system max. allocation size.\n");
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
65 return NULL;
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
66 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67 while(1){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 switch(shmem_type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 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
70 #ifdef MAP_ANON
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72 if(p==MAP_FAILED) break; // failed
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
73 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using mmap anon (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74 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
75 #else
448d1bf28f5a Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents: 498
diff changeset
76 // 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
77 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
78 #endif
1347
448d1bf28f5a Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents: 498
diff changeset
79 break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 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
81 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
83 if(p==MAP_FAILED) break; // failed
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
84 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using mmap /dev/zero (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
85 return p;
3280
542f7c228e38 undef HAVE_SHM compilation bug fixed
alex
parents: 3084
diff changeset
86 case 2: { // ========= shmget() ==========
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
87 #ifdef HAVE_SHM
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
88 struct shmid_ds shmemds;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
89 int shmemid;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
90 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
91 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
92 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno));
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93 shmctl (shmemid, IPC_RMID, &shmemds);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96 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
97 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno));
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 if (shmdt(p) == -1) perror ("shmdt()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
99 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
100 }
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
101 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using SHM (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
102 return p;
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
103 #else
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
104 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
105 return NULL;
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
106 #endif
3280
542f7c228e38 undef HAVE_SHM compilation bug fixed
alex
parents: 3084
diff changeset
107 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
108 default:
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
109 mp_msg(MSGT_OSDEP, MSGL_FATAL,
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
110 "FATAL: Cannot allocate %"PRId64" bytes of shared memory :(\n",size);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
111 return NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
112 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
113 ++shmem_type;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
114 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
115 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
116
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
117 void shmem_free(void* p,int64_t size){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
118 switch(shmem_type){
9914
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
119 case 0:
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
120 case 1:
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
121 if(munmap(p,size)) {
34704
cc658103f214 Allow using a cache size of up to 4 TB.
reimar
parents: 33884
diff changeset
122 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %"PRId64" bytes: %s\n",
9914
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
123 p,size,strerror(errno));
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
124 }
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
125 break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
126 case 2:
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
127 #ifdef HAVE_SHM
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
128 if (shmdt(p) == -1)
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
129 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
130 strerror(errno));
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
131 #else
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
132 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
133 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
134 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
135 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
136 }