2001-02-24 20:28:24 +00:00
|
|
|
/*
|
2009-03-01 13:13:25 +00:00
|
|
|
* shared memory allocation
|
|
|
|
*
|
|
|
|
* based on mpg123's xfermem.c by
|
|
|
|
* Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-02-24 20:28:24 +00:00
|
|
|
*/
|
|
|
|
|
2005-11-14 00:30:37 +00:00
|
|
|
#include "config.h"
|
2001-11-19 16:57:40 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/uio.h>
|
2002-03-24 01:05:43 +00:00
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/mman.h>
|
2002-03-24 01:05:43 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <fcntl.h>
|
2012-03-04 14:37:31 +00:00
|
|
|
#include <inttypes.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.h"
|
2001-11-23 16:08:44 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#ifdef AIX
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2001-11-19 16:57:40 +00:00
|
|
|
#ifdef HAVE_SHM
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/shm.h>
|
2001-11-23 16:08:44 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2008-04-29 11:52:45 +00:00
|
|
|
#include "shmem.h"
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
|
|
|
|
#define MAP_ANON MAP_ANONYMOUS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int shmem_type=0;
|
|
|
|
|
2012-03-04 14:37:31 +00:00
|
|
|
void* shmem_alloc(int64_t size){
|
2001-02-24 20:28:24 +00:00
|
|
|
void* p;
|
2001-07-19 20:32:13 +00:00
|
|
|
static int devzero = -1;
|
2012-03-04 14:37:31 +00:00
|
|
|
if (size > SIZE_MAX) {
|
|
|
|
mp_msg(MSGT_OSDEP, MSGL_FATAL,
|
|
|
|
"Shared memory allocation larger than system max. allocation size.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
while(1){
|
|
|
|
switch(shmem_type){
|
|
|
|
case 0: // ========= MAP_ANON|MAP_SHARED ==========
|
2001-07-19 20:32:13 +00:00
|
|
|
#ifdef MAP_ANON
|
2001-02-24 20:28:24 +00:00
|
|
|
p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
|
|
|
|
if(p==MAP_FAILED) break; // failed
|
2012-03-04 14:37:31 +00:00
|
|
|
mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using mmap anon (%p)\n",size,p);
|
2001-02-24 20:28:24 +00:00
|
|
|
return p;
|
2001-07-19 20:32:13 +00:00
|
|
|
#else
|
|
|
|
// system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail
|
2001-11-23 16:08:44 +00:00
|
|
|
mp_dbg(MSGT_OSDEP, MSGL_DBG3, "shmem: using mmap anon failed\n");
|
2001-07-19 20:32:13 +00:00
|
|
|
#endif
|
2001-11-23 16:08:44 +00:00
|
|
|
break;
|
2001-02-24 20:28:24 +00:00
|
|
|
case 1: // ========= MAP_SHARED + /dev/zero ==========
|
2001-07-19 20:32:13 +00:00
|
|
|
if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
|
2001-02-24 20:28:24 +00:00
|
|
|
p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
|
|
|
|
if(p==MAP_FAILED) break; // failed
|
2012-03-04 14:37:31 +00:00
|
|
|
mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using mmap /dev/zero (%p)\n",size,p);
|
2001-02-24 20:28:24 +00:00
|
|
|
return p;
|
|
|
|
case 2: { // ========= shmget() ==========
|
2001-12-03 14:37:26 +00:00
|
|
|
#ifdef HAVE_SHM
|
2001-02-24 20:28:24 +00:00
|
|
|
struct shmid_ds shmemds;
|
|
|
|
int shmemid;
|
|
|
|
if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
|
2002-05-23 15:16:03 +00:00
|
|
|
if ((p = shmat(shmemid, 0, 0)) == (void *)-1){
|
2001-11-23 16:08:44 +00:00
|
|
|
mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmat() failed: %s\n", strerror(errno));
|
2001-02-24 20:28:24 +00:00
|
|
|
shmctl (shmemid, IPC_RMID, &shmemds);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
|
2001-11-23 16:08:44 +00:00
|
|
|
mp_msg(MSGT_OSDEP, MSGL_ERR, "shmem: shmctl() failed: %s\n", strerror(errno));
|
2001-02-24 20:28:24 +00:00
|
|
|
if (shmdt(p) == -1) perror ("shmdt()");
|
|
|
|
break;
|
|
|
|
}
|
2012-03-04 14:37:31 +00:00
|
|
|
mp_dbg(MSGT_OSDEP, MSGL_DBG2, "shmem: %"PRId64" bytes allocated using SHM (%p)\n",size,p);
|
2001-02-24 20:28:24 +00:00
|
|
|
return p;
|
2001-11-23 16:08:44 +00:00
|
|
|
#else
|
|
|
|
mp_msg(MSGT_OSDEP, MSGL_FATAL, "shmem: no SHM support was compiled in!\n");
|
2008-05-16 09:42:28 +00:00
|
|
|
return NULL;
|
2001-11-23 16:08:44 +00:00
|
|
|
#endif
|
2001-12-03 14:37:26 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
default:
|
2001-11-23 16:08:44 +00:00
|
|
|
mp_msg(MSGT_OSDEP, MSGL_FATAL,
|
2012-03-04 14:37:31 +00:00
|
|
|
"FATAL: Cannot allocate %"PRId64" bytes of shared memory :(\n",size);
|
2001-02-24 20:28:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
++shmem_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-04 14:37:31 +00:00
|
|
|
void shmem_free(void* p,int64_t size){
|
2001-02-24 20:28:24 +00:00
|
|
|
switch(shmem_type){
|
2003-04-12 13:52:21 +00:00
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
if(munmap(p,size)) {
|
2012-03-04 14:37:31 +00:00
|
|
|
mp_msg(MSGT_OSDEP, MSGL_ERR, "munmap failed on %p %"PRId64" bytes: %s\n",
|
2003-04-12 13:52:21 +00:00
|
|
|
p,size,strerror(errno));
|
|
|
|
}
|
|
|
|
break;
|
2001-02-24 20:28:24 +00:00
|
|
|
case 2:
|
2001-11-23 16:08:44 +00:00
|
|
|
#ifdef HAVE_SHM
|
|
|
|
if (shmdt(p) == -1)
|
|
|
|
mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: shmdt() failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
#else
|
|
|
|
mp_msg(MSGT_OSDEP, MSGL_ERR, "shmfree: no SHM support was compiled in!\n");
|
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|