blob: b21566bbc1860ca6387988650129932ae93d7c40 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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#define LOG_TAG "MemoryFile"
18#include <utils/Log.h>
19
20#include <cutils/ashmem.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080021#include "core_jni_helpers.h"
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <unistd.h>
24#include <sys/mman.h>
25
26
27namespace android {
28
John Reckdea6a022017-05-25 17:09:33 -070029static jboolean android_os_MemoryFile_pin(JNIEnv* env, jobject clazz, jobject fileDescriptor,
30 jboolean pin) {
Bjorn Bringert761e0912009-05-29 11:46:12 +010031 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 int result = (pin ? ashmem_pin_region(fd, 0, 0) : ashmem_unpin_region(fd, 0, 0));
33 if (result < 0) {
34 jniThrowException(env, "java/io/IOException", NULL);
35 }
John Reckdea6a022017-05-25 17:09:33 -070036 return result == ASHMEM_WAS_PURGED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037}
38
Marco Nelissen7bcbd512009-06-23 10:34:55 -070039static jint android_os_MemoryFile_get_size(JNIEnv* env, jobject clazz,
Bjorn Bringert963cd0062009-05-29 14:05:12 +010040 jobject fileDescriptor) {
41 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
42 // Use ASHMEM_GET_SIZE to find out if the fd refers to an ashmem region.
43 // ASHMEM_GET_SIZE should succeed for all ashmem regions, and the kernel
44 // should return ENOTTY for all other valid file descriptors
45 int result = ashmem_get_size_region(fd);
46 if (result < 0) {
47 if (errno == ENOTTY) {
48 // ENOTTY means that the ioctl does not apply to this object,
49 // i.e., it is not an ashmem region.
Marco Nelissenec100902009-06-17 08:56:59 -070050 return (jint) -1;
Bjorn Bringert963cd0062009-05-29 14:05:12 +010051 }
52 // Some other error, throw exception
53 jniThrowIOException(env, errno);
Marco Nelissenec100902009-06-17 08:56:59 -070054 return (jint) -1;
Bjorn Bringert963cd0062009-05-29 14:05:12 +010055 }
Marco Nelissenec100902009-06-17 08:56:59 -070056 return (jint) result;
Bjorn Bringert963cd0062009-05-29 14:05:12 +010057}
58
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059static const JNINativeMethod methods[] = {
John Reckdea6a022017-05-25 17:09:33 -070060 {"native_pin", "(Ljava/io/FileDescriptor;Z)Z", (void*)android_os_MemoryFile_pin},
Marco Nelissen7bcbd512009-06-23 10:34:55 -070061 {"native_get_size", "(Ljava/io/FileDescriptor;)I",
62 (void*)android_os_MemoryFile_get_size}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063};
64
John Reckdea6a022017-05-25 17:09:33 -070065int register_android_os_MemoryFile(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -080066 return RegisterMethodsOrDie(env, "android/os/MemoryFile", methods, NELEM(methods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067}
68
69}