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>
45 #include <sys/select.h>
53 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
54 #define MAP_ANON MAP_ANONYMOUS
57 static int shmem_type
=0;
59 void* shmem_alloc(int size
){
61 static int devzero
= -1;
64 case 0: // ========= MAP_ANON|MAP_SHARED ==========
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
);
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");
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
);
81 case 2: { // ========= shmget() ==========
83 struct shmid_ds shmemds
;
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
);
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()");
96 mp_dbg(MSGT_OSDEP
, MSGL_DBG2
, "shmem: %d bytes allocated using SHM (%p)\n",size
,p
);
99 mp_msg(MSGT_OSDEP
, MSGL_FATAL
, "shmem: no SHM support was compiled in!\n");
104 mp_msg(MSGT_OSDEP
, MSGL_FATAL
,
105 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size
);
112 void shmem_free(void* p
,int size
){
117 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "munmap failed on %p %d bytes: %s\n",
118 p
,size
,strerror(errno
));
124 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmfree: shmdt() failed: %s\n",
127 mp_msg(MSGT_OSDEP
, MSGL_ERR
, "shmfree: no SHM support was compiled in!\n");