blob: 5b9542fb2b72a50f2fa08ff8a3494eae742704ad [file]
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <jni.h>
#include "bindings/java/com/google/iree/native/function_wrapper.h"
#include "iree/base/logging.h"
#define JNI_FUNC extern "C" JNIEXPORT
#define JNI_PREFIX(METHOD) Java_com_google_iree_Function_##METHOD
using iree::java::FunctionWrapper;
namespace {
// Returns a pointer to the native IREE function stored by the FunctionWrapper
// object.
static FunctionWrapper* GetFunctionWrapper(JNIEnv* env, jobject obj) {
jclass clazz = env->GetObjectClass(obj);
CHECK(clazz);
jfieldID field = env->GetFieldID(clazz, "nativeAddress", "J");
CHECK(field);
return reinterpret_cast<FunctionWrapper*>(env->GetLongField(obj, field));
}
} // namespace
JNI_FUNC jlong JNI_PREFIX(nativeNew)(JNIEnv* env, jobject thiz) {
return reinterpret_cast<jlong>(new FunctionWrapper());
}
JNI_FUNC void JNI_PREFIX(nativeFree)(JNIEnv* env, jobject thiz) {
FunctionWrapper* function = GetFunctionWrapper(env, thiz);
CHECK_NE(function, nullptr);
delete function;
}