blob: a62dd7c4048fd370ca54f13ed6ded57a0e1179e5 [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#include "android_nio_utils.h"
18
Andreas Gampe987f79f2014-11-18 17:29:46 -080019#include "core_jni_helpers.h"
20
Orion Hodsone522c992019-04-15 16:14:51 +010021namespace {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Orion Hodsone522c992019-04-15 16:14:51 +010023void* getPointer(JNIEnv *_env, jobject buffer, jarray *array, void** elements) {
24 assert(array);
Orion Hodson617835922019-02-28 15:15:34 +000025 jint position;
26 jint limit;
27 jint elementSizeShift;
28 jlong pointer = jniGetNioBufferFields(_env, buffer, &position, &limit, &elementSizeShift);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 if (pointer != 0L) {
Orion Hodsone522c992019-04-15 16:14:51 +010030 *array = nullptr;
31 *elements = nullptr;
Orion Hodson617835922019-02-28 15:15:34 +000032 pointer += position << elementSizeShift;
33 return reinterpret_cast<void*>(pointer);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034 }
Orion Hodson617835922019-02-28 15:15:34 +000035 jint offset = jniGetNioBufferBaseArrayOffset(_env, buffer);
36 *array = jniGetNioBufferBaseArray(_env, buffer);
Orion Hodsone522c992019-04-15 16:14:51 +010037 *elements = _env->GetPrimitiveArrayCritical(*array, (jboolean *) 0);
38 return reinterpret_cast<void*>(reinterpret_cast<char*>(*elements) + offset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039}
40
Orion Hodsone522c992019-04-15 16:14:51 +010041void releasePointer(JNIEnv *_env, jarray array, void *elements, jboolean commit) {
42 _env->ReleasePrimitiveArrayCritical(array, elements, commit ? 0 : JNI_ABORT);
43}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Orion Hodsone522c992019-04-15 16:14:51 +010045} // namespace
46
47void* android::nio_getPointer(JNIEnv *_env, jobject buffer, jarray *array) {
48 void* elements;
49 return getPointer(_env, buffer, array, &elements);
50}
51
52void android::nio_releasePointer(JNIEnv *_env, jarray array, void *data, jboolean commit) {
53 releasePointer(_env, array, data, commit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054}
55
56///////////////////////////////////////////////////////////////////////////////
57
Orion Hodsone522c992019-04-15 16:14:51 +010058android::AutoBufferPointer::AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 fEnv = env;
60 fCommit = commit;
Orion Hodsone522c992019-04-15 16:14:51 +010061 fPointer = getPointer(env, nioBuffer, &fArray, &fElements);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062}
63
64android::AutoBufferPointer::~AutoBufferPointer() {
Orion Hodsone522c992019-04-15 16:14:51 +010065 if (nullptr != fArray) {
66 releasePointer(fEnv, fArray, fElements, fCommit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 }
68}