• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <jni.h>
18 
19 #include <android/sharedmem_jni.h>
20 
21 #include <sys/mman.h>
22 #include <unistd.h>
23 
nWriteByte(JNIEnv * env,jobject,jobject jSharedMemory,jint index,jbyte value)24 jboolean nWriteByte(JNIEnv* env, jobject, jobject jSharedMemory, jint index, jbyte value) {
25     int fd = ASharedMemory_dupFromJava(env, jSharedMemory);
26     if (fd == -1) return false;
27     void* addr = mmap(nullptr, 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, index);
28     if (addr == nullptr) {
29         close(fd);
30         return false;
31     }
32     reinterpret_cast<int8_t*>(addr)[0] = value;
33     munmap(addr, 1);
34     close(fd);
35     return true;
36 }
37 
38 static JNINativeMethod gMethods[] = {
39     {  "nWriteByte", "(Landroid/os/SharedMemory;IB)Z", (void *) nWriteByte },
40 };
41 
register_android_os_cts_SharedMemoryTest(JNIEnv * env)42 int register_android_os_cts_SharedMemoryTest(JNIEnv* env)
43 {
44     jclass clazz = env->FindClass("android/os/cts/SharedMemoryTest");
45 
46     return env->RegisterNatives(clazz, gMethods,
47             sizeof(gMethods) / sizeof(JNINativeMethod));
48 }
49