Intel compiler icc 11.1 supports compilation of internal mp3lib and internal liba52.
[mplayer.git] / osdep / shmem.c
blob9788d02bc56eb505c3b083ed715656dcea985b6b
1 /*
2 * shared memory allocation
4 * based on mpg123's xfermem.c by
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
7 * This file is part of MPlayer.
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.
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.
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.
24 #include "config.h"
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>
34 #ifdef HAVE_SYS_MMAN_H
35 #include <sys/mman.h>
36 #elif defined(__BEOS__)
37 #include <mman.h>
38 #endif
39 #include <sys/socket.h>
40 #include <fcntl.h>
42 #include "mp_msg.h"
44 #ifdef AIX
45 #include <sys/select.h>
46 #endif
48 #ifdef HAVE_SHM
49 #include <sys/ipc.h>
50 #include <sys/shm.h>
51 #endif
53 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
54 #define MAP_ANON MAP_ANONYMOUS
55 #endif
57 static int shmem_type=0;
59 void* shmem_alloc(int size){
60 void* p;
61 static int devzero = -1;
62 while(1){
63 switch(shmem_type){
64 case 0: // ========= MAP_ANON|MAP_SHARED ==========
65 #ifdef MAP_ANON
66 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
67 if(p==MAP_FAILED) break; // failed
68 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p);
69 return p;
70 #else
71 // system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail
72 mp_dbg(MSGT_OSDEP, MSGL_DBG3, "shmem: using mmap anon failed\n");
73 #endif
74 break;
75 case 1: // ========= MAP_SHARED + /dev/zero ==========
76 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
77 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
78 if(p==MAP_FAILED) break; // failed
79 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p);
80 return p;
81 case 2: { // ========= shmget() ==========
82 #ifdef HAVE_SHM
83 struct shmid_ds shmemds;
84 int shmemid;
85 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
86 if ((p = shmat(shmemid, 0, 0)) == (void *)-1){
87 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno));
88 shmctl (shmemid, IPC_RMID, &shmemds);
89 break;
91 if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
92 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno));
93 if (shmdt(p) == -1) perror ("shmdt()");
94 break;
96 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p);
97 return p;
98 #else
99 mp_msg(MSGT_OSDEP, MSGL_FATAL, "shmem: no SHM support was compiled in!\n");
100 return NULL;
101 #endif
103 default:
104 mp_msg(MSGT_OSDEP, MSGL_FATAL,
105 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size);
106 return NULL;
108 ++shmem_type;
112 void shmem_free(void* p,int size){
113 switch(shmem_type){
114 case 0:
115 case 1:
116 if(munmap(p,size)) {
117 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %d bytes: %s\n",
118 p,size,strerror(errno));
120 break;
121 case 2:
122 #ifdef HAVE_SHM
123 if (shmdt(p) == -1)
124 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: shmdt() failed: %s\n",
125 strerror(errno));
126 #else
127 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: no SHM support was compiled in!\n");
128 #endif
129 break;