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.
31 #include <sys/types.h>
34 #ifdef HAVE_SYS_MMAN_H
36 #elif defined(__BEOS__)
39 #include <sys/socket.h>
46 #include <sys/select.h>
54 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
55 #define MAP_ANON MAP_ANONYMOUS
58 static int shmem_type
=0;
60 void* shmem_alloc(int size
){
62 static int devzero
= -1;
65 case 0: // ========= MAP_ANON|MAP_SHARED ==========
67 p
=mmap(0,size
,PROT_READ
|PROT_WRITE
,MAP_ANON
|MAP_SHARED
,-1,0);
68 if(p
==MAP_FAILED
) break; // failed
69 mp_dbg(MSGT_OSDEP
, MSGL_DBG2
, "shmem: %d bytes allocated using mmap anon (%p)\n",size
,p
);
72 // system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail
73 mp_dbg(MSGT_OSDEP
, MSGL_DBG3
, "shmem: using mmap anon failed\n");
76 case 1: // ========= MAP_SHARED + /dev/zero ==========
77 if (devzero
== -1 && (devzero
= open("/dev/zero", O_RDWR
, 0)) == -1) break;
78 p
=mmap(0,size
,PROT_READ
|PROT_WRITE
,MAP_SHARED
,devzero
,0);
79 if(p
==MAP_FAILED
) break; // failed
80 mp_dbg(MSGT_OSDEP
, MSGL_DBG2
, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size
,p
);
82 case 2: { // ========= shmget() ==========
84 struct shmid_ds shmemds
;
86 if ((shmemid
= shmget(IPC_PRIVATE
, size
, IPC_CREAT
| 0600)) == -1) break;
87 if ((p
= shmat(shmemid
, 0, 0)) == (void *)-1){
88 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmem: shmat() failed: %s\n", strerror(errno
));
89 shmctl (shmemid
, IPC_RMID
, &shmemds
);
92 if (shmctl(shmemid
, IPC_RMID
, &shmemds
) == -1) {
93 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmem: shmctl() failed: %s\n", strerror(errno
));
94 if (shmdt(p
) == -1) perror ("shmdt()");
97 mp_dbg(MSGT_OSDEP
, MSGL_DBG2
, "shmem: %d bytes allocated using SHM (%p)\n",size
,p
);
100 mp_msg(MSGT_OSDEP
, MSGL_FATAL
, "shmem: no SHM support was compiled in!\n");
105 mp_msg(MSGT_OSDEP
, MSGL_FATAL
,
106 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size
);
113 void shmem_free(void* p
,int size
){
118 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "munmap failed on %p %d bytes: %s\n",
119 p
,size
,strerror(errno
));
125 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmfree: shmdt() failed: %s\n",
128 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmfree: no SHM support was compiled in!\n");