aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorBrian Carlstrom <bdc@google.com>2013-05-10 07:59:41 -0700
committerBrian Carlstrom <bdc@google.com>2013-05-10 07:59:41 -0700
commit2a7250b5330daa8191106a53dc46d2310b2f4f16 (patch)
tree9ad754ae88967c926f0aa1c6409ab2e54803fb7c /include
parenta7de6785939e53982e93afacf03647ff1a759b5a (diff)
parentb50e3d9d138cf56d24726b3f7ed6aa14207d9cf5 (diff)
downloadplatform_libnativehelper-2a7250b5330daa8191106a53dc46d2310b2f4f16.tar.gz
platform_libnativehelper-2a7250b5330daa8191106a53dc46d2310b2f4f16.tar.bz2
platform_libnativehelper-2a7250b5330daa8191106a53dc46d2310b2f4f16.zip
resolved conflicts for merge of b50e3d9d to jb-mr2-dev-plus-aosp
Change-Id: If420d6dd7d80810d6768789f3f5ea362c8f11afc
Diffstat (limited to 'include')
-rw-r--r--include/nativehelper/JNIHelp.h19
-rw-r--r--include/nativehelper/JniConstants.h98
-rw-r--r--include/nativehelper/toStringArray.h73
3 files changed, 190 insertions, 0 deletions
diff --git a/include/nativehelper/JNIHelp.h b/include/nativehelper/JNIHelp.h
index 2fadcdf..8a630fe 100644
--- a/include/nativehelper/JNIHelp.h
+++ b/include/nativehelper/JNIHelp.h
@@ -19,6 +19,8 @@
*
* This file may be included by C or C++ code, which is trouble because jni.h
* uses different typedefs for JNIEnv in each language.
+ *
+ * TODO: remove C support.
*/
#ifndef NATIVEHELPER_JNIHELP_H_
#define NATIVEHELPER_JNIHELP_H_
@@ -31,6 +33,12 @@
# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
#endif
+// TODO: the build system doesn't ensure the standard C++ library header files are on the include
+// path when compiling C++, and this file is included all over the place.
+#ifdef LIBCORE_CPP_JNI_HELPERS
+#include <string>
+#endif // LIBCORE_CPP_JNI_HELPERS
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -169,6 +177,17 @@ inline jobject jniGetReferent(JNIEnv* env, jobject ref) {
inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) {
jniLogException(&env->functions, priority, tag, exception);
}
+
+#ifdef LIBCORE_CPP_JNI_HELPERS
+
+extern "C" std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception);
+
+inline std::string jniGetStackTrace(JNIEnv* env, jthrowable exception = NULL) {
+ return jniGetStackTrace(&env->functions, exception);
+}
+
+#endif // LIBCORE_CPP_JNI_HELPERS
+
#endif
/* Logging macros.
diff --git a/include/nativehelper/JniConstants.h b/include/nativehelper/JniConstants.h
new file mode 100644
index 0000000..f11ec9c
--- /dev/null
+++ b/include/nativehelper/JniConstants.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef JNI_CONSTANTS_H_included
+#define JNI_CONSTANTS_H_included
+
+#include "JNIHelp.h"
+
+/**
+ * A cache to avoid calling FindClass at runtime.
+ *
+ * Class lookup is relatively expensive (2.5us on passion-eng at the time of writing), so we do
+ * all such lookups eagerly at startup. This means that code that never uses, say,
+ * java.util.zip.Deflater still has to pay for the lookup, but it means that on a device the cost
+ * is definitely paid during boot and amortized. A central cache also removes the temptation to
+ * dynamically call FindClass rather than add a small cache to each file that needs one. Another
+ * cost is that each class cached here requires a global reference, though in practice we save
+ * enough by not having a global reference for each file that uses a class such as java.lang.String
+ * which is used in several files.
+ *
+ * FindClass is still called in a couple of situations: when throwing exceptions, and in some of
+ * the serialization code. The former is clearly not a performance case, and we're currently
+ * assuming that neither is the latter.
+ *
+ * TODO: similar arguments hold for field and method IDs; we should cache them centrally too.
+ */
+struct JniConstants {
+ static void init(JNIEnv* env);
+
+ static jclass bidiRunClass;
+ static jclass bigDecimalClass;
+ static jclass booleanClass;
+ static jclass byteArrayClass;
+ static jclass byteClass;
+ static jclass calendarClass;
+ static jclass characterClass;
+ static jclass charsetICUClass;
+ static jclass constructorClass;
+ static jclass deflaterClass;
+ static jclass doubleClass;
+ static jclass errnoExceptionClass;
+ static jclass fieldClass;
+ static jclass fieldPositionIteratorClass;
+ static jclass fileDescriptorClass;
+ static jclass floatClass;
+ static jclass gaiExceptionClass;
+ static jclass inet6AddressClass;
+ static jclass inetAddressClass;
+ static jclass inetSocketAddressClass;
+ static jclass inetUnixAddressClass;
+ static jclass inflaterClass;
+ static jclass inputStreamClass;
+ static jclass integerClass;
+ static jclass localeDataClass;
+ static jclass longClass;
+ static jclass methodClass;
+ static jclass mutableIntClass;
+ static jclass mutableLongClass;
+ static jclass objectClass;
+ static jclass objectArrayClass;
+ static jclass outputStreamClass;
+ static jclass parsePositionClass;
+ static jclass patternSyntaxExceptionClass;
+ static jclass realToStringClass;
+ static jclass shortClass;
+ static jclass socketClass;
+ static jclass socketImplClass;
+ static jclass stringClass;
+ static jclass structAddrinfoClass;
+ static jclass structFlockClass;
+ static jclass structGroupReqClass;
+ static jclass structLingerClass;
+ static jclass structPasswdClass;
+ static jclass structPollfdClass;
+ static jclass structStatClass;
+ static jclass structStatFsClass;
+ static jclass structTimevalClass;
+ static jclass structUcredClass;
+ static jclass structUtsnameClass;
+};
+
+#define NATIVE_METHOD(className, functionName, signature) \
+ { #functionName, signature, reinterpret_cast<void*>(className ## _ ## functionName) }
+
+#endif // JNI_CONSTANTS_H_included
diff --git a/include/nativehelper/toStringArray.h b/include/nativehelper/toStringArray.h
new file mode 100644
index 0000000..edd5f26
--- /dev/null
+++ b/include/nativehelper/toStringArray.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#ifndef TO_STRING_ARRAY_H_included
+#define TO_STRING_ARRAY_H_included
+
+#include "jni.h"
+#include "ScopedLocalRef.h"
+
+#include <string>
+#include <vector>
+
+jobjectArray newStringArray(JNIEnv* env, size_t count);
+
+template <typename Counter, typename Getter>
+jobjectArray toStringArray(JNIEnv* env, Counter* counter, Getter* getter) {
+ size_t count = (*counter)();
+ jobjectArray result = newStringArray(env, count);
+ if (result == NULL) {
+ return NULL;
+ }
+ for (size_t i = 0; i < count; ++i) {
+ ScopedLocalRef<jstring> s(env, env->NewStringUTF((*getter)(i)));
+ if (env->ExceptionCheck()) {
+ return NULL;
+ }
+ env->SetObjectArrayElement(result, i, s.get());
+ if (env->ExceptionCheck()) {
+ return NULL;
+ }
+ }
+ return result;
+}
+
+template <typename Counter, typename Getter>
+jobjectArray toStringArray16(JNIEnv* env, Counter* counter, Getter* getter) {
+ size_t count = (*counter)();
+ jobjectArray result = newStringArray(env, count);
+ if (result == NULL) {
+ return NULL;
+ }
+ for (size_t i = 0; i < count; ++i) {
+ int32_t charCount;
+ const jchar* chars = (*getter)(&charCount);
+ ScopedLocalRef<jstring> s(env, env->NewString(chars, charCount));
+ if (env->ExceptionCheck()) {
+ return NULL;
+ }
+ env->SetObjectArrayElement(result, i, s.get());
+ if (env->ExceptionCheck()) {
+ return NULL;
+ }
+ }
+ return result;
+}
+
+JNIEXPORT jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& strings);
+JNIEXPORT jobjectArray toStringArray(JNIEnv* env, const char* const* strings);
+
+#endif // TO_STRING_ARRAY_H_included