Makoto Onuki | aedf30b | 2024-05-01 11:16:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 <nativehelper/JNIHelp.h> |
| 18 | #include "jni.h" |
| 19 | #include "utils/Log.h" |
| 20 | #include "utils/misc.h" |
| 21 | |
| 22 | |
| 23 | typedef void (*FreeFunction)(void*); |
| 24 | |
| 25 | static void NativeAllocationRegistry_applyFreeFunction(JNIEnv*, |
| 26 | jclass, |
| 27 | jlong freeFunction, |
| 28 | jlong ptr) { |
| 29 | void* nativePtr = reinterpret_cast<void*>(static_cast<uintptr_t>(ptr)); |
| 30 | FreeFunction nativeFreeFunction |
| 31 | = reinterpret_cast<FreeFunction>(static_cast<uintptr_t>(freeFunction)); |
| 32 | nativeFreeFunction(nativePtr); |
| 33 | } |
| 34 | |
| 35 | static const JNINativeMethod sMethods_NAR[] = |
| 36 | { |
| 37 | { "applyFreeFunction", "(JJ)V", (void*)NativeAllocationRegistry_applyFreeFunction }, |
| 38 | }; |
| 39 | |
| 40 | extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) |
| 41 | { |
| 42 | JNIEnv* env = NULL; |
| 43 | jint result = -1; |
| 44 | |
| 45 | if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { |
| 46 | ALOGE("GetEnv failed!"); |
| 47 | return result; |
| 48 | } |
| 49 | ALOG_ASSERT(env, "Could not retrieve the env!"); |
| 50 | |
| 51 | ALOGI("%s: JNI_OnLoad", __FILE__); |
| 52 | |
| 53 | // Initialize the Ravenwood version of NativeAllocationRegistry. |
| 54 | // We don't use this JNI on the device side, but if we ever have to do, skip this part. |
| 55 | #ifndef __ANDROID__ |
| 56 | int res = jniRegisterNativeMethods(env, "libcore/util/NativeAllocationRegistry", |
| 57 | sMethods_NAR, NELEM(sMethods_NAR)); |
| 58 | if (res < 0) { |
| 59 | return res; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | return JNI_VERSION_1_4; |
| 64 | } |