[Mplayer-cvslog] CVS: main/linux shmem.c,1.2,1.3
Jürgen Keil
jkeil at mplayer.dev.hu
Thu Jul 19 20:49:15 CEST 2001
Update of /cvsroot/mplayer/main/linux
In directory mplayer:/var/tmp.root/cvs-serv27987/linux
Modified Files:
shmem.c
Log Message:
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
attempt on such a system. The code tries mmap with /dev/zero and MIT-shm next.
Index: shmem.c
===================================================================
RCS file: /cvsroot/mplayer/main/linux/shmem.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- shmem.c 18 Apr 2001 01:25:09 -0000 1.2
+++ shmem.c 19 Jul 2001 18:49:13 -0000 1.3
@@ -33,51 +33,57 @@
static int shmem_type=0;
-void* shmem_alloc(int size){
-void* p;
-int devzero;
-while(1){
- switch(shmem_type){
- case 0: // ========= MAP_ANON|MAP_SHARED ==========
- p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
- if(p==MAP_FAILED) break; // failed
-// printf("shmem: %d bytes allocated using mmap anon (%X)\n",size,p);
- return p;
- case 1: // ========= MAP_SHARED + /dev/zero ==========
- if ((devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
- p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
- if(p==MAP_FAILED) break; // failed
-// printf("shmem: %d bytes allocated using mmap /dev/zero (%X)\n",size,p);
- return p;
- case 2: { // ========= shmget() ==========
- struct shmid_ds shmemds;
- int shmemid;
- if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
- if ((int)(p = shmat(shmemid, 0, 0)) == -1){
- perror ("shmat()");
- shmctl (shmemid, IPC_RMID, &shmemds);
+void* shmem_alloc(int size) {
+ void* p;
+ static int devzero = -1;
+ while(1) {
+ switch(shmem_type) {
+ case 0:
+#ifdef MAP_ANON
+ // ========= MAP_ANON|MAP_SHARED ==========
+ p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
+ if(p==MAP_FAILED) break; // failed
+ //printf("shmem: %d bytes allocated using mmap anon (%X)\n",size,p);
+ return p;
+#else
+ //system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail
break;
+#endif
+ case 1: // ========= MAP_SHARED + /dev/zero ==========
+ if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
+ p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
+ if(p==MAP_FAILED) break; // failed
+ //printf("shmem: %d bytes allocated using mmap /dev/zero (%X)\n",size,p);
+ return p;
+ case 2: { // ========= shmget() ==========
+ struct shmid_ds shmemds;
+ int shmemid;
+ if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
+ if ((int)(p = shmat(shmemid, 0, 0)) == -1) {
+ perror ("shmat()");
+ shmctl (shmemid, IPC_RMID, &shmemds);
+ break;
+ }
+ if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
+ perror ("shmctl()");
+ if (shmdt(p) == -1) perror ("shmdt()");
+ break;
+ }
+ //printf("shmem: %d bytes allocated using shmget() & shmat() (%X)\n",size,p);
+ return p;
}
- if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
- perror ("shmctl()");
- if (shmdt(p) == -1) perror ("shmdt()");
- break;
+ default:
+ printf("FATAL: Cannot alloate %d bytes shared memory :(\n",size);
+ return NULL;
}
-// printf("shmem: %d bytes allocated using shmget() & shmat() (%X)\n",size,p);
- return p;
- }
- default:
- printf("FATAL: Cannot alloate %d bytes shared memory :(\n",size);
- return NULL;
+ ++shmem_type;
}
- ++shmem_type;
-}
}
-void shmem_free(void* p){
- switch(shmem_type){
- case 2:
- if (shmdt(p) == -1) perror ("shmdt()");
- break;
+void shmem_free(void* p) {
+ switch(shmem_type) {
+ case 2:
+ if (shmdt(p) == -1) perror ("shmdt()");
+ break;
}
}
More information about the MPlayer-cvslog
mailing list