Mercurial > mplayer.hg
annotate linux/shmem.c @ 2200:1333f6184ad3
some ifno for cvs write owners
author | arpi |
---|---|
date | Sun, 14 Oct 2001 19:18:02 +0000 |
parents | 448d1bf28f5a |
children | 8687599be101 |
rev | line source |
---|---|
1 | 1 /* |
2 * shmem.c - Shared memory allocation | |
3 * | |
4 * based on mpg123's xfermem.c by | |
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de> | |
6 * Sun Apr 6 02:26:26 MET DST 1997 | |
7 */ | |
8 | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <string.h> | |
12 #include <unistd.h> | |
13 #include <errno.h> | |
14 #include <sys/types.h> | |
15 #include <sys/time.h> | |
16 #include <sys/uio.h> | |
17 #include <sys/mman.h> | |
18 #include <sys/socket.h> | |
19 #include <fcntl.h> | |
20 | |
21 #ifdef AIX | |
22 #include <sys/select.h> | |
23 #endif | |
24 | |
25 #include <sys/ipc.h> | |
26 #include <sys/shm.h> | |
27 | |
28 extern int errno; | |
29 | |
30 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON) | |
31 #define MAP_ANON MAP_ANONYMOUS | |
32 #endif | |
33 | |
34 static int shmem_type=0; | |
35 | |
36 void* shmem_alloc(int size){ | |
37 void* p; | |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
38 static int devzero = -1; |
1 | 39 while(1){ |
40 switch(shmem_type){ | |
41 case 0: // ========= MAP_ANON|MAP_SHARED ========== | |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
42 #ifdef MAP_ANON |
1 | 43 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0); |
44 if(p==MAP_FAILED) break; // failed | |
498 | 45 // printf("shmem: %d bytes allocated using mmap anon (%X)\n",size,p); |
1 | 46 return p; |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
47 #else |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
48 // system does not support MAP_ANON at all (e.g. solaris 2.5.1/2.6), just fail |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
49 break; |
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
50 #endif |
1 | 51 case 1: // ========= MAP_SHARED + /dev/zero ========== |
1347
448d1bf28f5a
Solaris 2.6 and older do not support MAP_ANON, just fail the mmap MAP_ANON
jkeil
parents:
498
diff
changeset
|
52 if (devzero == -1 && (devzero = open("/dev/zero", O_RDWR, 0)) == -1) break; |
1 | 53 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0); |
54 if(p==MAP_FAILED) break; // failed | |
498 | 55 // printf("shmem: %d bytes allocated using mmap /dev/zero (%X)\n",size,p); |
1 | 56 return p; |
57 case 2: { // ========= shmget() ========== | |
58 struct shmid_ds shmemds; | |
59 int shmemid; | |
60 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break; | |
61 if ((int)(p = shmat(shmemid, 0, 0)) == -1){ | |
62 perror ("shmat()"); | |
63 shmctl (shmemid, IPC_RMID, &shmemds); | |
64 break; | |
65 } | |
66 if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) { | |
67 perror ("shmctl()"); | |
68 if (shmdt(p) == -1) perror ("shmdt()"); | |
69 break; | |
70 } | |
498 | 71 // printf("shmem: %d bytes allocated using shmget() & shmat() (%X)\n",size,p); |
1 | 72 return p; |
73 } | |
74 default: | |
75 printf("FATAL: Cannot alloate %d bytes shared memory :(\n",size); | |
76 return NULL; | |
77 } | |
78 ++shmem_type; | |
79 } | |
80 } | |
81 | |
82 void shmem_free(void* p){ | |
83 switch(shmem_type){ | |
84 case 2: | |
85 if (shmdt(p) == -1) perror ("shmdt()"); | |
86 break; | |
87 } | |
88 } |