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