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>
This commit is contained in:
Robert Varga
2016-01-30 18:30:36 +01:00
committed by Robert Varga
parent 3142430cea
commit 81d99acf45
5 changed files with 282 additions and 246 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ nobase_include_HEADERS = \
lib_LTLIBRARIES += libvppjni.la
libvppjni_la_SOURCES = japi/vppjni.c japi/vppapi.c
libvppjni_la_SOURCES = japi/vppjni.c japi/vppapi.c japi/vppjni_env.h japi/vppjni_env.c
libvppjni_la_LIBADD = -lvlibmemoryclient -lvlibapi -lsvm -lvppinfra \
-lpthread -lm -lrt
libvppjni_la_LDFLAGS = -module
+52 -243
View File
File diff suppressed because it is too large Load Diff
-2
View File
@@ -150,8 +150,6 @@ typedef struct {
/* attachment of rx thread to java thread */
JNIEnv *jenv;
JavaVM *jvm;
jclass jcls;
jmethodID jmtdIfDetails; // interfaceDetails method
uword *callback_hash; // map context_id => jobject
uword *ping_hash; // map ping context_id => msg type called
+111
View File
@@ -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;
}
}
+118
View File
@@ -0,0 +1,118 @@
/*
* 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.
*/
/*
* Utilities for accessing Java classes/method/fields in an efficient
* manner.
*/
/*
* A potentially-uninitialized reference to a Java class
*/
typedef struct vppjni_class {
// Fully-Qualified Class Name
const char *fqcn;
// Constructor signature
const char *init_sig;
// Global reference to class handle
jclass jclass;
// Constructor method handle
jmethodID jinit;
// Next item in linked list
struct vppjni_class *next;
} vppjni_class_t;
typedef struct jenv_field {
// Field name
const char *name;
// Field type
const char *type;
// Defining class reference
const vppjni_class_t *clsref;
// Field handle
jfieldID jfield;
// Next item in linked list
struct jenv_field *next;
} vppjni_field_t;
#define VPPJNI_CLASS_SYMBOL(name) vppjni_class_##name
#define VPPJNI_CLASS_INIT(name) vppjni_class_##name##_init
#define BIND_JAPI_CLASS(name, sig) \
static vppjni_class_t VPPJNI_CLASS_SYMBOL(name); \
static void VPPJNI_CLASS_INIT(name)(void) __attribute__((__constructor__)); \
static void VPPJNI_CLASS_INIT(name)() \
{ \
VPPJNI_CLASS_SYMBOL(name).fqcn = "org/openvpp/vppjapi/" #name; \
VPPJNI_CLASS_SYMBOL(name).init_sig = sig; \
vppjni_register_class(&VPPJNI_CLASS_SYMBOL(name)); \
} \
static __attribute__((unused)) jobject name##Array(JNIEnv *env, jsize length) \
{ \
return (*env)->NewObjectArray(env, length, VPPJNI_CLASS_SYMBOL(name).jclass, NULL); \
} \
static jobject name##Object(JNIEnv *env, ...) \
{ \
va_list ap; \
va_start(ap, env); \
jobject obj = vppjni_new_object(env, &VPPJNI_CLASS_SYMBOL(name), ap); \
va_end(ap); \
return obj; \
}
#define VPPJNI_FIELD_SYMBOL(cls, name) vppjni_field_##cls##_##name
#define VPPJNI_FIELD_INIT(cls, name) vppjni_field_##cls##_##name##_init
#define BIND_JAPI_FIELD(cls, field, sig) \
static vppjni_field_t VPPJNI_FIELD_SYMBOL(cls, field); \
static void VPPJNI_FIELD_INIT(cls, field)(void) __attribute__((__constructor__)); \
static void VPPJNI_FIELD_INIT(cls, field)() \
{ \
VPPJNI_FIELD_SYMBOL(cls, field).name = #field; \
VPPJNI_FIELD_SYMBOL(cls, field).type = sig; \
VPPJNI_FIELD_SYMBOL(cls, field).clsref = &VPPJNI_CLASS_SYMBOL(cls); \
vppjni_register_field(&VPPJNI_FIELD_SYMBOL(cls, field)); \
}
#define BIND_JAPI_BOOL_FIELD(cls, field) \
BIND_JAPI_FIELD(cls, field, "Z"); \
static void set_##cls##_##field(JNIEnv *env, jobject obj, jboolean value) \
{ \
(*env)->SetBooleanField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
}
#define BIND_JAPI_BYTE_FIELD(cls, field) \
BIND_JAPI_FIELD(cls, field, "B"); \
static void set_##cls##_##field(JNIEnv *env, jobject obj, jbyte value) \
{ \
(*env)->SetByteField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
}
#define BIND_JAPI_INT_FIELD(cls, field) \
BIND_JAPI_FIELD(cls, field, "I"); \
static void set_##cls##_##field(JNIEnv *env, jobject obj, jint value) \
{ \
(*env)->SetIntField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
}
#define BIND_JAPI_OBJ_FIELD(cls, field, sig) \
BIND_JAPI_FIELD(cls, field, sig); \
static void set_##cls##_##field(JNIEnv *env, jobject obj, jobject value) \
{ \
(*env)->SetObjectField(env, obj, VPPJNI_FIELD_SYMBOL(cls, field).jfield, value); \
}
#define BIND_JAPI_STRING_FIELD(cls, field) \
BIND_JAPI_OBJ_FIELD(cls, field, "Ljava/lang/String;")
jobject vppjni_new_object(JNIEnv *env, const vppjni_class_t *ptr, va_list ap);
void vppjni_register_class(vppjni_class_t *ptr);
void vppjni_register_field(vppjni_field_t *ptr);
int vppjni_init(JNIEnv *env);
void vppjni_uninit(JNIEnv *env);