diff --git a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c index 3d88384fb1884329d4ecc2119451e47704ea5633..9640c79cbb77f77dcb24ee6e32f1d5282b818301 100644 --- a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c +++ b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.c @@ -131,44 +131,47 @@ jmp_buf *getErrorJmpBuf() { } void releaseCopiedVector(JNIEnv *env, CopiedVector cv) { - if (cv.obj != NULL) { - switch (cv.type) { - case INTSXP: { - jintArray intArray = (jintArray) cv.jArray; - (*env)->ReleaseIntArrayElements(env, intArray, (jint *)cv.data, 0); - break; - } - - case LGLSXP: { - // for LOGICAL, we need to convert back to 1-byte elements - jintArray byteArray = (jbyteArray) cv.jArray; - int len = (*env)->GetArrayLength(env, byteArray); - jbyte* internalData = (*env)->GetByteArrayElements(env, byteArray, NULL); - int* data = (int*) cv.data; - for (int i = 0; i < len; i++) { - internalData[i] = data[i] == NA_INTEGER ? 255 : (jbyte) data[i]; - } - (*env)->ReleaseByteArrayElements(env, byteArray, internalData, 0); - break; - } - - case REALSXP: { - jdoubleArray doubleArray = (jdoubleArray) cv.jArray; - (*env)->ReleaseDoubleArrayElements(env, doubleArray, (jdouble *)cv.data, 0); - break; - - } - - case RAWSXP: { - jbyteArray byteArray = (jbyteArray) cv.jArray; - (*env)->ReleaseByteArrayElements(env, byteArray, (jbyte *)cv.data, 0); - break; - - } - default: - fatalError("copiedVector type"); + if (cv.obj != NULL) { +#if TRACE_COPIES + fprintf(traceFile, "releaseCopiedVector(%p)\n", cv.obj); +#endif + switch (cv.type) { + case INTSXP: { + jintArray intArray = (jintArray) cv.jArray; + (*env)->ReleaseIntArrayElements(env, intArray, (jint *)cv.data, 0); + break; + } + + case LGLSXP: { + // for LOGICAL, we need to convert back to 1-byte elements + jintArray byteArray = (jbyteArray) cv.jArray; + int len = (*env)->GetArrayLength(env, byteArray); + jbyte* internalData = (*env)->GetByteArrayElements(env, byteArray, NULL); + int* data = (int*) cv.data; + for (int i = 0; i < len; i++) { + internalData[i] = data[i] == NA_INTEGER ? 255 : (jbyte) data[i]; + } + (*env)->ReleaseByteArrayElements(env, byteArray, internalData, 0); + break; + } + + case REALSXP: { + jdoubleArray doubleArray = (jdoubleArray) cv.jArray; + (*env)->ReleaseDoubleArrayElements(env, doubleArray, (jdouble *)cv.data, 0); + break; + + } + + case RAWSXP: { + jbyteArray byteArray = (jbyteArray) cv.jArray; + (*env)->ReleaseByteArrayElements(env, byteArray, (jbyte *)cv.data, 0); + break; + + } + default: + fatalError("copiedVector type"); + } } - } } void callExit(JNIEnv *env) { diff --git a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.h b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.h index ee3ee6a5b7c9e200364beb863db28c61f9c19bee..bc3ba3dbd35922ddc7ba31ba2b1c91fb3ee4176e 100644 --- a/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.h +++ b/com.oracle.truffle.r.native/fficall/src/jni/rffiutils.h @@ -86,7 +86,7 @@ extern jclass RRuntimeClass; extern FILE *traceFile; // tracing/debugging support, set to 1 and recompile to enable -#define TRACE_UPCALLS 1 // trace upcalls +#define TRACE_UPCALLS 0 // trace upcalls #define TRACE_REF_CACHE 0 // trace JNI reference cache #define TRACE_COPIES 0 // trace copying of internal arrays #define TRACE_ENABLED TRACE_UPCALLS || TRACE_REF_CACHE || TRACE_COPIES diff --git a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java index db771135995605c5808df8fdd139438310cb2452..9672551088a89386556a01555282a82d0eb92cbb 100644 --- a/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java +++ b/com.oracle.truffle.r.runtime.ffi/src/com/oracle/truffle/r/runtime/ffi/RFFIUtils.java @@ -71,14 +71,7 @@ public class RFFIUtils { if (traceEnabled) { if (RContext.isEmbedded()) { if (traceStream == null) { - Path tracePath = Utils.getLogPath(TRACEFILE); - try { - traceFileStream = new FileOutputStream(tracePath.toString()); - traceStream = new PrintStream(traceFileStream); - } catch (IOException ex) { - System.err.println(ex.getMessage()); - System.exit(1); - } + initTraceStream(); } } else { traceStream = System.out; @@ -88,6 +81,17 @@ public class RFFIUtils { } } + private static void initTraceStream() { + Path tracePath = Utils.getLogPath(TRACEFILE); + try { + traceFileStream = new FileOutputStream(tracePath.toString()); + traceStream = new PrintStream(traceFileStream); + } catch (IOException ex) { + System.err.println(ex.getMessage()); + System.exit(1); + } + } + /** * Upcalled from native when tracing to get FD of the {@link #traceFileStream}. Allows the same * fd to be used on both sides of the JNI boundary. @@ -95,6 +99,10 @@ public class RFFIUtils { @SuppressWarnings("unused") private static FileDescriptor getTraceFileDescriptor() { try { + if (traceStream == null) { + // Happens if native has tracing enabled and Java does not + initTraceStream(); + } return traceFileStream.getFD(); } catch (IOException ex) { System.err.println(ex.getMessage());