OSDN Git Service

update and improved clock timing
[motonesemu/motonesemu.git] / libs / vgashm.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/ipc.h>
4 #include <sys/shm.h>
5
6 #include "tools.h"
7 #include "vga.h"
8
9 void *vga_shm_get(void) {
10     void* ret;
11     key_t key;
12     int   shmid;
13
14     //create shared memory
15     key = ftok(VGA_SHM, VGA_SHM_PRJ_ID);
16     if (key == -1) {
17         fprintf(stderr, "error preparing shared memory.\n");
18         return NULL;
19     }
20
21     if((shmid = shmget(key, VGA_SHM_SIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
22     {
23         //printf("Shared memory segment exists - opening as client\n");
24
25         /* Segment probably already exists - try as a client */
26         if((shmid = shmget(key, VGA_SHM_SIZE, 0)) == -1) 
27         {
28             fprintf(stderr, "error opening shared memory.\n");
29             return NULL;
30         }
31     }
32
33     /* Attach (map) the shared memory segment into the current process */
34     if((ret = shmat(shmid, 0, 0)) == (void*)-1)
35     {
36         fprintf(stderr, "error attaching shared memory.\n");
37         return NULL;
38     }
39
40     return ret;
41 }
42
43 void vga_shm_free(void* addr) {
44     shmdt(addr);
45 }
46