diff options
author | Robert Varga <robert.varga@pantheon.sk> | 2016-01-30 18:30:36 +0100 |
---|---|---|
committer | Robert Varga <nite@hq.sk> | 2016-01-30 22:20:30 +0100 |
commit | 81d99acf457ddc199ffbb3355278906cf0585fbc (patch) | |
tree | 7da470cc82ba34b1f838dd2c19fe138b8c91601b /vpp-japi/japi/vppjni_env.c | |
parent | 3142430cea57f810d136bb3249ededb2a4f2f18b (diff) |
Cache jclass/jmethodID/jfieldID references
This patch introduces an initialization framework, which tracks required
references to Java classes and methods.
It works by declaring classes and their constructor signatures, which
are linked into a singly-linked list when the .so initializers are run.
Once JNI_OnLoad() is invoked, this list is walked and all classes and
their initializers are resolved. These are then used while the library
remains loaded. Once JNI_OnUnload() is called, global references are
released, so we can cleanly unload.
The class declaration results in static utility objects being emitted in
the scope of the declaration, hence to allocate an object or an array of
objects is done via simple calls.
Change-Id: I41984c13756339364dbcbf0144b947627e8e4fe1
Signed-off-by: Robert Varga <nite@hq.sk>
Diffstat (limited to 'vpp-japi/japi/vppjni_env.c')
-rw-r--r-- | vpp-japi/japi/vppjni_env.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/vpp-japi/japi/vppjni_env.c b/vpp-japi/japi/vppjni_env.c new file mode 100644 index 00000000000..84991eee242 --- /dev/null +++ b/vpp-japi/japi/vppjni_env.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * 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: + * + * http://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 "vppjni_env.h" + +// Head of the class registration list. +static vppjni_class_t *class_head; +// Head of the class registration list. +static vppjni_field_t *field_head; + +void vppjni_init_register_class(vppjni_class_t *ptr) +{ + vppjni_class_t **where = &class_head; + while (*where != NULL) { + where = &((*where)->next); + } + *where = ptr; +} + +void vppjni_register_field(vppjni_field_t *ptr) { + vppjni_field_t **where = &field_head; + while (*where != NULL) { + where = &((*where)->next); + } + *where = ptr; +} + +jobject vppjni_init_new_object(JNIEnv *env, const vppjni_class_t *ptr, va_list ap) { + jobject obj = (*env)->NewObjectV(env, ptr->jclass, ptr->jinit, ap); + if ((*env)->ExceptionCheck(env)) { + (*env)->ExceptionDescribe(env); + return NULL; + } + + return obj; +} + +int vppjni_env_init(JNIEnv *env) +{ + vppjni_class_t *cwlk; + vppjni_field_t *fwlk; + + for (cwlk = class_head; cwlk != NULL; cwlk = cwlk->next) { + jclass cls; + jmethodID method; + + cls = (*env)->FindClass(env, cwlk->fqcn); + if ((*env)->ExceptionCheck(env)) { + (*env)->ExceptionDescribe(env); + vppjni_uninit(env); + return JNI_ERR; + } + + method = (*env)->GetMethodID(env, cls, "<init>", cwlk->init_sig); + if ((*env)->ExceptionCheck(env)) { + (*env)->ExceptionDescribe(env); + vppjni_uninit(env); + return JNI_ERR; + } + + cwlk->jclass = (*env)->NewGlobalRef(env, cls); + if (cwlk->jclass == NULL) { + vppjni_uninit(env); + return JNI_ERR; + } + cwlk->jinit = method; + } + + for (fwlk = field_head; fwlk != NULL; fwlk = fwlk->next) { + fwlk->jfield = (*env)->GetFieldID(env, fwlk->clsref->jclass, fwlk->name, fwlk->type); + if ((*env)->ExceptionCheck(env)) { + (*env)->ExceptionDescribe(env); + vppjni_uninit(env); + return JNI_ERR; + } + } + + return 0; +} + +void vppjni_env_uninit(JNIEnv *env) { + vppjni_class_t *cwlk; + vppjni_field_t *fwlk; + + for (fwlk = field_head; fwlk != NULL; fwlk = fwlk->next) { + fwlk->jfield = NULL; + } + + for (cwlk = class_head; cwlk != NULL; cwlk = cwlk->next) { + if (cwlk->jclass != NULL ) { + (*env)->DeleteGlobalRef(env, cwlk->jclass); + } + + cwlk->jclass = NULL; + cwlk->jinit = NULL; + } +} + |