1 /* 2 * Copyright (C) 2016 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 <atomic> 18 #include <jni.h> 19 #include <cutils/ashmem.h> 20 #include <linux/ashmem.h> 21 #include <sys/ioctl.h> 22 #include <sys/mman.h> 23 24 jint android_util_MemoryIntArrayTest_createAshmem(__attribute__((unused)) JNIEnv* env, 25 __attribute__((unused)) jobject clazz, 26 jstring name, jint size) 27 { 28 29 if (name == NULL) { 30 return -1; 31 } 32 33 if (size < 0) { 34 return -1; 35 } 36 37 const char* nameStr = env->GetStringUTFChars(name, NULL); 38 const int ashmemSize = sizeof(std::atomic_int) * size; 39 int fd = ashmem_create_region(nameStr, ashmemSize); 40 env->ReleaseStringUTFChars(name, nameStr); 41 42 if (fd < 0) { 43 return -1; 44 } 45 46 int setProtResult = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); 47 if (setProtResult < 0) { 48 return -1; 49 } 50 51 return fd; 52 } 53 54 void android_util_MemoryIntArrayTest_setAshmemSize(__attribute__((unused)) JNIEnv* env, 55 __attribute__((unused)) jobject clazz, jint fd, jint size) 56 { 57 if (fd < 0) { 58 return; 59 } 60 61 if (size < 0) { 62 return; 63 } 64 65 ioctl(fd, ASHMEM_SET_SIZE, size); 66 } 67