docs: document --quvi-format
[mplayer.git] / osdep / shmem.c
blobf1cec1be37980a3bf892fc8d43b71c0d5266c7c3
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 #include "shmem.h"
55 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
56 #define MAP_ANON MAP_ANONYMOUS
57 #endif
59 static int shmem_type=0;
61 void* shmem_alloc(int size){
62 void* p;
63 static int devzero = -1;
64 while(1){
65 switch(shmem_type){
66 case 0: // ========= MAP_ANON|MAP_SHARED ==========
67 #ifdef MAP_ANON
68 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
69 if(p==MAP_FAILED) break; // failed
70 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap anon (%p)\n",size,p);
71 return p;
72 #else
73 // system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail
74 mp_dbg(MSGT_OSDEP, MSGL_DBG3, "shmem: using mmap anon failed\n");
75 #endif
76 break;
77 case 1: // ========= MAP_SHARED + /dev/zero ==========
78 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
79 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
80 if(p==MAP_FAILED) break; // failed
81 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using mmap /dev/zero (%p)\n",size,p);
82 return p;
83 case 2: { // ========= shmget() ==========
84 #ifdef HAVE_SHM
85 struct shmid_ds shmemds;
86 int shmemid;
87 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
88 if ((p = shmat(shmemid, 0, 0)) == (void *)-1){
89 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno));
90 shmctl (shmemid, IPC_RMID, &shmemds);
91 break;
93 if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
94 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno));
95 if (shmdt(p) == -1) perror ("shmdt()");
96 break;
98 mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %d bytes allocated using SHM (%p)\n",size,p);
99 return p;
100 #else
101 mp_msg(MSGT_OSDEP, MSGL_FATAL, "shmem: no SHM support was compiled in!\n");
102 return NULL;
103 #endif
105 default:
106 mp_msg(MSGT_OSDEP, MSGL_FATAL,
107 "FATAL: Cannot allocate %d bytes of shared memory :(\n",size);
108 return NULL;
110 ++shmem_type;
114 void shmem_free(void* p,int size){
115 switch(shmem_type){
116 case 0:
117 case 1:
118 if(munmap(p,size)) {
119 mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %d bytes: %s\n",
120 p,size,strerror(errno));
122 break;
123 case 2:
124 #ifdef HAVE_SHM
125 if (shmdt(p) == -1)
126 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: shmdt() failed: %s\n",
127 strerror(errno));
128 #else
129 mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: no SHM support was compiled in!\n");
130 #endif
131 break;