Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android/jni/JniJsValue.cpp

Issue 29354787: Issue 4442 - Cache class and ctors references in JNI_Load/UnLoad (Closed)
Patch Set: reverted to NewTuple Created Nov. 21, 2016, 8:25 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniLibrary.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: libadblockplus-android/jni/JniJsValue.cpp
diff --git a/libadblockplus-android/jni/JniJsValue.cpp b/libadblockplus-android/jni/JniJsValue.cpp
index 6464b1eae10f09b28da34c71f8c3a9affc2b4a1f..a9adf4f4ad921c42b4a6ee24bde3b49157aef339 100644
--- a/libadblockplus-android/jni/JniJsValue.cpp
+++ b/libadblockplus-android/jni/JniJsValue.cpp
@@ -20,26 +20,29 @@
#include "JniJsValue.h"
// precached in JNI_OnLoad and released in JNI_OnUnload
-jclass globalJsValueClass;
-jmethodID jsValueClassCtor;
+JniGlobalReference<jclass>* jsValueClass;
+jmethodID jsValueCtor;
void JniJsValue_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
{
- // precache for performance and avoid attaching threads
- jclass localJsValueClass = env->FindClass(PKG("JsValue"));
- globalJsValueClass = (jclass)env->NewGlobalRef(localJsValueClass);
- jsValueClassCtor = env->GetMethodID(globalJsValueClass, "<init>", "(J)V");
- env->DeleteLocalRef(localJsValueClass);
+ jsValueClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("JsValue")));
+ jsValueCtor = env->GetMethodID(jsValueClass->Get(), "<init>", "(J)V");
}
void JniJsValue_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
{
- if (globalJsValueClass)
+ if (jsValueClass)
{
- env->DeleteGlobalRef(globalJsValueClass);
+ delete jsValueClass;
+ jsValueClass = NULL;
}
}
+jclass GetJsValueClass()
+{
+ return (jsValueClass != NULL ? jsValueClass->Get() : NULL);
+}
+
static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr)
{
try
@@ -164,7 +167,7 @@ static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr);
}
-jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclass jsValueClass)
+jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclass jsValueClassArg)
{
if (!jsValue.get())
{
@@ -172,7 +175,7 @@ jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
}
jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue));
- return env->NewObject(globalJsValueClass, jsValueClassCtor, ptr);
+ return env->NewObject(jsValueClass->Get(), jsValueCtor, ptr);
}
AdblockPlus::JsValue* JniGetJsValue(jlong ptr)
« no previous file with comments | « libadblockplus-android/jni/JniJsValue.h ('k') | libadblockplus-android/jni/JniLibrary.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld