Skip to content

Commit

Permalink
Merge pull request #4328 from gacholio/jvm
Browse files Browse the repository at this point in the history
Initial implementation of JVM_CopySwapMemory
  • Loading branch information
DanHeidinga authored Jan 17, 2019
2 parents ee2036a + 0362067 commit 92e9b4b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
113 changes: 112 additions & 1 deletion runtime/j9vm/j8vmi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2002, 2017 IBM Corp. and others
* Copyright (c) 2002, 2019 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -99,3 +99,114 @@ JVM_GetTemporaryDirectory(JNIEnv *env)
return result;
}


/**
* Copies memory from one place to another, endian flipping the data.
*
* @param [in] env Pointer to JNI environment
* @param [in] srcObj Source primitive array (NULL means srcOffset represents native memory)
* @param [in] srcOffset Offset in source array / address in native memory
* @param [in] dstObj Destination primitive array (NULL means dstOffset represents native memory)
* @param [in] dstOffset Offset in destination array / address in native memory
* @param [in] size Number of bytes to copy
* @param [in] elemSize Size of elements to copy and flip
*
* elemSize = 1 means a straight byte copy
* elemSize = 2 means byte order 1,2 becomes 2,1
* elemSize = 4 means byte order 1,2,3,4 becomes 4,3,2,1
* elemSize = 8 means byte order 1,2,3,4,5,6,7,8 becomes 8,7,6,5,4,3,2,1
*/
void JNICALL
JVM_CopySwapMemory(JNIEnv *env, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)
{
void *srcBytes = NULL;
void *dstBytes = NULL;
U_8 *srcAddr = NULL;
U_8 *dstAddr = NULL;
/* Ensure that size is a non-negative multiple of elemSize */
Assert_SC_true(size >= 0);
Assert_SC_true(0 == (size % elemSize));
if (NULL != srcObj) {
/* Disallow negative object offsets */
Assert_SC_true(srcOffset >= 0);
srcBytes = (*env)->GetPrimitiveArrayCritical(env, srcObj, NULL);
srcAddr = srcBytes;
}
if (NULL != dstObj) {
/* Disallow negative object offsets */
Assert_SC_true(dstOffset >= 0);
dstBytes = (*env)->GetPrimitiveArrayCritical(env, dstObj, NULL);
dstAddr = dstBytes;
}
srcAddr += (UDATA)srcOffset;
dstAddr += (UDATA)dstOffset;
/*
Generic code would be:
jlong elemCount = size / elemSize;
for (jlong i = 0; i < elemCount; ++i) {
for (UDATA j = 0; j < elemSize; ++j) {
dstAddr[j] = srcAddr[elemSize - j - 1];
}
srcAddr += elemSize;
dstAddr += elemSize;
}
break;
}
*/
switch(elemSize) {
case 1:
memcpy(dstAddr, srcAddr, (size_t)size);
break;
case 2: {
jlong elemCount = size / 2;
while (0 != elemCount) {
dstAddr[0] = srcAddr[1];
dstAddr[1] = srcAddr[0];
srcAddr += 2;
dstAddr += 2;
elemCount -= 1;
}
break;
}
case 4: {
jlong elemCount = size / 4;
while (0 != elemCount) {
dstAddr[0] = srcAddr[3];
dstAddr[1] = srcAddr[2];
dstAddr[2] = srcAddr[1];
dstAddr[3] = srcAddr[0];
srcAddr += 4;
dstAddr += 4;
elemCount -= 1;
}
break;
}
case 8: {
jlong elemCount = size / 8;
while (0 != elemCount) {
dstAddr[0] = srcAddr[7];
dstAddr[1] = srcAddr[6];
dstAddr[2] = srcAddr[5];
dstAddr[3] = srcAddr[4];
dstAddr[4] = srcAddr[3];
dstAddr[5] = srcAddr[2];
dstAddr[6] = srcAddr[1];
dstAddr[7] = srcAddr[0];
srcAddr += 8;
dstAddr += 8;
elemCount -= 1;
}
break;
}
default:
Assert_SC_unreachable();
break;
}
if (NULL != srcObj) {
(*env)->ReleasePrimitiveArrayCritical(env, srcObj, srcBytes, 0);
}
if (NULL != dstObj) {
(*env)->ReleasePrimitiveArrayCritical(env, dstObj, dstBytes, 0);
}
}
3 changes: 2 additions & 1 deletion runtime/j9vm/j9vmnatives.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright (c) 2007, 2018 IBM Corp. and others
Copyright (c) 2007, 2019 IBM Corp. and others
This program and the accompanying materials are made available under
the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -287,6 +287,7 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-excepti
<export name="_JVM_GetMethodTypeAnnotations@8"/>
<export name="_JVM_IsVMGeneratedMethodIx@12"/>
<export name="JVM_GetTemporaryDirectory"/>
<export name="_JVM_CopySwapMemory@44"/>

<!-- Additions for Java 9 (Modularity) -->
<export name="JVM_DefineModule"/>
Expand Down
3 changes: 2 additions & 1 deletion runtime/redirector/forwarders.m4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dnl Copyright (c) 2001, 2018 IBM Corp. and others
dnl Copyright (c) 2001, 2019 IBM Corp. and others
dnl
dnl This program and the accompanying materials are made available under
dnl the terms of the Eclipse Public License 2.0 which accompanies this
Expand Down Expand Up @@ -256,6 +256,7 @@ _X(JVM_GetMethodParameters,JNICALL,true,jobjectArray ,JNIEnv *env, jobject metho
_X(JVM_GetMethodTypeAnnotations,JNICALL,true,jbyteArray ,JNIEnv *env, jobject method)
_X(JVM_IsVMGeneratedMethodIx,JNICALL,true,jboolean ,JNIEnv *env, jclass cb, jint index)
_X(JVM_GetTemporaryDirectory,JNICALL,false,jstring ,JNIEnv *env)
_X(JVM_CopySwapMemory,JNICALL,true,void ,JNIEnv *env, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)
_X(JVM_GetPrimitiveField,JNICALL,true,jobject ,jint arg0, jint arg1, jint arg2, jint arg3)
_X(JVM_InitializeCompiler,JNICALL,true,jobject ,jint arg0, jint arg1)
_X(JVM_IsSilentCompiler,JNICALL,true,jobject ,jint arg0, jint arg1)
Expand Down

0 comments on commit 92e9b4b

Please sign in to comment.