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 | #include "android_nio_utils.h" |
| 18 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 19 | #include "core_jni_helpers.h" |
| 20 | |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 21 | namespace { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 23 | void* getPointer(JNIEnv *_env, jobject buffer, jarray *array, void** elements) { |
| 24 | assert(array); |
Orion Hodson | 61783592 | 2019-02-28 15:15:34 +0000 | [diff] [blame] | 25 | jint position; |
| 26 | jint limit; |
| 27 | jint elementSizeShift; |
| 28 | jlong pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | if (pointer != 0L) { |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 30 | *array = nullptr; |
| 31 | *elements = nullptr; |
Orion Hodson | 61783592 | 2019-02-28 15:15:34 +0000 | [diff] [blame] | 32 | pointer += position << elementSizeShift; |
| 33 | return reinterpret_cast<void*>(pointer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | } |
Orion Hodson | 61783592 | 2019-02-28 15:15:34 +0000 | [diff] [blame] | 35 | jint offset = jniGetNioBufferBaseArrayOffset(_env, buffer); |
| 36 | *array = jniGetNioBufferBaseArray(_env, buffer); |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 37 | *elements = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0); |
| 38 | return reinterpret_cast<void*>(reinterpret_cast<char*>(*elements) + offset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 41 | void releasePointer(JNIEnv *_env, jarray array, void *elements, jboolean commit) { |
| 42 | _env->ReleasePrimitiveArrayCritical(array, elements, commit ? 0 : JNI_ABORT); |
| 43 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 45 | } // namespace |
| 46 | |
| 47 | void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) { |
| 48 | void* elements; |
| 49 | return getPointer(_env, buffer, array, &elements); |
| 50 | } |
| 51 | |
| 52 | void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit) { |
| 53 | releasePointer(_env, array, data, commit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 58 | android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | fEnv = env; |
| 60 | fCommit = commit; |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 61 | fPointer = getPointer(env, nioBuffer, &fArray, &fElements); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | android::AutoBufferPointer::~AutoBufferPointer() { |
Orion Hodson | e522c99 | 2019-04-15 16:14:51 +0100 | [diff] [blame^] | 65 | if (nullptr != fArray) { |
| 66 | releasePointer(fEnv, fArray, fElements, fCommit); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | } |
| 68 | } |