The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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> |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | #include "JNIHelp.h" |
| 23 | #include <unistd.h> |
| 24 | #include <sys/mman.h> |
| 25 | |
| 26 | |
| 27 | namespace android { |
| 28 | |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 29 | static jobject android_os_MemoryFile_open(JNIEnv* env, jobject clazz, jstring name, jint length) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | { |
| 31 | const char* namestr = (name ? env->GetStringUTFChars(name, NULL) : NULL); |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | int result = ashmem_create_region(namestr, length); |
| 34 | |
| 35 | if (name) |
| 36 | env->ReleaseStringUTFChars(name, namestr); |
| 37 | |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 38 | if (result < 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | jniThrowException(env, "java/io/IOException", "ashmem_create_region failed"); |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 40 | return NULL; |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return jniCreateFileDescriptor(env, result); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 46 | static jint android_os_MemoryFile_mmap(JNIEnv* env, jobject clazz, jobject fileDescriptor, |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 47 | jint length, jint prot) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 49 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 50 | jint result = (jint)mmap(NULL, length, prot, MAP_SHARED, fd, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | if (!result) |
| 52 | jniThrowException(env, "java/io/IOException", "mmap failed"); |
| 53 | return result; |
| 54 | } |
| 55 | |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 56 | static void android_os_MemoryFile_munmap(JNIEnv* env, jobject clazz, jint addr, jint length) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 58 | int result = munmap((void *)addr, length); |
| 59 | if (result < 0) |
| 60 | jniThrowException(env, "java/io/IOException", "munmap failed"); |
| 61 | } |
| 62 | |
| 63 | static void android_os_MemoryFile_close(JNIEnv* env, jobject clazz, jobject fileDescriptor) |
| 64 | { |
| 65 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 66 | if (fd >= 0) { |
| 67 | jniSetFileDescriptorOfFD(env, fileDescriptor, -1); |
| 68 | close(fd); |
| 69 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static jint android_os_MemoryFile_read(JNIEnv* env, jobject clazz, |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 73 | jobject fileDescriptor, jint address, jbyteArray buffer, jint srcOffset, jint destOffset, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | jint count, jboolean unpinned) |
| 75 | { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 76 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | if (unpinned && ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) { |
| 78 | ashmem_unpin_region(fd, 0, 0); |
| 79 | jniThrowException(env, "java/io/IOException", "ashmem region was purged"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
Bjorn Bringert | ad984f1 | 2009-06-01 18:33:33 +0100 | [diff] [blame] | 83 | env->SetByteArrayRegion(buffer, destOffset, count, (const jbyte *)address + srcOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | if (unpinned) { |
| 86 | ashmem_unpin_region(fd, 0, 0); |
| 87 | } |
| 88 | return count; |
| 89 | } |
| 90 | |
| 91 | static jint android_os_MemoryFile_write(JNIEnv* env, jobject clazz, |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 92 | jobject fileDescriptor, jint address, jbyteArray buffer, jint srcOffset, jint destOffset, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | jint count, jboolean unpinned) |
| 94 | { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 95 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | if (unpinned && ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) { |
| 97 | ashmem_unpin_region(fd, 0, 0); |
| 98 | jniThrowException(env, "java/io/IOException", "ashmem region was purged"); |
| 99 | return -1; |
| 100 | } |
| 101 | |
Bjorn Bringert | ad984f1 | 2009-06-01 18:33:33 +0100 | [diff] [blame] | 102 | env->GetByteArrayRegion(buffer, srcOffset, count, (jbyte *)address + destOffset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | |
| 104 | if (unpinned) { |
| 105 | ashmem_unpin_region(fd, 0, 0); |
| 106 | } |
| 107 | return count; |
| 108 | } |
| 109 | |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 110 | static void android_os_MemoryFile_pin(JNIEnv* env, jobject clazz, jobject fileDescriptor, jboolean pin) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 112 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | int result = (pin ? ashmem_pin_region(fd, 0, 0) : ashmem_unpin_region(fd, 0, 0)); |
| 114 | if (result < 0) { |
| 115 | jniThrowException(env, "java/io/IOException", NULL); |
| 116 | } |
| 117 | } |
| 118 | |
Marco Nelissen | 7bcbd51 | 2009-06-23 10:34:55 -0700 | [diff] [blame] | 119 | static jint android_os_MemoryFile_get_size(JNIEnv* env, jobject clazz, |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 120 | jobject fileDescriptor) { |
| 121 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 122 | // Use ASHMEM_GET_SIZE to find out if the fd refers to an ashmem region. |
| 123 | // ASHMEM_GET_SIZE should succeed for all ashmem regions, and the kernel |
| 124 | // should return ENOTTY for all other valid file descriptors |
| 125 | int result = ashmem_get_size_region(fd); |
| 126 | if (result < 0) { |
| 127 | if (errno == ENOTTY) { |
| 128 | // ENOTTY means that the ioctl does not apply to this object, |
| 129 | // i.e., it is not an ashmem region. |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 130 | return (jint) -1; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 131 | } |
| 132 | // Some other error, throw exception |
| 133 | jniThrowIOException(env, errno); |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 134 | return (jint) -1; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 135 | } |
Marco Nelissen | ec10090 | 2009-06-17 08:56:59 -0700 | [diff] [blame] | 136 | return (jint) result; |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 137 | } |
| 138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | static const JNINativeMethod methods[] = { |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 140 | {"native_open", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_os_MemoryFile_open}, |
Bjorn Bringert | 963cd006 | 2009-05-29 14:05:12 +0100 | [diff] [blame] | 141 | {"native_mmap", "(Ljava/io/FileDescriptor;II)I", (void*)android_os_MemoryFile_mmap}, |
Bjorn Bringert | 761e091 | 2009-05-29 11:46:12 +0100 | [diff] [blame] | 142 | {"native_munmap", "(II)V", (void*)android_os_MemoryFile_munmap}, |
| 143 | {"native_close", "(Ljava/io/FileDescriptor;)V", (void*)android_os_MemoryFile_close}, |
| 144 | {"native_read", "(Ljava/io/FileDescriptor;I[BIIIZ)I", (void*)android_os_MemoryFile_read}, |
| 145 | {"native_write", "(Ljava/io/FileDescriptor;I[BIIIZ)V", (void*)android_os_MemoryFile_write}, |
| 146 | {"native_pin", "(Ljava/io/FileDescriptor;Z)V", (void*)android_os_MemoryFile_pin}, |
Marco Nelissen | 7bcbd51 | 2009-06-23 10:34:55 -0700 | [diff] [blame] | 147 | {"native_get_size", "(Ljava/io/FileDescriptor;)I", |
| 148 | (void*)android_os_MemoryFile_get_size} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | int register_android_os_MemoryFile(JNIEnv* env) |
| 152 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | return AndroidRuntime::registerNativeMethods( |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 154 | env, "android/os/MemoryFile", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | methods, NELEM(methods)); |
| 156 | } |
| 157 | |
| 158 | } |