summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-05-20 20:54:45 -0700
committerElliott Hughes <enh@google.com>2010-05-20 20:54:45 -0700
commitebca53a204302c5e559b5c2c9014b48048faf4d4 (patch)
tree799ecee95c8a34f917ceb18a4d673894418e5502 /include
parent40679a19b5de44363aa3f68d77cc13a74b41ffd6 (diff)
downloadlibcore-ebca53a204302c5e559b5c2c9014b48048faf4d4.zip
libcore-ebca53a204302c5e559b5c2c9014b48048faf4d4.tar.gz
libcore-ebca53a204302c5e559b5c2c9014b48048faf4d4.tar.bz2
Add write-back ScopedPrimitiveArrays (and use them).
I've left the remaining Get/Release Critical calls in "NativeConverter.cpp" for the next patch, even though getting into position to fix them is part of the point of this patch. Change-Id: I99e15a3cf3919008343ae4dc856c86ced233e07a
Diffstat (limited to 'include')
-rw-r--r--include/ScopedPrimitiveArray.h85
1 files changed, 60 insertions, 25 deletions
diff --git a/include/ScopedPrimitiveArray.h b/include/ScopedPrimitiveArray.h
index b3b16e1..b6dfd95 100644
--- a/include/ScopedPrimitiveArray.h
+++ b/include/ScopedPrimitiveArray.h
@@ -19,47 +19,82 @@
#include "JNIHelp.h"
-// ScopedBooleanArray, ScopedByteArray, ScopedCharArray, ScopedDoubleArray, ScopedFloatArray,
-// ScopedIntArray, ScopedLongArray, and ScopedShortArray provide convenient read-only access to
-// Java arrays from JNI code.
-#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(PRIMITIVE_TYPE, NAME) \
- class Scoped ## NAME ## Array { \
+// ScopedBooleanArrayRO, ScopedByteArrayRO, ScopedCharArrayRO, ScopedDoubleArrayRO,
+// ScopedFloatArrayRO, ScopedIntArrayRO, ScopedLongArrayRO, and ScopedShortArrayRO provide
+// convenient read-only access to Java arrays from JNI code. This is cheaper than read-write
+// access and should be used by default.
+#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(PRIMITIVE_TYPE, NAME) \
+ class Scoped ## NAME ## ArrayRO { \
public: \
- Scoped ## NAME ## Array(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
+ Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
: mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
} \
- ~Scoped ## NAME ## Array() { \
+ ~Scoped ## NAME ## ArrayRO() { \
if (mRawArray) { \
mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \
} \
} \
- const PRIMITIVE_TYPE* get() const { \
- return mRawArray; \
- } \
- const PRIMITIVE_TYPE& operator[](size_t n) const { \
- return mRawArray[n]; \
+ const PRIMITIVE_TYPE* get() const { return mRawArray; } \
+ const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
+ size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
+ private: \
+ JNIEnv* mEnv; \
+ PRIMITIVE_TYPE ## Array mJavaArray; \
+ PRIMITIVE_TYPE* mRawArray; \
+ Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \
+ void operator=(const Scoped ## NAME ## ArrayRO&); \
+ }
+
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short);
+
+#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO
+
+// ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW,
+// ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide
+// convenient read-write access to Java arrays from JNI code. These are more expensive,
+// since they entail a copy back onto the Java heap, and should only be used when necessary.
+#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \
+ class Scoped ## NAME ## ArrayRW { \
+ public: \
+ Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \
+ : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \
+ mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \
} \
- size_t size() const { \
- return mEnv->GetArrayLength(mJavaArray); \
+ ~Scoped ## NAME ## ArrayRW() { \
+ if (mRawArray) { \
+ mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \
+ } \
} \
+ const PRIMITIVE_TYPE* get() const { return mRawArray; } \
+ const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \
+ PRIMITIVE_TYPE* get() { return mRawArray; } \
+ PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \
+ size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \
private: \
JNIEnv* mEnv; \
PRIMITIVE_TYPE ## Array mJavaArray; \
PRIMITIVE_TYPE* mRawArray; \
- Scoped ## NAME ## Array(const Scoped ## NAME ## Array&); \
- void operator=(const Scoped ## NAME ## Array&); \
+ Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \
+ void operator=(const Scoped ## NAME ## ArrayRW&); \
}
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jboolean, Boolean);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jbyte, Byte);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jchar, Char);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jdouble, Double);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jfloat, Float);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jint, Int);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jlong, Long);
-INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jshort, Short);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long);
+INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short);
-#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY
+#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW
#endif // SCOPED_PRIMITIVE_ARRAY_H_included