Skip to content

Commit

Permalink
Use NativeMemory abstraction everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-s committed Jan 24, 2020
1 parent 82b2d76 commit c5acf56
Show file tree
Hide file tree
Showing 28 changed files with 1,080 additions and 915 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -51,6 +51,7 @@ com.oracle.truffle.r.native/run/Makeconf.etc-e
/deparse/
/DEPARSE_ERROR
/dumps/
/graal_dumps/
/Rpkgsource/*
/install.cran.logs/
/library/
Expand Down Expand Up @@ -162,3 +163,4 @@ lib.install.packages.gnur
.Rproj.user
com.oracle.truffle.r.test.native/packages/*/*/src/*.so
com.oracle.truffle.r.test.native/packages/*/*/src/*.o
tags
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -107,8 +107,9 @@
import com.oracle.truffle.r.runtime.ffi.DLL.DotSymbol;
import com.oracle.truffle.r.runtime.ffi.DLL.SymbolHandle;
import com.oracle.truffle.r.runtime.ffi.FFIMaterializeNode;
import com.oracle.truffle.r.runtime.ffi.UnsafeAdapter;
import com.oracle.truffle.r.runtime.ffi.VectorRFFIWrapper;
import com.oracle.truffle.r.runtime.ffi.util.NativeMemory;
import com.oracle.truffle.r.runtime.ffi.util.NativeMemory.ElementType;
import com.oracle.truffle.r.runtime.gnur.SA_TYPE;
import com.oracle.truffle.r.runtime.gnur.SEXPTYPE;
import com.oracle.truffle.r.runtime.nmath.RMath;
Expand All @@ -117,8 +118,6 @@
import com.oracle.truffle.r.runtime.nodes.RSyntaxNode;
import com.oracle.truffle.r.runtime.rng.RRNG;

import sun.misc.Unsafe;

/**
* This class provides a simple Java-based implementation of {@link UpCallsRFFI}, where all the
* argument values are standard Java types, i.e. no special types used by Truffle NFI or Truffle
Expand Down Expand Up @@ -1464,7 +1463,7 @@ public int R_ReadConnection(int fd, long bufAddress, int size) {
} catch (IOException e) {
throw RError.error(RError.SHOW_CALLER, RError.Message.ERROR_READING_CONNECTION, e.getMessage());
}
UnsafeAdapter.UNSAFE.copyMemory(buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, bufAddress, Math.min(result, size));
NativeMemory.copyMemory(buf, bufAddress, ElementType.BYTE, Math.min(result, size));
return result;
}

Expand All @@ -1473,7 +1472,7 @@ public int R_ReadConnection(int fd, long bufAddress, int size) {
public int R_WriteConnection(int fd, long bufAddress, int size) {
// Workaround using Unsafe until GR-5927 is fixed
byte[] buf = new byte[size];
UnsafeAdapter.UNSAFE.copyMemory(null, bufAddress, buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, size);
NativeMemory.copyMemory(bufAddress, buf, ElementType.BYTE, size);
try (BaseRConnection fromIndex = RConnection.fromIndex(fd)) {
final ByteBuffer wrapped = ByteBuffer.wrap(buf);
fromIndex.writeBin(wrapped);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -110,8 +110,8 @@ protected void afterCall(Object before, NativeFunction fn, TruffleObject target,

for (int i = 0; i < args.length; i++) {
Object obj = args[i];
if (obj instanceof NativeArray<?>) {
((NativeArray<?>) obj).refresh();
if (obj instanceof NativeArray) {
((NativeArray) obj).refresh();
}
}
}
Expand Down
Expand Up @@ -27,7 +27,6 @@
import static com.oracle.truffle.r.runtime.ffi.RFFILog.logDownCallReturn;
import static com.oracle.truffle.r.runtime.ffi.RFFILog.logEnabled;

import java.lang.reflect.Field;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.concurrent.locks.ReentrantLock;
Expand Down Expand Up @@ -71,25 +70,6 @@
import com.oracle.truffle.r.runtime.ffi.ZipRFFI;

import com.oracle.truffle.r.runtime.ffi.util.NativeMemory;
import sun.misc.Unsafe;

class UnsafeAdapter {
public static final Unsafe UNSAFE = initUnsafe();

private static Unsafe initUnsafe() {
try {
return Unsafe.getUnsafe();
} catch (SecurityException se) {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(Unsafe.class);
} catch (Exception e) {
throw new RuntimeException("exception while trying to get Unsafe", e);
}
}
}
}

public class TruffleNFI_Context extends RFFIContext {

Expand Down Expand Up @@ -240,7 +220,7 @@ private long initCallbacksAddress() {
private void initCallbacks(RContext context) {
if (context.getKind() == ContextKind.SHARE_NOTHING) {
// create and fill a new callbacks table
callbacks = NativeMemory.allocate(Callbacks.values().length * Unsafe.ARRAY_LONG_INDEX_SCALE, "callbacks");
callbacks = NativeMemory.allocate(Callbacks.values().length * Long.BYTES, "callbacks");
InteropLibrary interop = InteropLibrary.getFactory().getUncached();
Object addCallback;
try {
Expand Down Expand Up @@ -283,9 +263,9 @@ private long pushCallbacks() {
assert callbacks != 0L;
if (singleThreadOnly && callbacksAddressThread == Thread.currentThread().getId()) {
// Fast path for contexts used only from a single thread
long oldCallbacks = UnsafeAdapter.UNSAFE.getLong(callbacksAddress);
long oldCallbacks = NativeMemory.getLong(callbacksAddress);
assert callbacksAddress != 0L;
UnsafeAdapter.UNSAFE.putLong(callbacksAddress, callbacks);
NativeMemory.putLong(callbacksAddress, callbacks);
return oldCallbacks;
}
// Slow path: cache the address, but reinitialize it if the thread has changed, without
Expand All @@ -299,20 +279,20 @@ private long pushCallbacks() {
lastCallbacksAddress = initCallbacksAddress();
lastCallbacksAddressThread = Thread.currentThread().getId();
}
long oldCallbacks = UnsafeAdapter.UNSAFE.getLong(lastCallbacksAddress);
long oldCallbacks = NativeMemory.getLong(lastCallbacksAddress);
assert lastCallbacksAddress != 0L;
assert lastCallbacksAddressThread == Thread.currentThread().getId();
UnsafeAdapter.UNSAFE.putLong(lastCallbacksAddress, callbacks);
NativeMemory.putLong(lastCallbacksAddress, callbacks);
return oldCallbacks;
}

private void popCallbacks(long beforeValue) {
if (beforeValue < 0) {
return;
}
assert !singleThreadOnly || UnsafeAdapter.UNSAFE.getLong(callbacksAddress) == callbacks : "invalid nesting of native calling contexts";
assert singleThreadOnly || UnsafeAdapter.UNSAFE.getLong(lastCallbacksAddress) == callbacks : "invalid nesting of native calling contexts";
UnsafeAdapter.UNSAFE.putLong(callbacksAddress, beforeValue);
assert !singleThreadOnly || NativeMemory.getLong(callbacksAddress) == callbacks : "invalid nesting of native calling contexts";
assert singleThreadOnly || NativeMemory.getLong(lastCallbacksAddress) == callbacks : "invalid nesting of native calling contexts";
NativeMemory.putLong(callbacksAddress, beforeValue);
}

protected void addLibRToDLLContextState(RContext context, DLLInfo libR) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -114,8 +114,8 @@ protected void afterCall(Object before, NativeFunction fn, TruffleObject target,
(RContext.getInstance().getRFFI(TruffleNFI_Context.class)).afterDowncall(before, RFFIFactory.Type.NFI);
for (Object obj : args) {
// TODO: can this ever happen in NFI?
if (obj instanceof NativeArray<?>) {
((NativeArray<?>) obj).refresh();
if (obj instanceof NativeArray) {
((NativeArray) obj).refresh();
}
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,15 +22,15 @@
*/
package com.oracle.truffle.r.ffi.impl.nfi;

import java.nio.charset.StandardCharsets;
import com.oracle.truffle.r.runtime.ffi.util.NativeMemory;

import com.oracle.truffle.r.runtime.ffi.interop.UnsafeAdapter;
import java.nio.charset.StandardCharsets;

public class TruffleNFI_Utils {
static String getString(long address, int len) {
byte[] byteArray = new byte[len];
for (int i = 0; i < len; i++) {
byteArray[i] = UnsafeAdapter.UNSAFE.getByte(address + i);
byteArray[i] = NativeMemory.getByte(address, i);
}
return new String(byteArray, StandardCharsets.UTF_8);
}
Expand Down

0 comments on commit c5acf56

Please sign in to comment.