• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifdef __ANDROID__
2   #ifndef _ANDROID_ASHMEM_H
3     #define _ANDROID_ASHMEM_H
4 
5     #define _GNU_SOURCE
6     #include <sys/syscall.h>
7     #include <unistd.h>
8     #include <fcntl.h>
9     #include <linux/ashmem.h>
10     #include <sys/ioctl.h>
11     #include <sys/mman.h>
12     #include <sys/shm.h>
13     #include <stdio.h>
14     #define ASHMEM_DEVICE "/dev/ashmem"
15 
shmdt(const void * address)16 int shmdt(const void *address) {
17 
18     #if defined(SYS_shmdt)
19   return syscall(SYS_shmdt, address);
20     #else
21   return syscall(SYS_ipc, SHMDT, 0, 0, 0, address, 0);
22     #endif
23 
24 }
25 
shmctl(int __shmid,int __cmd,struct shmid_ds * __buf)26 int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf) {
27 
28   int ret = 0;
29   if (__cmd == IPC_RMID) {
30 
31     int               length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
32     struct ashmem_pin pin = {0, length};
33     ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
34     close(__shmid);
35 
36   }
37 
38   return ret;
39 
40 }
41 
shmget(key_t __key,size_t __size,int __shmflg)42 int shmget(key_t __key, size_t __size, int __shmflg) {
43 
44   (void)__shmflg;
45   int  fd, ret;
46   char ourkey[11];
47 
48   fd = open(ASHMEM_DEVICE, O_RDWR);
49   if (fd < 0) return fd;
50 
51   sprintf(ourkey, "%d", __key);
52   ret = ioctl(fd, ASHMEM_SET_NAME, ourkey);
53   if (ret < 0) goto error;
54 
55   ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
56   if (ret < 0) goto error;
57 
58   return fd;
59 
60 error:
61   close(fd);
62   return ret;
63 
64 }
65 
shmat(int __shmid,const void * __shmaddr,int __shmflg)66 void *shmat(int __shmid, const void *__shmaddr, int __shmflg) {
67 
68   (void)__shmflg;
69   int   size;
70   void *ptr;
71 
72   size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
73   if (size < 0) { return NULL; }
74 
75   ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
76   if (ptr == MAP_FAILED) { return NULL; }
77 
78   return ptr;
79 
80 }
81 
82   #endif                                              /* !_ANDROID_ASHMEM_H */
83 #endif                                                      /* !__ANDROID__ */
84 
85