Last active
February 26, 2020 17:05
-
-
Save matyapiro31/1c364fc61cd7d3dd2eedba39ce1547dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SHM_NAME "test_mem" | |
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/mman.h> | |
#include "ashmem.h" | |
int main(int argc, char **argv) { | |
int shID = ashmem_create_region(SHM_NAME, 2); | |
if (shID < 0) | |
{ | |
perror("ashmem_create_region failed\n"); | |
return 1; | |
} | |
printf("ashmem_create_region: %d\n", shID); | |
char *sh_buffer = (char*)mmap(NULL, 2, PROT_READ | PROT_WRITE, MAP_SHARED, shID, 0); | |
if (sh_buffer == (char*)-1) | |
{ | |
perror("mmap failed"); | |
return 1; | |
} | |
printf("PID=%d\n", getpid()); | |
int ch = getchar(); | |
sh_buffer[0] = ch; | |
printf("Written 0x%x\n", ch); | |
munmap(sh_buffer, 2); | |
close(shID); | |
return 0; | |
} | |
//read mem | |
#define SHM_NAME "test_mem" | |
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/mman.h> | |
#include "ashmem.h" | |
int main(int argc, char **argv) { | |
int shID = ashmem_create_region(SHM_NAME, 2); | |
if (shID < 0) | |
{ | |
perror("ashmem_create_region failed\n"); | |
return 1; | |
} | |
// right here /dev/ashmem/test_mem is deleted | |
printf("ashmem_create_region: %d\n", shID); | |
char *sh_buffer = (char*)mmap(NULL, 2, PROT_READ | PROT_WRITE, MAP_SHARED, shID, 0); | |
if (sh_buffer == (char*)-1) | |
{ | |
perror("mmap failed"); | |
return 1; | |
} | |
printf("PID=%d", getpid()); | |
do | |
{ | |
printf("VALUE = 0x%x\n", sh_buffer[0]); | |
} | |
while (getchar()); | |
return 0; | |
} | |
// | |
struct memArea{ | |
int *map; | |
int fd; | |
int size; | |
}; | |
struct memArea maps[10]; | |
int num = 0; | |
static jint getFD(JNIEnv *env, jclass cl, jstring path,jint size) | |
{ | |
const char *name = env->GetStringUTFChars(path,NULL); | |
jint fd = open("/dev/ashmem",O_RDWR); | |
ioctl(fd,ASHMEM_SET_NAME,name); | |
ioctl(fd,ASHMEM_SET_SIZE,size); | |
maps[num].size = size; | |
maps[num].fd = fd; | |
maps[num++].map = (int *)mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); | |
env->ReleaseStringUTFChars(path,name); | |
return fd; | |
} | |
static jint setNum(JNIEnv *env, jclass cl,jint fd, jint pos,jint num) | |
{ | |
for(int i = 0; i < num; i++) | |
{ | |
if(maps[i].fd == fd) | |
{ | |
if(pos < (maps[i].size/ sizeof(int))) | |
{ | |
maps[i].map[pos] = num; | |
return 0; | |
} | |
return -1; | |
} | |
} | |
return -1; | |
} | |
static jint getNum(JNIEnv *env, jclass cl,jint fd, jint pos) | |
{ | |
for(int i = 0; i < num; i++) | |
{ | |
if(maps[i].fd == fd) | |
{ | |
if(pos < (maps[i].size/ sizeof(int))) | |
{ | |
return maps[i].map[pos]; | |
} | |
return -1; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment