annotate osdep/shmem.c @ 22661:9babdd441b76

Some more details for the mga_vid section taken from drivers/README.
author diego
date Sat, 17 Mar 2007 09:40:27 +0000
parents 936209c39ed1
children 8eff880f638c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 /*
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2 * shmem.c - Shared memory allocation
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3 *
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
4 * based on mpg123's xfermem.c by
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
6 * Sun Apr 6 02:26:26 MET DST 1997
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
7 */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
8
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13612
diff changeset
9 #include "config.h"
3008
8687599be101 #ifdef HAVE_SHM
alex
parents: 1347
diff changeset
10
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
11 #include <stdio.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
12 #include <stdlib.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
13 #include <string.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
14 #include <unistd.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
15 #include <errno.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
16 #include <sys/types.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
17 #include <sys/time.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
18 #include <sys/uio.h>
5298
7bb8ded875db check for mman.h
arpi
parents: 3280
diff changeset
19 #ifdef HAVE_SYS_MMAN_H
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
20 #include <sys/mman.h>
13612
c0bde085511c Zeta OS support, mostly working.
reimar
parents: 10197
diff changeset
21 #elif defined(__BEOS__)
c0bde085511c Zeta OS support, mostly working.
reimar
parents: 10197
diff changeset
22 #include <mman.h>
5298
7bb8ded875db check for mman.h
arpi
parents: 3280
diff changeset
23 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24 #include <sys/socket.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25 #include <fcntl.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 13612
diff changeset
27 #include "mp_msg.h"
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
28
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29 #ifdef AIX
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30 #include <sys/select.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32
3008
8687599be101 #ifdef HAVE_SHM
alex
parents: 1347
diff changeset
33 #ifdef HAVE_SHM
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 #include <sys/ipc.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35 #include <sys/shm.h>
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
36 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39 #define MAP_ANON MAP_ANONYMOUS
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
40 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42 static int shmem_type=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
43
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
44 void* shmem_alloc(int size){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 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
46 static int devzero = -1;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47 while(1){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
48 switch(shmem_type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 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
50 #ifdef MAP_ANON
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52 if(p==MAP_FAILED) break; // failed
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
53 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 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
55 #else
448d1bf28f5a Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents: 498
diff changeset
56 // 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
57 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
58 #endif
1347
448d1bf28f5a Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents: 498
diff changeset
59 break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 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
61 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
62 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 if(p==MAP_FAILED) break; // failed
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
64 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65 return p;
3280
542f7c228e38 undef HAVE_SHM compilation bug fixed
alex
parents: 3084
diff changeset
66 case 2: { // ========= shmget() ==========
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
67 #ifdef HAVE_SHM
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 struct shmid_ds shmemds;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 int shmemid;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70 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
71 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
72 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno));
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73 shmctl (shmemid, IPC_RMID, &shmemds);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
75 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
76 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
77 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno));
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
78 if (shmdt(p) == -1) perror ("shmdt()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
79 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 }
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
81 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82 return p;
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
83 #else
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
84 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
85 return(NULL);
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
86 #endif
3280
542f7c228e38 undef HAVE_SHM compilation bug fixed
alex
parents: 3084
diff changeset
87 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
88 default:
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
89 mp_msg(MSGT_OSDEP, MSGL_FATAL,
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
90 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
91 return NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
92 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93 ++shmem_type;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96
9914
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
97 void shmem_free(void* p,int size){
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 switch(shmem_type){
9914
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
99 case 0:
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
100 case 1:
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
101 if(munmap(p,size)) {
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
102 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %d bytes: %s\n",
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
103 p,size,strerror(errno));
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
104 }
3c856c0fc45e mem mapped area need to be unmapped.
albeu
parents: 9797
diff changeset
105 break;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
106 case 2:
3084
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
107 #ifdef HAVE_SHM
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
108 if (shmdt(p) == -1)
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
109 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
110 strerror(errno));
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
111 #else
a3f947d5f847 converted to mp_msg and fixed my previous HAVE_SHM bug
alex
parents: 3008
diff changeset
112 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
113 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
114 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
115 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
116 }