annotate linux/shmem.c @ 1238:e7d98f8f9459

Corrected SDL include path.
author atmos4
date Wed, 27 Jun 2001 11:40:15 +0000
parents 86c689b3e40e
children 448d1bf28f5a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
1 /*
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
2 * shmem.c - Shared memory allocation
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
3 *
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
4 * based on mpg123's xfermem.c by
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
5 * Oliver Fromme <oliver.fromme@heim3.tu-clausthal.de>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
6 * Sun Apr 6 02:26:26 MET DST 1997
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
7 */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
8
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
9 #include <stdio.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
10 #include <stdlib.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
11 #include <string.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
12 #include <unistd.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
13 #include <errno.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
14 #include <sys/types.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
15 #include <sys/time.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
16 #include <sys/uio.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
17 #include <sys/mman.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
18 #include <sys/socket.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
19 #include <fcntl.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
20
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
21 #ifdef AIX
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
22 #include <sys/select.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
24
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25 #include <sys/ipc.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
26 #include <sys/shm.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
27
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
28 extern int errno;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
29
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30 #if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 #define MAP_ANON MAP_ANONYMOUS
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
33
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 static int shmem_type=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
36 void* shmem_alloc(int size){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
37 void* p;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 int devzero;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39 while(1){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
40 switch(shmem_type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41 case 0: // ========= MAP_ANON|MAP_SHARED ==========
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_ANON|MAP_SHARED,-1,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
43 if(p==MAP_FAILED) break; // failed
498
86c689b3e40e disable ddebug printfs
arpi_esp
parents: 1
diff changeset
44 // printf("shmem: %d bytes allocated using mmap anon (%X)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
45 return p;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
46 case 1: // ========= MAP_SHARED + /dev/zero ==========
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
47 if ((devzero = open("/dev/zero", O_RDWR, 0)) == -1) break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
48 p=mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,devzero,0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 if(p==MAP_FAILED) break; // failed
498
86c689b3e40e disable ddebug printfs
arpi_esp
parents: 1
diff changeset
50 // printf("shmem: %d bytes allocated using mmap /dev/zero (%X)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
51 return p;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
52 case 2: { // ========= shmget() ==========
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
53 struct shmid_ds shmemds;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
54 int shmemid;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
55 if ((shmemid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600)) == -1) break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
56 if ((int)(p = shmat(shmemid, 0, 0)) == -1){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
57 perror ("shmat()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 shmctl (shmemid, IPC_RMID, &shmemds);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
60 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 if (shmctl(shmemid, IPC_RMID, &shmemds) == -1) {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
62 perror ("shmctl()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
63 if (shmdt(p) == -1) perror ("shmdt()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
65 }
498
86c689b3e40e disable ddebug printfs
arpi_esp
parents: 1
diff changeset
66 // printf("shmem: %d bytes allocated using shmget() & shmat() (%X)\n",size,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
67 return p;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
69 default:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70 printf("FATAL: Cannot alloate %d bytes shared memory :(\n",size);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 return NULL;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
72 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
73 ++shmem_type;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
75 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
76
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
77 void shmem_free(void* p){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
78 switch(shmem_type){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
79 case 2:
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 if (shmdt(p) == -1) perror ("shmdt()");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
81 break;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
83 }