Skip to content

Commit

Permalink
Introduce CTypeConversion.asByteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hofer committed Oct 12, 2018
1 parent 8bed697 commit 5fd3aea
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
*/
package org.graalvm.nativeimage.c.type;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

import org.graalvm.nativeimage.ImageSingletons;
Expand Down Expand Up @@ -259,4 +261,18 @@ public void close() {
public static CCharPointerPointerHolder toCStrings(CharSequence[] javaStrings) {
return new CCharPointerPointerHolder(javaStrings);
}

/**
* Creates a {@link ByteBuffer} that refers to the native memory at the specified address. The
* passed size becomes the {@linkplain ByteBuffer#capacity capacity} of the byte buffer, and the
* buffer's {@linkplain ByteBuffer#order() byte order} is set to
* {@linkplain ByteOrder#nativeOrder() native byte order}. The caller is responsible for
* ensuring that the memory can be safely accessed while the ByteBuffer is used, and for freeing
* the memory afterwards.
*
* @since 1.0
*/
public static ByteBuffer asByteBuffer(PointerBase address, int size) {
return ImageSingletons.lookup(CTypeConversionSupport.class).asByteBuffer(address, size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
*/
package org.graalvm.nativeimage.impl;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;

public interface CTypeConversionSupport {
Expand All @@ -59,4 +61,6 @@ public interface CTypeConversionSupport {
String toJavaString(CCharPointer cString, UnsignedWord length);

String toJavaString(CCharPointer cString, UnsignedWord length, Charset charset);

ByteBuffer asByteBuffer(PointerBase address, int size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
import org.graalvm.compiler.debug.Indent;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.c.function.CodePointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.word.Pointer;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.code.CodeInfoEncoder;
import com.oracle.svm.core.code.CodeInfoTable;
Expand Down Expand Up @@ -294,7 +294,7 @@ private void installOperation() {
}

/* Write primitive constants to the buffer, record object constants with offsets */
ByteBuffer constantsBuffer = SubstrateUtil.wrapAsByteBuffer(code.add(constantsOffset), compilation.getDataSection().getSectionSize());
ByteBuffer constantsBuffer = CTypeConversion.asByteBuffer(code.add(constantsOffset), compilation.getDataSection().getSectionSize());
compilation.getDataSection().buildDataSection(constantsBuffer, (position, constant) -> {
objectConstants.add(constantsOffset + position, (SubstrateObjectConstant) constant);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@

import com.oracle.svm.core.LibCHelper;
import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.InjectAccessors;
Expand Down Expand Up @@ -161,11 +160,11 @@ static byte[][] environ() {
int valLength = (int) LibC.strlen(valBeg).rawValue();

byte[] var = new byte[varLength];
SubstrateUtil.wrapAsByteBuffer(varBeg, varLength).get(var);
CTypeConversion.asByteBuffer(varBeg, varLength).get(var);
result[2 * j] = var;

byte[] val = new byte[valLength];
SubstrateUtil.wrapAsByteBuffer(valBeg, valLength).get(val);
CTypeConversion.asByteBuffer(valBeg, valLength).get(val);
result[2 * j + 1] = val;

j++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.util.Arrays;

import org.graalvm.compiler.api.replacements.Fold;
Expand Down Expand Up @@ -56,7 +55,6 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.code.CodeInfoTable;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.deopt.DeoptimizationSupport;
import com.oracle.svm.core.deopt.DeoptimizedFrame;
import com.oracle.svm.core.deopt.Deoptimizer;
Expand Down Expand Up @@ -149,30 +147,10 @@ public static UnsignedWord strlen(CCharPointer str) {
return n;
}

@TargetClass(className = "java.nio.DirectByteBuffer")
@SuppressWarnings("unused")
static final class Target_java_nio_DirectByteBuffer {
@Alias
Target_java_nio_DirectByteBuffer(long addr, int cap) {
}

@Alias
public native long address();
}

/**
* Wraps a pointer to C memory into a {@link ByteBuffer}.
*
* @param pointer The pointer to C memory.
* @param size The size of the C memory.
* @return A new {@link ByteBuffer} wrapping the pointer.
*/
/** @deprecated replaced by {@link CTypeConversion#asByteBuffer(PointerBase, int)} */
@Deprecated
public static ByteBuffer wrapAsByteBuffer(PointerBase pointer, int size) {
return KnownIntrinsics.unsafeCast(new Target_java_nio_DirectByteBuffer(pointer.rawValue(), size), ByteBuffer.class).order(ConfigurationValues.getTarget().arch.getByteOrder());
}

public static <T extends PointerBase> T getBaseAddress(MappedByteBuffer buffer) {
return WordFactory.pointer(KnownIntrinsics.unsafeCast(buffer, Target_java_nio_DirectByteBuffer.class).address());
return CTypeConversion.asByteBuffer(pointer, size);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.oracle.svm.core.c;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

Expand All @@ -34,11 +35,16 @@
import org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder;
import org.graalvm.nativeimage.impl.CTypeConversionSupport;
import org.graalvm.word.Pointer;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.snippets.KnownIntrinsics;

class CTypeConversionSupportImpl implements CTypeConversionSupport {

Expand Down Expand Up @@ -130,6 +136,20 @@ public CCharPointerHolder toCString(CharSequence javaString) {
}
return new CCharPointerHolderImpl(javaString);
}

@TargetClass(className = "java.nio.DirectByteBuffer")
@SuppressWarnings("unused")
static final class Target_java_nio_DirectByteBuffer {
@Alias
Target_java_nio_DirectByteBuffer(long addr, int cap) {
}
}

@Override
public ByteBuffer asByteBuffer(PointerBase address, int size) {
ByteBuffer byteBuffer = KnownIntrinsics.unsafeCast(new Target_java_nio_DirectByteBuffer(address.rawValue(), size), ByteBuffer.class);
return byteBuffer.order(ConfigurationValues.getTarget().arch.getByteOrder());
}
}

final class CCharPointerHolderImpl implements CCharPointerHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ static JNIObjectHandle NewString(JNIEnvironment env, CShortPointer unicode, int
static JNIObjectHandle NewStringUTF(JNIEnvironment env, CCharPointer bytes) {
String str = null;
if (bytes.isNonNull()) {
ByteBuffer buffer = SubstrateUtil.wrapAsByteBuffer(bytes, Integer.MAX_VALUE);
ByteBuffer buffer = CTypeConversion.asByteBuffer(bytes, Integer.MAX_VALUE);
try {
str = Utf8.utf8ToString(true, buffer);
} catch (CharConversionException ignore) {
Expand Down Expand Up @@ -578,7 +578,7 @@ static void GetStringUTFRegion(JNIEnvironment env, JNIObjectHandle hstr, int sta
}
int capacity = Utf8.maxUtf8ByteLength(len, true); // estimate: caller must pre-allocate
// enough
ByteBuffer buffer = SubstrateUtil.wrapAsByteBuffer(buf, capacity);
ByteBuffer buffer = CTypeConversion.asByteBuffer(buf, capacity);
Utf8.substringToUtf8(buffer, str, start, start + len, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.MonitorSupport;
import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.c.function.CEntryPointActions;
import com.oracle.svm.core.c.function.CEntryPointOptions;
Expand Down Expand Up @@ -273,7 +272,7 @@ static int attachCurrentThread(JNIJavaVM vm, JNIEnvironmentPointer penv, JNIJava
if (args.isNonNull() && args.getVersion() != JNIVersion.JNI_VERSION_1_1()) {
group = JNIObjectHandles.getObject(args.getGroup());
if (args.getName().isNonNull()) {
ByteBuffer buffer = SubstrateUtil.wrapAsByteBuffer(args.getName(), Integer.MAX_VALUE);
ByteBuffer buffer = CTypeConversion.asByteBuffer(args.getName(), Integer.MAX_VALUE);
try {
name = Utf8.utf8ToString(true, buffer);
} catch (CharConversionException ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CCharPointerPointer;
import org.graalvm.nativeimage.c.type.CIntPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.nativeimage.c.type.WordPointer;
import org.graalvm.word.Pointer;
import org.graalvm.word.PointerBase;
import org.graalvm.word.WordBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.c.function.CEntryPointActions;
import com.oracle.svm.core.c.function.CEntryPointOptions;
import com.oracle.svm.core.c.function.CEntryPointOptions.Publish;
Expand Down Expand Up @@ -85,7 +85,7 @@ private ByteBuffer createRetBuffer(PointerBase buffer) {
if (size < SizeOf.get(ffi_arg.class)) {
size = SizeOf.get(ffi_arg.class);
}
return SubstrateUtil.wrapAsByteBuffer(buffer, size);
return CTypeConversion.asByteBuffer(buffer, size);
}

static Target_com_oracle_truffle_nfi_impl_ClosureNativePointer prepareClosure(Target_com_oracle_truffle_nfi_impl_NFIContext ctx,
Expand Down Expand Up @@ -125,7 +125,7 @@ private Object call(WordPointer argPointers, ByteBuffer retBuffer) {
// skip
} else {
WordPointer argPtr = argPointers.read(i);
args[argIdx++] = SubstrateUtil.wrapAsByteBuffer(argPtr, argTypes[i].size);
args[argIdx++] = CTypeConversion.asByteBuffer(argPtr, argTypes[i].size);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@

import org.graalvm.nativeimage.ObjectHandle;
import org.graalvm.nativeimage.ObjectHandles;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platform.DARWIN;
import org.graalvm.nativeimage.Platform.LINUX;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.nativeimage.c.type.CTypeConversion;
import org.graalvm.word.SignedWord;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;
Expand All @@ -44,9 +48,6 @@
import com.oracle.svm.core.util.VMError;
import com.oracle.svm.truffle.nfi.NativeAPI.TruffleContextHandle;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platform.DARWIN;
import org.graalvm.nativeimage.Platform.LINUX;

public final class TruffleNFISupport {

Expand Down Expand Up @@ -142,7 +143,7 @@ static String utf8ToJavaString(CCharPointer str) {
return null;
} else {
UnsignedWord len = SubstrateUtil.strlen(str);
ByteBuffer buffer = SubstrateUtil.wrapAsByteBuffer(str, (int) len.rawValue());
ByteBuffer buffer = CTypeConversion.asByteBuffer(str, (int) len.rawValue());
return UTF8.decode(buffer).toString();
}
}
Expand Down

0 comments on commit 5fd3aea

Please sign in to comment.